简体   繁体   English

CSS继承类不起作用

[英]css inheritance classes not working

I have two div elements: 我有两个div元素:

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

and those css classes: 和那些CSS类:

.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和clsdiv2继承clsdiv类的背景色,所以我可以这样实现:

.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? 为什么继承不起作用?

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: 如果要使其与当前的CSS一起使用,可以使用以下标记:

<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… 另一方面,这样的设置与继承无关。

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 如果您想让两个div都具有红色背景,请给他们同一个班级

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM