相邻兄弟选择器 (+) 介于两个选择器之间,当第二个元素紧跟在第一个元素之后,并且两个元素都是属于同一个父元素的子元素,则第二个元素将被选中。

/* 图片后面紧跟着的段落将被选中 */
img + p {
  font-style: bold;
}

语法

前方元素 + 目标元素 {样式声明 }

示例

CSS

li:first-of-type + li {
  color: red;
}

HTML

<ul>
  <li>One</li>
  <li>Two!</li>
  <li>Three</li>
</ul>

结果

  • One
  • Two!
  • There

规范

规范 状态 备注
Selectors Level 4
next-sibling combinator
Working Draft 重命名为: "next-sibling" combinator。
Selectors Level 3
Adjacent sibling combinator
Recommendation  
CSS Level 2 (Revision 1)
Adjacent sibling selectors
Recommendation 初始定义。

浏览器兼容性

FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
Basic support112171 2 Yes Yes
FeatureAndroid webviewChrome for AndroidEdge mobileFirefox for AndroidOpera AndroidiOS SafariSamsung Internet
Basic support Yes Yes Yes4 Yes Yes Yes

1. Internet Explorer 7 doesn't update the style correctly when an element is dynamically placed before an element that matched the selector.

2. In Internet Explorer 8, if an element is inserted dynamically by clicking on a link the first-child style isn't applied until the link loses focus.

参阅

文档标签和贡献者

标签: 
此页面的贡献者: zhangqiangoffice, Ricardo-Ke, fscholz, teoli, alimon
最后编辑者: zhangqiangoffice,