0

I have two div elements:

  <div  class = "clsdiv1">Hello</div>
  <div  class = "clsdiv2">Bye</div>

and those css classes:

.clsdiv{
  background: red;
}

.clsdiv1 {
  background: black;
  border: 2px solid black;
}
.clsdiv2 {
  background: green;
  border: 2px solid white;
}

I want the clsdiv1 and clsdiv2 inherit background color from clsdiv class,so I implement it this way:

.clsdiv1 .clsdiv{
  background: inherit;
  border: 2px solid black;
}
.clsdiv2 .clsdiv{
    background: inherit;
  border: 2px solid white;
}

But inheritance not working,any idea what I'm missing? Why inheritence not working?

2
  • <div class = "clsdiv1 clsdiv">Hello</div> <div class = "clsdiv2 clsdiv">Bye</div> Commented Dec 8, 2014 at 9:52
  • 1
    I create a JSfiddle for you: jsfiddle.net/nvuwddjn Commented Dec 8, 2014 at 9:53

2 Answers 2

1

Yes, you are not referencing the class name that should get inherited at the element. If you want to make it work with your current CSS, you may use the following markup:

<div  class = "clsdiv clsdiv1">Hello</div>
<div  class = "clsdiv clsdiv2">Bye</div>

You don't even need to inherit the background color, as it already was set to the same element:

.clsdiv {
    background: red;
}
.clsdiv1 {
    border: 2px solid black;
}
.clsdiv2 {
    border: 2px solid white;
}

On the other hand, such a setup has nothing to do with inheritance…

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

Comments

1

Well you are approaching it the wrong way.

<div  class = "clsdiv">Hello</div>
<div  class = "clsdiv">Bye</div>

.clsdiv{
  background: red;
}

if you want both div to have red color background give them the same class

2 Comments

Why would I use !important?
oh that was my mistake.. !important is not necessary here.

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.