Constants in CSS via PHP

Album Cover: Life After Death

"While your gun's raisin', mine is blazin'."
The Notorious B.I.G. / I Love the Dough

Posted on November 08, 2005 1:22 AM in Web Development
Warning: This blog entry was written two or more years ago. Therefore, it may contain broken links, out-dated or misleading content, or information that is just plain wrong. Please read on with caution.

Back in August of last year I talked about the lack of support for constants in CSS. Tonight I stumbled upon a cool article called CSS Colors: Take Control Using PHP. In the article, Patrick Fitzgerald details the steps you can take to mimic CSS constants using PHP.

I'm sure most developers who have hit the brick wall, so to speak, with CSS constants (or the lack thereof) have thought of similar methods, but Patrick does a great job of pointing out exactly what it takes to get the job done with PHP.

The only thing I would add to his tutorial is an explanation of how to utilize PHP's cache control so that the .css files in question are not reloaded every single time a visitor stops by your site. PHP files, because they are typically dynamic in nature, are almost always reloaded, but in the case of Patrick's CSS constants trick, these files will rarely, if ever, change. Therefore, by setting the caching of the PHP-generated .css files appropriately, bandwidth usage is very similar, if not the same, as if the .css files were of the typical garden variety.

I'd still like to see constants supported in CSS by default, but if you're one of those web developers that is itching for a quick fix, Patrick's solution is definitely worth taking a look at.


No one has added any comments.

Post Comments

If you feel like commenting on the above item, use the form below. Your email address will be used for personal contact reasons only, and will not be shown on this website.


Email Address:



Check this box if you hate spam.