ChatGPT解决这个技术问题 Extra ChatGPT

What does the "+" (plus sign) CSS selector mean?

This question's answers are a community effort. Edit existing answers to improve this post. It is not currently accepting new answers or interactions.

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?

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.

T
TylerH

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.


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.
what will be the difference between p + p and p > p
@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>.
C
Community

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

(

inside

)

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

(

next to/after

)


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?
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/>
@MatthewVines u should add that h1>p and h1+p to your answer
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?

<p> is invalid as a child of <h1> but point understood.

C
Community

The + sign means select an "adjacent sibling"

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

p + p { font-weight: bold; }

Paragraph 1

Paragraph 2

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

http://css-tricks.com/almanac/selectors/a/adjacent-sibling/

http://www.w3.org/TR/CSS2/selector.html#adjacent-selectors

https://developer.mozilla.org/en-US/docs/Web/CSS/Adjacent_sibling_selectors


G
Gordon Gustafson

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


T
TylerH

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; }

Header paragraph

This is a paragraph

This is another paragraph

This is yet another paragraph


Footer paragraph

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; }

Header paragraph

This is a paragraph

This is another paragraph

This is yet another paragraph


Footer paragraph

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


c
corn on the cob

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:

https://i.stack.imgur.com/kdXBN.png


So what about #chicken > box?
Well then @HaveProblemsEveryday if the chicken ate a box, it would target the box that would have been eaten by the chicken.
c
corn on the cob

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


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
P
Philip Kirkbride

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


R
Rajesh Prasad Yadav
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

https://i.stack.imgur.com/hjmgu.png