2

I use the same 3-4 colors on 99% of the elements on my website. I know of absolutely no way this is possible, but I'd thought I would ask.

Is there any way to specify a color and quickly reference it within other elements further down the page? For example:

.red_color {
  color: #FF0000;
}

Now, further down the page we have other elements:

div.example {
  padding: 10px;
  color: [REFERENCE ABOVE] 
}

This way, if the color ever changes, I can update it in one place and all the other elements will follow suit.

I know it is possible if I list all the elements in one place, like:

div.example, div.other_example, p {
  color: #FF0000;
}

But this way, every time I add another element to the stylesheet, I have to remember to add it to this list.

Any other ways of doing this?

Thanks.

2
  • Why not just specify a class with a specific color and add that class to all DIVs you want to use that color? It will have the same effect. Commented May 5, 2014 at 16:05
  • 1
    you don't always have control over markup, so adding a class isn't always possible. Commented May 5, 2014 at 16:07

1 Answer 1

4

Yes, but not in CSS. Look at using LESS or SASS. Then you can define variables and use them as you're suggesting.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.