Dynamic CSS generation with PHP

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.

Advertisement


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.