Page 1 of 1

Use CSS in PHP file?

Posted: Mon Jun 16, 2003 2:14 pm
by slipstream
I have a php file that echos HTML and was wondering if I can use CSS in that PHP to change the look of my text. Will it work? Do I have to echo the CSS command or should I put it above the <?php ?

I am a bit confused.

Posted: Mon Jun 16, 2003 3:05 pm
by trollll
As far as I know, you can either echo the css (just as you can echo html or javascript), or you could put the css code above the <?php. Since css doesn't get used until the browser gets a hold of it and the php runs before then, the browser really won't care how you get the css to it.

You may want to have the php write it out, just in case you want to get fancy with custom css styles down the road. That way (unlike, say, the wired news site) you won't have to create cross-platform/browser javascript and the page won't "jump" after loading.

Hope that helps! :)

Posted: Mon Jun 16, 2003 3:50 pm
by slipstream
Ya I tried to put it before and echo it but neither worked. hmm.. :?:

Hmmm...

Posted: Mon Jun 16, 2003 4:29 pm
by trollll
I tried the following first just with the external stylesheet linked and then with both external and internal to see if a cascading issue would come up, but that worked fine, too.

Code: Select all

<?php
echo "<link rel="stylesheet" type="text/css" href="style_main.css" />";
echo "<style type="text/css">
echo "    body    &#123;background-color: #000000;&#125;";
echo "</style>\n";
?>
What does the code look like for echo-ing the css? Does your css work when you write it directly into html (not with php)?

Posted: Mon Jun 16, 2003 5:40 pm
by patrikG
You can also have php write the HTML-include

Code: Select all

<?php
echo "<link rel='stylesheet' type='text/css' href='css/main.css'>";
?>
or simply do it HTML itself

<link rel='stylesheet' type='text/css' href='css/main.css'>

Bear in mind though that stylesheets have nothing to do with PHP. To PHP it's merely data it hands to the server which then sends it to the client. PHP = serverside.

Posted: Mon Jun 16, 2003 5:58 pm
by trollll
You may want to check out W3Schools CSS2 Reference just to check to see if your css has a glitch in it. Especially as you mentioned that it didn't work when you placed it before the php code. I keep it bookmarked and easy to get to anyway, as it lets you know whether IE or Netscape support each property.