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

/* 图片后面紧跟着的段落将被选中 */
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 初始定义。

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
Basic supportChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 7
Notes
Full support 7
Notes
Notes Internet Explorer 7 doesn't update the style correctly when an element is dynamically placed before an element that matched the selector.
Notes 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.
Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes

Legend

Full support  
Full support
See implementation notes.
See implementation notes.

参阅

文档标签和贡献者

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