怎么可能选择仅第一个元素(例如 h2)在另一个元素(例如 h1)之后,但它是不一定立即放置在 ?
因此,element+element(例如 h1 + h2)不起作用,因为它选择放置的元素立即地元素之后
<h1> Title1 </h1>
..... <--! many tags but h2 -->
<h2> h2 Title2 selected </h2>
..... <--! many tags but h1 and h2-->
<h2> h2 Title3 not selected </h2>
现在只需一个选择器就可以实现:
h1:first-of-type ~ h2:not(h1:first-of-type ~ h2 ~ h2) {
color:red;
}
<div>
<h2>h2 Title3 not selected </h2>
<h1> Title1 </h1>
<p>text</p>
<h2> h2 Title2 selected </h2>
<h3>text</h3>
<h2> h2 Title3 not selected </h2>
</div>
旧答案
我认为您无法通过一个选择器来实现这一目标,因此您可以尝试如下所示:
h1:first-of-type ~ h2 {
color:red;
}
h1:first-of-type ~ h2 ~ h2 {
color:initial; /*we reset the style for the others*/
}
<div>
<h2>h2 Title3 not selected </h2>
<h1> Title1 </h1>
<p>text</p>
<h2> h2 Title2 selected </h2>
<h3>text</h3>
<h2> h2 Title3 not selected </h2>
</div>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)