CSS中的上一个相邻选择器

时间:2013-12-17 01:47:29

标签: css

CSS中是否有方法定位p之前的每个div.carrots?在这种情况下,我们会得到 Munch 但不会 Burp

<p>Munch Munch!</p>
<div class="carrots">
</div>

<p>Burp!</p>
<div class="potatoes">
</div>

1 个答案:

答案 0 :(得分:0)

没有“上一个兄弟”选择器。只有adjacent and general combinators,要求目标兄弟在之后

为了在这种情况下选择<p>,您可以使用某个元素(例如.container并使用.container p:first-child作为选择器来包围内容。我认为事情不会如果你这样做,那就完全不同了:

<div class=carrots>
    <p>Munch Munch!</p>
    <div>
    </div>
</div>