我想在这个.feature-section-heading
类的所有子元素上添加特定的样式属性。我知道我可以做到这一点使用以下技巧如何通过class或id使用css来选择所有的子元素?
.feature-section-heading > h1 {...}
但上面的逻辑将只实现h1
标记。那么有可能我可以在所有子元素上添加样式属性?我搜索了一下,发现接受answer,但它不起作用。
我想在这个.feature-section-heading
类的所有子元素上添加特定的样式属性。我知道我可以做到这一点使用以下技巧如何通过class或id使用css来选择所有的子元素?
.feature-section-heading > h1 {...}
但上面的逻辑将只实现h1
标记。那么有可能我可以在所有子元素上添加样式属性?我搜索了一下,发现接受answer,但它不起作用。
使用universal selector而不是type selector。
.feature-section-heading > *
访问与Css * selector所有儿童,如下面的代码片段:
.feature-section-heading > * {
background: red;
}
这匹配所有的后代,而不仅仅是儿童。 – Quentin
谢谢,更新选择器 –
CSS
.feature-section-heading > * {...}
可以使用*
所有元素选择。
您可以使用*(Asterik选择)
这里是Demo
CSS
.foo *{background:red}
HTML
<div class="foo">
<span>1</span>
<p>2</p>
<div>3</div>
</div>
请找到jsfiddle链接,说明所需的行为。
HTML
<div class="feature-section-heading">
I am parent div
<br />
<h1>
Heading
</h1>
<div>
Child DIV
</div>
<div>
Child DIV
</div>
<p>
Child paragraph
</p>
<p>
Child paragraph
</p>
<p>
Child paragraph
</p>
<span>
Child span
</span>
</div>
CSS
.feature-section-heading {
color: red;
}
.feature-section-heading > :not(:empty){
color: darkgreen;
}
'.feature形部标题> * {...}'将样式应用到所有直接子(但也有可能做一个更好的方法来实现你所要做的)。通用选择器“*”不被认为是有效的CSS。你想要申请什么? – ne1410s
这是先前询问 http://stackoverflow.com/questions/26349987/how-do-i-apply-a-style-to-all-children-of-an-elements – lacexd