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

2018-12-31 03:18发布

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?

11条回答
只靠听说
2楼-- · 2018-12-31 03:45
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

查看更多
临风纵饮
3楼-- · 2018-12-31 03:47

+ selector is called Adjacent Sibling Selector.

For example, the selector p + p, selects the p elements immediately following the p elements

It 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:

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 one the same topic, it is worth mentioning another selector, ~ selector, which is General Sibling Selector

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

查看更多
孤独总比滥情好
4楼-- · 2018-12-31 03:49

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

查看更多
泪湿衣
5楼-- · 2018-12-31 03:50

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

查看更多
明月照影归
6楼-- · 2018-12-31 03:59

It means it matches to every p element which is immediately adjacent

www.snoopcode.com/css/examples/css-adjacent-sibling-selector

查看更多
何处买醉
7楼-- · 2018-12-31 04:02

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.

 div+ span{
   color: green;
   padding :100px;
}

     <div>The top or first element  </div>
       <span >this is span immediately after div, this will be selected</span>
       <span>This will not be selected</span>

Above style will only apply to first span after div. It is important to note that second span will not be selected.

查看更多
登录 后发表回答