Is there are way in CSS to target every p
preceding a div.carrots
? In this case we would get Munch but not Burp.
<p>Munch Munch!</p>
<div class="carrots">
</div>
<p>Burp!</p>
<div class="potatoes">
</div>
Is there are way in CSS to target every p
preceding a div.carrots
? In this case we would get Munch but not Burp.
<p>Munch Munch!</p>
<div class="carrots">
</div>
<p>Burp!</p>
<div class="potatoes">
</div>
There is no "previous sibling" selector. Only The adjacent and general combinators, which require the target sibling to be after the first.
In order to select <p>
in this case, you can surround the contents with some element (say, .container
and use .container p:first-child
as the selector. I don't think things would be all that different if you did this either:
<div class=carrots>
<p>Munch Munch!</p>
<div>
</div>
</div>