Use CSS in PHP file?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
slipstream
Forum Commoner
Posts: 86
Joined: Fri Apr 19, 2002 8:53 am
Location: Canada

Use CSS in PHP file?

Post 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.
User avatar
trollll
Forum Contributor
Posts: 181
Joined: Tue Jun 10, 2003 11:56 pm
Location: Round Rock, TX
Contact:

Post 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! :)
slipstream
Forum Commoner
Posts: 86
Joined: Fri Apr 19, 2002 8:53 am
Location: Canada

Post by slipstream »

Ya I tried to put it before and echo it but neither worked. hmm.. :?:
User avatar
trollll
Forum Contributor
Posts: 181
Joined: Tue Jun 10, 2003 11:56 pm
Location: Round Rock, TX
Contact:

Hmmm...

Post 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)?
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post 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.
Last edited by patrikG on Mon Jun 16, 2003 6:00 pm, edited 1 time in total.
User avatar
trollll
Forum Contributor
Posts: 181
Joined: Tue Jun 10, 2003 11:56 pm
Location: Round Rock, TX
Contact:

Post 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.
Post Reply