1

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;
}
Jephrei
  • 13
  • 3

2 Answers2

5

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>
3

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.

Peter Rowell
  • 7,498