Matt Sheppard Matt Sheppard - 1 year ago 103
CSS Question

Avoiding repeated constants in CSS

Are there any useful techniques for reducing the repetition of constants in a CSS file?

(For example, a bunch of different selectors which should all apply the same colour, or the same font size)?

Answer Source

Elements can belong to more than one class, so you can do something like this:

    background-color: #123456;
    //other stuff here

And then in the content portion somewhere:

<div class="DefaultBackColor SomeOtherStyle DefaultForeColor">Your content</div>

The weaknesses here are that it gets pretty wordy in the body and you're unlikely to be able to get it down to listing a color only once. But you might be able to do it only two or three times and you can group those colors together, perhaps in their own sheet. Now when you want to change the color scheme they're all together and the change is pretty simple.

But, yeah, my biggest complain with CSS is the inability to define your own constants.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download