能否用这种方式定义样式?在此示例中,类child通常为红色,除非它被包裹在parent类中,我希望它重置,以便使用parent的样式属性定义的颜色。
.child {
color: red;
}
.parent > .child {
color: _clear_;
}
<div class="parent" style="color:blue;">
<div class="child">Some Text</div>
</div>
我认为对于.parent > .child
,color: inherit;
是您要找的内容:
.child {
color: red;
}
.parent > .child {
color: inherit;
}
<div class="parent" style="color:blue;">
<div class="child">This will be blue</div>
</div>
<br/>
<div class="child">This will still be red</div>
JSFiddle供示例参考
如果您正在使用有效的选择器,就像在您的示例中一样,您应该使用inherit
:
.child {
color: red;
}
.parent > .child {
color: inherit;
}
color
规则,因此子元素将从更高的位置获取值。