:empty CSS 伪类 代表没有子元素的元素。子元素只可以是元素节点或文本(包括空格)。注释或处理指令都不会产生影响。

/* Selects any <div> that contains no content */
div:empty {
  background: lime;
}

语法

:empty

例子

HTML

<div class="box"><!-- I will be lime --></div>
<div class="box">I will be pink</div>
<div class="box">
    <!-- I will be red because of the whitespace around this comment -->
</div>

CSS

.box {
    background: pink;
    height: 80px;
    width: 80px;
}

.box:empty {
    background: lime;
}

Result

规范

Specification Status Comment
Selectors Level 4
:empty
Working Draft No change.
Selectors Level 3
:empty
Recommendation Initial definition.

浏览器兼容

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
:emptyChrome Full support 1Edge Full support YesFirefox Full support 1IE Full support 9Opera Full support 9.5Safari Full support 3.1WebView Android Full support 2Chrome Android ? Edge Mobile Full support YesFirefox Android Full support 4Opera Android Full support 10Safari iOS Full support 3.1Samsung Internet Android ?

Legend

Full support  
Full support
Compatibility unknown  
Compatibility unknown

参考

文档标签和贡献者

此页面的贡献者: mdnwebdocs-bot, zhuangyin, xgqfrms-GitHub, DaPanda, FredWe
最后编辑者: mdnwebdocs-bot,