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
?
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
?
final output look like this
+
selector is calledAdjacent Sibling Selector
.For example, the selector
p + p
, selects thep
elements immediately following thep
elementsIt can be thought of as a
looking outside
selector which checks for the immediately following element.Here is a sample snippet to make things more clear:
Since we are one the same topic, it is worth mentioning another selector,
~
selector, which isGeneral Sibling Selector
For example,
p ~ p
selects all thep
which follows thep
doesn't matter where it is, but bothp
should be having the same parent.Here is how it looks like with the same markup:
Notice that the last
p
is also matched in this sample.+
presents one of the relative selectors. List of all relative selectors:div p
- All<p>
elements inside<div>
elements are selected.div > p
- All<p>
elements whose direct parent is<div>
are selected. It works backward too (p < div
)div + p
- All<p>
elements places immediately after<div>
element are selected.div ~ p
- All<p>
elements that are preceded by a<div>
element are selected.More about selectors check here.
"+" is the adjacent sibling selector. It will select any p DIRECTLY AFTER a p (not a child or parent though, a sibling).
It means it matches to every
p
element which is immediately adjacentwww.snoopcode.com/css/examples/css-adjacent-sibling-selector
The Plus (+) will select the first immediate element. When you use + selector you have to give two parameters. This will be more clear by example: here div and span are parameters, so in this case only first span after the div will be styled.
Above style will only apply to first span after div. It is important to note that second span will not be selected.