If a <p>
element is immediately after a <div>
element, doesn't that mean that the <p>
element is preceded by a <div>
element?
This is correct. In other words, div + p
is a proper subset of div ~ p
— anything matched by the former is also matched by the latter, by necessity.
The difference between +
and ~
is that ~
matches all following siblings regardless of their proximity from the first element, as long as they both share the same parent.
Both of these points are most succinctly illustrated with a single example, where each rule applies a different property. Notice that the one p
that immediately follows the div
has both rules applied:
div + p {
color: red;
}
div ~ p {
background-color: yellow;
}
<section>
<div>Div</div>
<p>Paragraph</p>
<p>Paragraph</p>
<p>Paragraph</p>
</section>
<section>
No div
<p>Paragraph</p>
<p>Paragraph</p>
<p>Paragraph</p>
</section>
Anyhow, I'm looking for a selector where I can select an element that is place immediately before a given element.
Unfortunately, there isn't one yet.