What does the "+" (plus sign) CSS selector mean?
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
?
Solution 1:
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.
Solution 2:
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>
)