如何将 css 规则应用于元素的所有后代
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8749466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I apply a css rule to all descendants of an elements
提问by clarkk
How can you redefine a class if its in another specific class?
如果一个类在另一个特定的类中,你如何重新定义它?
div.cls {
color:blue;
}
div.tst > div.cls {
color:red;
}
<div class="cls">test</div> // text color = blue
<div class="tst">
<div class="cls">test</div> // text color = red
<div>
<div class="cls">test</div> // text color = blue
</div>
<div>
How to make the last one also red?
怎么让最后一个也红?
jsfiddle
提琴手
采纳答案by Felix Kling
Use the descendant selector[W3C]: div.tst div.cls
使用后代选择器[W3C]:div.tst div.cls
>
is the child selector[W3C]and will only match children of an element.
>
是子选择器[W3C]并且只会匹配元素的子元素。
回答by TomoMiha
I used this, it work for me:
我用过这个,它对我有用:
.name-of-parent * { color: red; }
.name-of-parent * { 颜色:红色;}
回答by animuson
Exactly like that. However, your second division won'tbe red text because it's also contained within another division. The >
selector only matches to the immediate children under the element matched before it, so it's looking inside div.tst
at only one level. Try removing the >
from the selector:
正是这样。但是,您的第二个部门不会是红色文本,因为它也包含在另一个部门中。该>
选择只匹配之前它相匹配的元素下的直接孩子,所以它看起来里面div.tst
只有一个级别。尝试>
从选择器中删除:
div.tst div.cls {
color:red;
}