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?

Salman A
  • 262,204
  • 82
  • 430
  • 521
gday
  • 8,281
  • 4
  • 19
  • 7
  • 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. – Christophe Roussy Dec 13 '19 at 16:48

9 Answers9

827

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.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Thorarin
  • 47,289
  • 11
  • 75
  • 111
  • 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](http://www.w3schools.com/css/css_display_visibility.asp). – KFL Aug 24 '14 at 05:40
  • 14
    what will be the difference between p + p and p > p – Muhammad Rizwan Nov 28 '16 at 17:30
  • 15
    @MuhammadRizwan `p > p` means a nested `p`, e.i. any `p` that is directly below another `p`, such as `

    This paragraph

    `.
    – dwitvliet Jul 30 '17 at 05: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>)
Community
  • 1
  • 1
Matthew Vines
  • 27,253
  • 7
  • 76
  • 97
  • 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 '14 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` matches `

    `
    – Matthew Vines May 13 '14 at 15:20
  • 1
    @MatthewVines u should add that h1>p and h1+p to your answer – Furkan Gözükara Jun 27 '16 at 20:41
  • So essentially in your example it will match the first

    after

    , but would it also match that same

    if it came before

    ? Or is it only after?

    – Vincent Jan 02 '19 at 20:43
  • 2
    `

    ` is [invalid](https://html.spec.whatwg.org/multipage/dom.html#flow-content-2) as a child of `

    ` but point understood.

    – ggorlen Jul 16 '20 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

Community
  • 1
  • 1
Cas Bloem
  • 4,846
  • 2
  • 24
  • 23
45

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

Gordon Gustafson
  • 40,133
  • 25
  • 115
  • 157
31

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.

TylerH
  • 20,799
  • 66
  • 75
  • 101
L J
  • 5,249
  • 3
  • 19
  • 29
29

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

corn on the cob
  • 2,093
  • 3
  • 18
  • 29
22

+ 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.

corn on the cob
  • 2,093
  • 3
  • 18
  • 29
Nesha Zoric
  • 6,218
  • 42
  • 34
  • 1
    The last selector is mistaken. [According to MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/General_sibling_combinator): 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 – Neithan Max Jul 08 '19 at 02: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

Philip Kirkbride
  • 21,381
  • 38
  • 125
  • 225
Mara Morton
  • 4,429
  • 1
  • 21
  • 12
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