Christian Heilmann

⚡️CSS constants

Friday, February 11th, 2005 at 2:17 am

A lot of web designers complain about CSS lacking constants you could define once and reuse throughout the CSS document. CSS was not meant for that and does not support it.
What you can do though is use a server side language to simulate a CSS file by setting the appropriate header. However, this means you need to stick to the language syntax.

Playing with that idea, I came up with a small PHP script that parses any CSS given to it via a variable and reads and replaces the defined constants in the CSS.

This page shows how it can be used. You apply the CSS with the invalid constants syntax by looping it through the PHP script:

In the CSS file, you define the constants with the following syntax:
$colour1 = '#999';
$colour2 = '#363';
$colour4 = '#696';
$colour3 = '#cfc';

And you can use them throughout the whole CSS document:

padding:.2em .5em;

Any improvements?

I am aware that this is butchering the idea of CSS as presentation only, and means you design invalid CSS in the raw form, but it fills a gap and it might make CSS maintenance a bit easier. You can still validate your CSS by looping it through the script:

Share on Twitter