100

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.

BoltClock
  • 665,005
  • 155
  • 1,345
  • 1,328
user4055428
  • 1,087
  • 2
  • 7
  • 5
  • 6
    `+` is the immediate next `p` element and `~` is **all** of the next `p` elements (after each `div` element) – vsync Oct 09 '14 at 15:34
  • 2
    you cannot do `before`. impossible. only with javascript. you'll have to look for it and it all the `before` ones a class to target in your CSS. – vsync Oct 09 '14 at 15:36
  • 9
    Also try to avoid w3schools :) http://www.w3fools.com/ – Alex Char Oct 09 '14 at 15:41
  • 3
    See [this](http://techbrij.com/css-selector-adjacent-child-sibling) for a visual explanation – Marc B Oct 09 '14 at 15:42
  • 1
    @AlexChar check the current state of the w3fools site :) – Pointy Oct 09 '14 at 15:49
  • 2
    One interesting thing about this question is that it had not been asked before. At least I couldn't find any similar question on SO. – Hashem Qolami Oct 10 '14 at 15:29
  • Glad someone else had this question. I read this on W3Schools like 5 times just thinking I had drank too much night before, as it sounded the same to me also. :) – CodeFinity Aug 18 '17 at 13:23

4 Answers4

134

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

Alex Char
  • 32,295
  • 8
  • 49
  • 69
  • 10
    This answer is nothing more than a quotation of another article, with no attempt to address the specific question at hand. The examples given don't do a very good job of addressing the question either. – BoltClock Feb 26 '15 at 10:54
  • 1
    @BoltClock thanks i get your point. I always welcome feedback from you. But i think my answer explain to OP what is the difference between those selectors. – Alex Char Feb 26 '15 at 10:57
  • That's true. Although it doesn't quite address the question, it does a reasonable job of illustrating the difference between the two selectors. – BoltClock Feb 26 '15 at 10:59
29

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.

Community
  • 1
  • 1
BoltClock
  • 665,005
  • 155
  • 1,345
  • 1,328
  • @user4055428 "~ matches all following siblings regardless of their proximity from the first element" This explains the real difference, this should be the accepted answer. – Umur Karagöz Nov 27 '21 at 16:14
9

consider this example:

p + p { /* the first p immediately after a preceding p */
   color: red; 
} 

p ~ p { /* all p's after a preceding p */
   font-weight: bold;
} 
<div>
    <p>1</p>
 <div>separator</div>
    <p>2</p> <!-- only ~ is applied here -->
   <p>3</p> <!-- both + and ~ are applied here -->
</div>
Reece
  • 737
  • 4
  • 20
  • 38
Mina Luke
  • 1,806
  • 18
  • 18
1

1) Adjacent Sibling Selectors (S1 + S2)

Adjacent sibling selector is used to select a specified element which is immediate next to another specified element. Both elements should be in the same level.

div + p {
    color:red;
}

Adjacent Sibling Selectors example

2) General Sibling Selectors (S1 ~ S2)

General sibling selector is used to select all specified sibling elements of another specified element.

div ~ p {
   color:red;
}

General Sibling Selectors example

Adjacent Sibling(S1 + S2) vs General Sibling(S1 ~ S2) selectors:

Adjacent sibling(S1 + S2) selector selects immediate sibling element only but general sibling(S1 ~ S2) selector selects all sibling elements of another specified element. Both cases, both elements(S1 and S2) should be in the same level.

Remaining selectors are explained here: https://www.csssolid.com/35-css-selectors-to-remember.html

Selva
  • 29
  • 2