Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

For example, I'm changing color pallettes throughout a site i'm coding and would love it if i could reference that color somehow instead of replacing the hex value for every item. I know the following doesn't work but i'd like something similar.

color1 = red;
color2 = blue;
color3 = green;

h1 {
color: color1;
background-color: color2;
}

h2 {
color: color2;
background-color: color3;
}
share|improve this question

2 Answers 2

up vote 3 down vote accepted

This is among several missing features in CSS. You can still get this functionality, but you need to move outside the box. Take a look at Less CSS. It's really quite nice.

share|improve this answer
    
Sorry, can only give this +1 –  kevin cline Jun 6 '11 at 16:37
    
oooh. i like that. not sure if i'll use it for good but i'll check it out. thank you. –  Jephrei Jun 15 '11 at 16:14
    
@Jephrei: If you use LessCSS on the server side (using Node.js) to generate your .css files, you can have all of the benefits of it but not impact your page coding at all. –  Peter Rowell Jun 15 '11 at 16:39

What you're asking for is CSS constants and they've been cried for in the web community for years. Not gonna come, sadly.

The usual suggestion is to define container classes to only hold color values then paint document elements by attaching those marker classes:

.color1 { color: red; }
.color2 { color: blue; }
.color3 { color: green; }

<html>
  <body>
    <h1 class="big-header color1">Title</h1>
  </body>
</html>
share|improve this answer
    
thank you. that's PRETTY much what i've been doing so i'm on the right track. –  Jephrei Jun 15 '11 at 16:14
    
You shouldn't use presentational class names. –  melhosseiny Jun 18 '11 at 20:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.