The way that w3schools phrases it, they sound the same.
W3Schools' CSS reference
div + p
Selects all<p>
elements that are placed immediately after<div>
elements
div ~ p
Selects every<p>
element that are preceded by a<div>
element
If a <p>
element is immediately after a <div>
element, doesn't that mean that the <p>
element is preceded by a <div>
element?
Anyhow, I'm looking for a selector where I can select an element that is place immediately before a given element.
To clarify: div>p will only select a p that is the direct child of the div (that is, directly inside the div). On the other hand, div p will select any p inside the div, even if it is buried inside other elements. E.g.
2 Answers. Show activity on this post. + will only select the first element that is immediately preceded by the former selector. ~ selector all the sibling preceded by the former selector.
In CSS, the symbol tilde(~) is know as Subsequent-sibling Combinator (also known as tilde or squiggle or twiddle or general-sibling selector). As the name suggests it is made of the “tilde” (U+007E, ~) character that separates two sequences of simple selectors.
Adjacent sibling selectors X + Y
Adjacent sibling selectors have the following syntax: E1 + E2, where E2 is the subject of the selector. The selector matches if E1 and E2 share the same parent in the document tree and E1 immediately precedes E2, ignoring non-element nodes (such as text nodes and comments).
ul + p { color: red; }
In this example it will select only the element that is immediately preceded by the former element. In this case, only the first paragraph after each ul will have red text.
ul + p { color: red; }
<div id="container"> <ul> <li>List Item</li> <li>List Item</li> <li>List Item</li> <li>List Item</li> </ul> <p>This will be red</p> <p>This will be black</p> <p>This will be black</p> </div>
General sibling selectors X ~ Y
The ~ combinator separates two selectors and matches the second element only if it is preceded by the first, and both share a common parent.
ul ~ p { color: red; }
This sibling combinator is similar to X + Y, however, it's less strict. While an adjacent selector (ul + p) will only select the first element that is immediately preceded by the former selector, this one is more generalized. It will select, referring to our example above, any p elements, as long as they follow a ul.
ul ~ p { color: red; }
<div id="container"> <ul> <li>List Item <ul> <li>Child</li> </ul> </li> <li>List Item</li> <li>List Item</li> <li>List Item</li> </ul> <p>This will be red.</p> <p>This will be red.</p> <p>This will be red.</p> <p>This will be red.</p> </div>
Source
code.tutsplus
General sibling selectors MDN
Adjacent sibling selectors w3
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With