9

Lets say I want to define 2 styles.

.color_red { color: Red; }
.banner { display: block; width: 100%; }

Is there a way that I can have the h1 style inherit the color_red style? I don't want to have to do

<div class="banner color_red">This is my banner.</div>

I'd prefer to do something like this...

.banner { ... inherit: color_red; }
...
<div class="banner">This is my banner.</div>

And have the banner have red text.

1

2 Answers 2

19

You have to write:

.color_red, .banner { color: Red; }
.banner { display: block; width: 100%; }
Sign up to request clarification or add additional context in comments.

1 Comment

This is the closest thing I could come up with as well. I guess I'll have to do things this way.
17

No, there is no way to do this directly, better to share style definitions:

.color_red,
.banner
{
  color: red;
}

However I'd like to point out that you're approaching the problem from the wrong direction. Your mark-up should be independant of the style and should lead the style in development ideally. This means that defining .color_red is a serious problem, because it says nothing about the semantics of the mark-up and how much of a problem are you faced with if you need to style that bit of mark-up blue?

Far better to allow your mark-up to exist like this:

<div class="banner highlight">I am a banner AND I am highlighted!</div>

supported by:

<style>
.highlight
{
  color: red;
}

.banner
{
  display: block; 
  width: 100%;
}
</style>

1 Comment

I wanted this to allow for templating of colors. Such as having a dark color 1 etc. My example uses explicit colors for simplicity. I was trying to accomplish the same effect as partial classes in C#.

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.