--- title: ':empty' slug: 'Web/CSS/:empty' tags: - ':empty' - CSS - CSS 骨架效果 - CSS伪类 - Layout - css skeleton - skelenton - 参考 - 布局 - 网络 translation_of: 'Web/CSS/:empty' ---
:empty
CSS 伪类 代表没有子元素的元素。子元素只可以是元素节点或文本(包括空格)。注释或处理指令都不会产生影响。
/* Selects any <div> that contains no content */ div:empty { background: lime; }
{{csssyntax}}
<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>
body { display: flex; justify-content: space-around; }
.box { background: pink; height: 80px; width: 80px; } .box:empty { background: lime; }
{{EmbedLiveSample('Examples', 300, 80)}}
Specification | Status | Comment |
---|---|---|
{{ SpecName('CSS4 Selectors', '#empty-pseudo', ':empty') }} | {{ Spec2('CSS4 Selectors') }} | No change. |
{{ SpecName('CSS3 Selectors', '#empty-pseudo', ':empty') }} | {{ Spec2('CSS3 Selectors') }} | Initial definition. |
{{Compat("css.selectors.empty")}}
https://codepen.io/xgqfrms/full/zQEJWw