PDA

View Full Version : Declaring Constants in a CSS file?



AlistairH
06-25-2005, 02:10 PM
Is this possible?

In a css file I have a whole load of elements that have similar settings, colour for example. If I wanted to change the colour during the design phase I don't want to have to go and edit every reference to that colour. In a CSS file can a constant be declared to which a colour is assigned with the elemnets concerned referencing the constant so that all can be changed simply by changing the colour assigned to the constant?

mwinter
06-25-2005, 04:20 PM
Declaring Constants in a CSS file?

Is this possible?No, but there are alternatives.

The first is to combine common declarations into a single rule with multiple selectors. Consider the snippet below, which was posted on Usenet recently:


.titlejgloss {
background : #91eaff;
border-color : #808080;
border-style : inset;
border-width : 1px;
display : block;
font-family : Arial,Helvetica,sans-serif;
font-size : 200%;
font-weight : bold;
padding : 5px;
margin-bottom : 10px;
margin-top : 0px;
}

.titlebgloss {
background : red;
border-color : #808080;
border-style : inset;
border-width : 1px;
display : block;
font-family : Arial,Helvetica,sans-serif;
font-size : 200%;
font-weight : bold;
padding : 5px;
margin-bottom : 10px;
margin-top : 0px;
}The only thing that is different between them is the background property. It could be replaced by:


.titlejgloss,
.titlebgloss {
border: 1px inset #808080;
display: block;
font: bold 200% Arial, Helvetica, sans-serif;
padding: 5px;
margin: 0 0 10px;
}

.titlejgloss {
background: #91eaff;
}

.titlebgloss {
background: red;
}Notice the comma that separates the two class selectors. This could be applied to your situation.

If that isn't adequate, then use a preprocessor of some kind to expand macros within the file. You could use PHP, for instance, on the command line to generate a CSS file from a PHP file, and upload the output.

Mike

AlistairH
06-25-2005, 06:26 PM
That'll do nicely. Thanks Mike.