PHP in CSS?

He has: 578 posts

Joined: Jun 2004

Is it possible to use php in css?

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

They're independent technologies. Why would you want to use PHP in CSS?

The best I can think of is for some kind of conditional formatting, for which you may use PHP with CSS just as you would use PHP with HTML. PHP effectively doesn't know the difference between HTML and CSS -- it's all just text.

Confused

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

I have seen people do the following:

<link rel="stylesheet" type="text/css" href="style.php" />'

I haven't tried this out for myself though. Another way of doing this would be to have the CSS in the tags.

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

Or you could have PHP print the path to your chosen stylesheet:

<?php
echo \"<link rel=\\"stylesheet\\" type=\\"text/css\\" href=\\"\" . $path . \"\\" />\"
?>

Smiling

He has: 578 posts

Joined: Jun 2004

I am thinking of conditional formatting, and maybe some stuff where I have to have ten buttons, all the same code other than background color. I could do some type of for loop.

He has: 176 posts

Joined: Oct 1999

Dragon of Ice wrote: I am thinking of conditional formatting, and maybe some stuff where I have to have ten buttons, all the same code other than background color. I could do some type of for loop.

This article might be some use to you:
http://www.sitepoint.com/article/css-simple-style-switcher

He has: 578 posts

Joined: Jun 2004

Also, do you think it would still validate?

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

Yeah, it would validate. This is how server-side theme switchers, "currently selected" nav elements and such are done. Smiling

PHP doesn't know what it's printing out, as it's only text as far as it's concerned. The validator doesn't know about any PHP or server-side code, as it's only the output it can see.

He has: 93 posts

Joined: Jun 2004

You could use PHP to generate the buttons with classes assigned as you would normally do with HTML...you would not need to do anything differant with your CSS.
My site for example the main menu and the footer are generated by the same PHP script....the only problem I have at the moment with it is that it dose not validate because I am using an id tag for the active link and am having a little trouble getting it to work with a class tag....if anyone has any ideas.

There are no stupid questions, only stupid people!

slickfish
web site design, production and maintenance for small business

www.justapickle.com
Blogging for the socially conscious

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.