Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the drawbacks of using PHP to create variables in my CSS stylesheet?

Tags:

css

php

One significant drawback of CSS is that one can't use variables. For example, I'd like to use variables to control the location of imported CSS, and it would be awesome to create variables for colors that are used repeatedly in a design.

One approach is to use a PHP file for the CSS stylesheet. In other words, create a "style.php" with...

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

...at the top of the file, and then link to it using...

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

...in any file that uses these styles.

So what's the catch? I think it might be performance -- I did a few quick experiments in Firefox/Firebug and as one would expect, the CSS stylesheet is cached, but the PHP stylesheet isn't. So we're paying the price of an additional GET.

The other annoying thing is that TextMate does not syntax highlight properly for CSS in a .php file.

Are there other drawbacks? Have you used this approach, and if so, would you recommend it?

like image 855
Greg Avatar asked Jun 01 '10 01:06

Greg


1 Answers

Performance is pretty much it. It's a good idea, but only if you cache it. You can send out browser headers to ask the client to pretty-please cache it, but if performance is an issue, you might benefit from developing a system through which you compile your PHP-enabled stylesheets to vanilla CSS files to serve as normal.

If you're going to bother to hand-roll your own compilation system, though, you might want to look into SASS, instead.

like image 186
Matchu Avatar answered Sep 20 '22 23:09

Matchu