CSS

:empty

:empty

:empty伪类代表没有子节点的所有元素。这里只考虑元素节点和文本(包括空格)。注释或处理指令不影响元素是否被认为是空的。

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

语法

:empty

实例

body { display: flex; justify-content: space-around; } .box { background: red; height: 100px; width: 100px; } .box:empty { background: lime; }

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

规范

SpecificationStatusComment
Selectors Level 4The definition of ':empty' in that specification.Working DraftNo change
Selectors Level 3The definition of ':empty' in that specification.RecommendationInitial definition

浏览器兼容性

FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support1.0(Yes)1.0 (1.7 or earlier)9.09.53.1

FeatureAndroidEdgeFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support2.1(Yes)1.0 (1)9.510.03.1