:empty

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

浏览器兼容

BCD tables only load in the browser

参考

example

https://codepen.io/xgqfrms/full/zQEJWw