Dynamic CSS generation with PHP
Posted: June 2, 2010 Filed under: php Leave a comment »The incentive and idea for writing this article comes from Jeff Atwood’s article “What’s Wrong With CSS?”.
I know that in CSS you need to rewrite stuff and it gets annoying if you need more than 200 lines of CSS, which you most probably do even for tiny projects, while huge web applications could need as many as 2000 lines or more.
The short answer to the problem of repetition, is PHP. You can generate your CSS with PHP files, same way you use PHP to generate HTML. The advantage that you get from this is that you can use variables to store CSS items such as colors. You don’t need to repeat the code for Chartreuse (#7FFF00) over 50 times.
<? $chartreuse = ‘#7FFF00′; ?>
.archive-pagination {
color: <? $chartreuse ?> ;
padding: 10px 0;
}
So you simply use the variable you create for the color or for any other CSS element you need to repeat over and over again. This way you can make CSS a little bit more “programmable” and save some time and effort.


