如何从特定类型获取第一个元素。
h1:first-of-type {
background: red;
}
<h1>Main Title</h1>
<div class="content">
<h1>Another Title</h1>
<div class="para">
<h1>One More Title</h1>
</div>
</div>
我要设置“主要标题”
h1
,背景为红色。问题是所有三个h1
background:red;
http://jsfiddle.net/EaEFa/
最佳答案
如果您只想要第一个h1级别(而不是任何元素中的第一个类型),则可以使用子组合器
body > h1 {
background:red;
}
http://jsfiddle.net/EaEFa/1/
或者,您可以将它们结合起来以仅获得第一个。
body > h1:first-of-type {
background:red;
}
http://jsfiddle.net/EaEFa/2/
当然,这确实取决于html结构以及第一个
h1
的子元素。关于html - 整个页面css3中的第一个类型,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17271235/