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? 1...
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?
9 Answers
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.
It's the Adjacent sibling selector.
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>pmatches<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+pmatches<h1></h1><p><p/>(<p>next to/after<h1>)
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>
Must Read
Example
See this JSFiddle and you will understand it: (Another JSFiddle: )