:empty
Baseline Widely available *
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
* Some parts of this feature may have varying levels of support.
尝试一下
div:empty {
outline: 2px solid deeppink;
height: 1em;
}
Element with no content:
Element with comment:
Element with nested empty element:
备注:
在选择器 Level 4 中,:empty
伪类被更改为与 :-moz-only-whitespace
类似一样的行为,但目前还没有浏览器支持它。
语法
css
:empty {
/* ... */
}
示例
HTML
html
我将会是粉色。
CSS
css
.box {
background: pink;
height: 80px;
width: 80px;
}
.box:empty {
background: lime;
}
结果
无障碍考虑
规范
Specification |
---|
Selectors Level 4 # the-empty-pseudo |
浏览器兼容性
参见
:-moz-only-whitespace
非标准 是选择器 Level 4 更改的前缀实现。:blank
实验性