Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CSS - repeating colours, fonts etc "constants" 1

Status
Not open for further replies.

Glasgow

IS-IT--Management
Jul 30, 2001
1,669
GB
A web site I am building has a colour scheme for text and backgrounds. The same colour will often apply to several different styles of text. The same argument will apply to font sizes. So in my style sheet I might have:

Code:
.TxtHigh01          { color: #CC3333; font-size: 12px}
.TxtHigh02          { color: #CC3333; font-size: 8.5pt}
.TxtHigh03          { color: #CC3333; font-size: 10pt}

What would be the best approach (if one exists) to implement this example in such a way that it does not need to be changed in 3 places if the colour scheme changes? In VB I would use a constant but I am aware this is not an option in CSS.

Thanks in advance.
 
It's a real shortcoming in CSS (in my opinion) that there's no way to define constants of this sort. It'd be really good if you could do something like this...
Code:
definecolor my_bg #FFFFCC;

body { background: my_bg; }
Sadly, that's not the way CSS works. You can reduce the duplication by relying on the cascade - there's no need to set a colour for something if its parent element is already that colour - and you can also bulk up your declarations:
Code:
.TxtHigh01, .TxtHigh02, .TxtHigh03 { color: #CC3333; }
.TxtHigh01          { font-size: 12px}
.TxtHigh02          { font-size: 8.5pt}
.TxtHigh03          { font-size: 10pt}
If you really wanted to go to town, you could use a server-side script to generate your stylesheet on the fly, allowing you to use normal variables:
Code:
#!/usr/bin/perl

$my_bg = '#FFFFCC';

print "Content-type: text/css\n\n";

print "body { background: $my_bg; }\n";
But that's probably overkill in most cases.

What I generally do is put a big comment at the top of the stylesheet, listing the all colours and what I use them for. If I decide I need to change them I just do a global find/replace on the file.

-- Chris Hunt
Webmaster & Tragedian
Extra Connections Ltd
 
Thanks Chris - I agree re shortcoming. However, I was not aware that you could repeat references to the same class as per your first suggestion so I may be able to make good use of that.

Thanks again.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top