814

For example:

p + p {
  /* Some declarations */
}

I don't know what the + means. What's the difference between this and just defining a style for p without + p?

1
  • 1
    In practice this is mostly useful to apply a margin or padding between list elements of the same kind, thus no special case required for the first or last element. Dec 13, 2019 at 16:48

9 Answers 9

831

See adjacent selectors on W3.org.

In this case, the selector means that the style applies only to paragraphs directly following another paragraph.

A plain p selector would apply the style to every paragraph in the page.


This will only work on IE7 or above. In IE6, the style will not be applied to any elements. This also goes for the > combinator, by the way.

See also Microsoft's overview for CSS compatibility in Internet Explorer.

3
  • I found it useful to not collapse the element when hidden. Therefore a more appropriate way to hide it is by using visibility : hidden/visible instead of display : none/block. See this reference.
    – KFL
    Aug 24, 2014 at 5:40
  • 14
    what will be the difference between p + p and p > p Nov 28, 2016 at 17:30
  • 15
    @MuhammadRizwan p > p means a nested p, e.i. any p that is directly below another p, such as <p><p>This paragraph</p></p>.
    – dwitvliet
    Jul 30, 2017 at 5:49
231

It's the Adjacent sibling selector.

From Splash of Style blog.

To define a CSS adjacent selector, the plus sign is used.

h1+p {color:blue;}

The above CSS code will format the first paragraph after (not inside) any h1 headings as blue.

h1>p selects any p element that is a direct (first generation) child (inside) of an h1 element.

  • h1>p matches <h1> <p></p> </h1> (<p> inside <h1>)

h1+p will select the first p element that is a sibling (at the same level of the dom) as an h1 element.

  • h1+p matches <h1></h1> <p><p/> (<p> next to/after <h1>)
5
  • 3
    I'm confused between plus sign and greater sign. If I use h1>p instead of h1+p, does it give me the same result? Could you explain a little bit how different between them?
    – lvarayut
    May 13, 2014 at 13:56
  • 94
    In your examples, h1>p selects any p element that is a direct (first generation) child of an h1 element. h1+p will select the first p element that is a sibling (at the same level of the dom) as an h1 element. h1>p matches <h1><p><p></h1>, h1+p matches <h1></h1><p><p/> May 13, 2014 at 15:20
  • 1
    @MatthewVines u should add that h1>p and h1+p to your answer Jun 27, 2016 at 20:41
  • So essentially in your example it will match the first <p> after <h1>, but would it also match that same <p> if it came before <h1>? Or is it only after?
    – Vincent
    Jan 2, 2019 at 20:43
  • 2
    <p> is invalid as a child of <h1> but point understood.
    – ggorlen
    Jul 16, 2020 at 15:26
66

The + sign means select an "adjacent sibling"

For example, this style will apply from the second <p>:

p + p {
   font-weight: bold;
} 
<div>
   <p>Paragraph 1</p>
   <p>Paragraph 2</p>
</div>


Example

See this JSFiddle and you will understand it: http://jsfiddle.net/7c05m7tv/ (Another JSFiddle: http://jsfiddle.net/7c05m7tv/70/)


Browser Support

Adjacent sibling selectors are supported in all modern browsers.


Learn more

0
45

"+" is the adjacent sibling selector. It will select any p DIRECTLY AFTER a p (not a child or parent though, a sibling).

32

The + combinator is called the Adjacent sibling combinator / Next-sibling combinator.

For example, the combination of p + p selectors, selects the p elements immediately following the p elements

It can be thought of as a "looking alongside" combination that checks for the immediately following element.

Here is a sample snippet to make things more clear:

body {
  font-family: Tahoma;
  font-size: 12px;
}
p + p {
  margin-left: 10px;
}
<div>
  <p>Header paragraph</p>
  <p>This is a paragraph</p>
  <p>This is another paragraph</p>
  <p>This is yet another paragraph</p>
  <hr>
  <p>Footer paragraph</p>
</div>

Since we are on the same topic, it is worth mentioning another combinator, ~, which is the General sibling combinator / Subsequent-sibling combinator

For example, p ~ p selects all the p which follows the p doesn't matter where it is, but both p should be having the same parent.

Here is what it looks like with the same markup:

body {
  font-family: Tahoma;
  font-size: 12px;
}
p ~ p {
  margin-left: 10px;
}
<div>
  <p>Header paragraph</p>
  <p>This is a paragraph</p>
  <p>This is another paragraph</p>
  <p>This is yet another paragraph</p>
  <hr>
  <p>Footer paragraph</p>
</div>

Notice that the last p is also matched in this sample.

0
30

The + selector targets the one element after. On a similar note, the ~ selector targets all the elements after. Here's a diagram, if you're confused:

enter image description here

2
  • So what about #chicken > box?
    – user14341744
    Nov 18, 2020 at 22:48
  • 5
    Well then @HaveProblemsEveryday if the chicken ate a box, it would target the box that would have been eaten by the chicken. Nov 19, 2020 at 7:53
23

+ presents one of the relative selectors. Here is a list of all relative selectors:

div p - All <p> elements inside of a <div> element are selected.

div > p - All <p> elements whose direct parent is <div> are selected. It works backwards too (p < div)

div + p - All <p> elements placed immediately after a <div> element are selected.

div ~ p - All <p> elements that are preceded by a <div> element are selected.

Here is some more about selectors.

1
  • 1
    The last selector is mistaken. According to MDN: The general sibling combinator (~) separates two selectors and matches the second element only if it follows the first element (though not necessarily immediately), and both are children of the same parent element Jul 8, 2019 at 2:22
12

It would match any element p that's immediately adjacent to an element 'p'. See: http://www.w3.org/TR/CSS2/selector.html

2
p+p{
//styling the code
}

p+p{
} simply mean find all the adjacent/sibling paragraphs with respect to first paragraph in DOM body.

    <div>
    <input type="text" placeholder="something">
    <p>This is first paragraph</p>
    <button>Button </button>
    <p> This is second paragraph</p>
    <p>This is third paragraph</p>
    </div>

    Styling part 
    <style type="text/css">
        p+p{
            color: red;
            font-weight: bolder;
        }
    </style>

   It will style all sibling paragraph with red color.

final output look like this

enter image description here

Not the answer you're looking for? Browse other questions tagged or ask your own question.