Php in css

Wouldn’t it be nice if we could do php in css files?
I’m at the stage in php now where I can build a dynamic database driven cms site, albeit fairly basic. If I want any dynamic styling, I have to do the css for that inline in order to use php to allow the user to choose their own styling.
It would be great if we could use php (or whichever language you use) in stylesheets as this would surely open up enormous possibilities for dynamic design.

Anyone else ever had this thought? Has it ever been looked at?

Hi BuddyH,

Most people use JavaScript to target dom node selectors like #ids, .classes, <p>, <ul>… This means that the CSS does not need to be inline and you don’t have to use a serverside language to deal with a client side technology.

If you really wanted to, you could write a .css file that has styles that dynamically had been created, and then link (require) it in a php page.

Steve

Try this:


<!document html>
<head>
   <link rel='stylesheet' href='style-001.php' type="text/css" />
</head>
<body>

style-001.php


<?php
header("Content-type: text/css;charset:UTF-8");
$bg2="f00";
?>

body {background:#<?php echo $bg2;?>}


Beware that if PHP errors exist they are difficult to trace?

Try Googling, there are many articles available on using PHP in CSS.

Ah brilliant!! So basically instead of having php in a css file, have the css in a php file. I have Irish blood, I’m supposed to have a natural abillity to think the other way around. Or perhaps that is actually what I did? :stuck_out_tongue:

Thanks guys! :slight_smile:

@BuddyH,

I always have trouble selecting a colour that is highly visible on a coloured background. Googling revealed this link that started me experimenting with PHP and CSS:

http://www.barelyfitz.com/projects/csscolor/