2 replies [Last post]
Last seen: 15 years 42 weeks ago
Joined: 2005-01-03
Posts: 15
Points: 0

After a couple of thoughts on creating dynamic and easy to edit CSS template files (for my opensource projects).

I came up with a solution.

This solution is basically
Use a PHP file as the CSS file.

By utilising PHP's header functions you can use variables and other functions to dynamically creae a CSS file...

This is something that I had never though of before and possibly some other people may not have too...

Has anyone else done this?

I think its a worthy discussion article at least.

It now opens the door to so much.
A simple example is disabling access to the style sheet when not accessed through the website, requiring logged access to view it etc...

Suddenly the realisation of securing CSS files hits you doesn't it!

I appreciate the sharing aspect, but would like to log user accessing the pages, (and I will do so), people using zen garden could log the number of times their CSS file has been requested.

My main use of this will be for some generic templates.

In the file I will hold some variables which will hold the repeating data in the site, this is for such information as background colours, font styles, image locations...

This will make it easier to change the key and repeating values.

Hope to hear a response from you soon.

James Dodd


If you wish to check out my CSS file see

jetboy's picture
Last seen: 8 years 20 weeks ago
Timezone: GMT+1
Joined: 2004-10-20
Posts: 22
Points: 0

PHP File generating CSS

You can use a PHP file in place of a CSS file, but you can also just enable PHP parsing in regular CSS files. On Apache:

AddType application/x-httpd-php .css

will do the trick. I'd say that this was the more common approach by far. Any global values can be held in a separate globals file and included into the stylesheet.

Things to watch out for:

Whichever approach you use, make sure you send a text/css header along with the file:

<?php Header ("Content-type: text/css"); ?>

You'll need this regardless of whether you actually have any other PHP code in there. Netscape 6 (and Mozilla/Firefox) and later will not display your CSS unless you have this in place.

Also, once you enable any kind of file for PHP parsing, the usual headers that get sent to allow user agents to cache the file will no longer be sent. Yes, you can generate these in PHP to allow your CSS to be cached like any other CSS file, but if you do, your PHP won't be re-executed by user agents that have cached it! Catch 22.

Probably the best approach is to have a 'PHP enabled' CSS directory, and a regular CSS directory, and run multiple stylesheets.

If you need to decide to have a play with caching headers, this tells you everything you need to know:


Finally, I believe that CSS Zen Garden uses a local copy of any submitted stylesheets, so you'd need their cooperation to run a dynamic stylesheet. Personally, I think it's very doubtful that they'd allow this.

Last seen: 15 years 42 weeks ago
Joined: 2005-01-03
Posts: 15
Points: 0

PHP File generating CSS

yea I realised that.

Its irrelevant which way you use the files css php (extensions)...

All you have to do is make sure the headers (mime types) are correct...

I'm looking into the document caching at the moment...

I think I need to test things out, obviously there is pros and cons to caching the CSS.

I find it a little annoying during development when I may have to refresh etc...

It doesn't seem to affect the site in an obvious way at the moment...

cssZengarden uses both local CSS files and external ones...

heres a couple of examples