为一个文件:CSS一般兄弟选择特异性
<h1>Section 1</h1>
<p>I came after a h1 and should be red.</p>
<p>I came after a h1 and should be red.</p>
<h2>Section 2</h2>
<p>I came after a h2 and should be green.</p>
<p>I came after a h2 and should be green.</p>
<h1>Section 3</h1>
<p>I should be the same color as the section one text?</p>
<p>I should be the same color as the section one text?</p>
我试过的风格是:
h1 ~ p {
color: red;
}
h2 ~ p {
color: green;
}
http://jsfiddle.net/4ks7j938/7/
我预计,第1和第3将具有相同的风格,第三段与更具体的h1 ~ p
选择器匹配,因为h1
比更接近兄弟10。但是,在我的测试中,结果是第2段和第3段的样式相同。
两个问题:
请问CSS选择器的规格其实某处指定此行为? css spec on the General sibling selector似乎可以在这里解释。
我怎样才能达到第1段和第3段的样式相同的预期结果?我不能添加类或任何属性的HTML,我只能控制CSS。
关于[计算选择特异性](http://www.w3.org/TR/css3-selectors/#specificity)一节从未提及组合器,所以假设或甚至推断组合器对于选择特异性。 – BoltClock 2014-12-11 05:31:11
至于达到预期的结果,Musa是正确的:在给定文档结构的情况下,您将无法使用纯CSS完成此操作。类似问题:http://stackoverflow.com/questions/10427998/targeting-more-than-one-following-element-with-elementelement http://stackoverflow.com/questions/13330757/target-all-general-siblings-相同类型之前的下一个出现 – BoltClock 2014-12-11 05:32:25