Click here to Skip to main content
15,892,737 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm new to the php programming...
and i'm doing self learning about the language.

i create a basic php program and now i want to dd css style into it.

how can i do that?

can i add css codes into php code?
or else can i define separate css file in php code to access the css file?

XML
<?php
 echo '<form id="form1" name="form1" method="post" action="">'.
'<p>PASWORD<br>'.
'<input type="text" name="name0" id="text" {color:red;text-align:center;} /><br><br>'.
'<p>BACKLINK<br>'.
'<input type="text" name="name0" id="text" /><br><br>'.
'<p>Your Name<br>'.
'<input type="text" name="name" id="text" />'.
'<br>;'.
'<input type="submit" name="Enter" id="Enter" value="Enter" />'.
'</form>';
?>


i added this CSS short code to colour the text and center it.

CSS
{color:red;text-align:center;}
Posted
Updated 8-Dec-13 18:32pm
v2

Perhaps you don't quite understand the role of PHP or any other server-side technology. It's designed to deliver any content, not specifically HTML or something in particular.

First of all, most often you have static CSS and add it exactly in the same was as in static HTML, which has nothing to do with PHP:
http://www.w3schools.com/css/css_howto.asp[^].

In this case, it does not matter if the HTML file generated via PHP or if it is completely static, the syntax is always the same.

Perhaps you need to calculate CSS elements on the fly. The in could also be generated as PHP on the server side. Then you simply need to use content type "text/css" when you generate a PHP header for CSS: http://php.net/manual/en/function.header.php[^].

It could be done this way:
PHP
<?php
// We'll be outputting a PDF
header('Content-type: text/css');

... your CSS declarations come here:
echo "li { ... }";
...

?>


With styles, the most important thing is to avoid all those ad-hoc style attributes and always use CSS classes, in the HTML files or separate, to be re-used in several HTML files.

—SA
 
Share this answer
 
Comments
Er. Tushar Srivastava 22-Dec-13 14:46pm    
I appreciate your response, but I am pretty sure you have confused a beginner here ... Though, brilliant response.
Sergey Alexandrovich Kryukov 22-Dec-13 15:19pm    
Thank you very much.
I do understand your concern, but this is generally quite difficult to explain certain things on the elementary level. It's also pretty difficult to avoid such confusions. OP should take a more active role and ask some follow-up questions until understanding is achieved. By the way, OP already asked 50 questions, and I believe some more ware automatically deleted, so he had enough time to get some experience of getting knowledge.

Please, you have some some more ideas on how this matter can be clarified, share them.

—SA
Er. Tushar Srivastava 22-Dec-13 15:23pm    
Please Sir have a look at my answer I just posted :) BTW, you are right. It's difficult to explain basics. It take too much time to explain things as simple as you can and then typing it :D Anyways, a Very Merry Christmas to you :)
My Friend,

I will take you down the alley where you live and help you understand what PHP, HTML, CSS and JavaScript are all about. I must mention here though that I will be simplifying the answer of Sir SA Kryukov.

So friend, Let's have a look at HTML :
1. A Markup Language (Built using tags): The tags are parsed in the browser and browser actually displays the objects, format it, color it etc. (That's Why each browser displays the same page slightly different.

2. HTML is used for layout definition of a webpage (not a website: its a collection of webpages linked together via hyperlinks and are stored on a single machine (generally)).

3. Via HTML you create the front-end of your website. You define, the header, footer, body, navigation, sidebar etc via HTML.

4. HTML support addition of non-html content to be added also via some tags like <script> tag is used for javascript, <style> tag is used to define CSS

Now, We will talk about CSS :
1. CSS (Cascading Stylesheet): It is actually you can say language, for styling the layout that you have defined via HTML.

2. You can directly style a particular tag globally For example : a { background: #00eeee; } will make all the hyperlinks of the webpage to have a background of yellow.

3. You can also style a particular tag that itself is encapsulated inside another tag. For example: If you want to make all the <p> tags to have a font-family of "Verdana" which are lying inside a <span> tag; you can write span p { font-family: "Verdana" }

4. You can also target a particular group or a completely unique element using class (for group) and ID (for unique individual) and style them independently.

Now, enough about HTML and CSS. Both are Static to themselves but, when you want to generate a page which is actually generated at the time a user has requested it like a search result or a particular post on a blog. Then?

Well Friend, here comes the world of scripting languages. Scripting languages are like any other language like C, C++ etc but the difference is that these are web development languages.

There are basically two types of scripting. These are :
1. Server-Side Scripting : Example PHP, ASP, ASP.Net, JSP etc
2. Client-Side Scripting: Example Javascript

Now, let us talk about PHP.

PHP is a server-side scripting language. By scripting language, I mean that you can use this language to define your own logic. Thus, you can do some work based on the condition that satisfies. Examples are : if-else, switch-case etc; or, you can loop through a particular block of code to realize a repetitive work without much code. Examples are : while, do-while, for, foreach.

Being, a server -side scripting language, it is run only on server side. That implies that, No part of code is executed on client side, the whole script is executed on server side and it's result is transferred via a web server to client side.

Now, the benefit of this technique is that you can generate HTML codes based on some conditions or generate a repetitive HTML block using Loop etc. Thus, I can generate a search result page. Wherein, I have a variable that has stored all the search result and I can loop through it and display all the content. Moreover, I will also add HTML tags along with the response, so the result of the whole script parsing at server will generate simply a pure HTML (in fact a pure binary. You can set it's mime-type to make it behave like anything thereby making it deliver any content even images or videos can be transmitted not just HTML Cool right?).

Now, Your question: You wanted to add stylesheet to your page that you had generated via PHP. Now, that you know, how PHP is working and what actually is it doing, you know, that similar to a normal HTML page, you have to write the
<link rel="stylesheet" href="global.css" type="text/css" /> in the php echo inside the part where you have echoed the <head> tag since <link> tag is put in the <head> tag.

this is a simple code :
I can mix HTML and PHP too. The PHP part is always put inside a <?php ?> tag block while HTML can be put anywhere. If you have to put HTML inside the <?php ?> tag block. Then you actually have to print it using either print or echo function.
HTML
<html>
<head>
  <title>Test</title>
  <style type="text/css">
  p {
      font-face: "Verdana";
  }
  </style>
</head>
<body>
   // This is a comment
   $message = "Hello World"; // This is a variable in PHP. Its value is Hello World
   echo '<p>'.$message.'</p>';  // Here the result of this operation is <p>Hello World</p>
?>
</body>
</html>


Hope that this complete explanation helped you. Took me 1/2 hrs to type :P Please do rate it and mark it as solved if it solved your problem.

With Regards
Tushar Srivastava
 
Share this answer
 
v5
Comments
Sergey Alexandrovich Kryukov 22-Dec-13 16:21pm    
If if helps OP, I'll be glad I provoked you to do this contribution. But would you first fix the formatting? Can you see the tags auto-generated by the submission scripts? If happens when you forget to escape <> characters from some code above...
—SA
Er. Tushar Srivastava 23-Dec-13 3:41am    
Thank you Sir, I updated the Answer and corrected all the formatting related issues :)
Sergey Alexandrovich Kryukov 23-Dec-13 12:57pm    
Nice, I'm voting 5; hopefully those good points can help OP.
—SA
Er. Tushar Srivastava 25-Dec-13 6:09am    
Thank you Sir and a very Merry Christmas to you :)
Sergey Alexandrovich Kryukov 25-Dec-13 11:34am    
You are welcome, Merry Christmas!
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900