:indeterminate

Baseline 已广泛支持

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 ⁨2015 年 7 月⁩以来,各浏览器均已提供此特性。

:indeterminate CSS 伪类表示任何处于不确定状态的表单元素,例如通过 JavaScript 设置为 indeterminate 状态的复选框,作为组内成员但所有单选按钮都未选中的单选按钮,以及没有 value 属性的 <progress> 元素。

css
/* Selects any <input> whose state is indeterminate */
input:indeterminate {
  background: lime;
}

此选择器定位的元素包括:

语法

css
:indeterminate {
  /* ... */
}

示例

复选框和单选按钮

此示例为与不确定表单字段关联的标签应用特殊样式。

HTML

html
<fieldset>
  <legend>Checkbox</legend>
  <div>
    <input type="checkbox" id="checkbox" />
    <label for="checkbox">This checkbox label starts out lime.</label>
  </div>
</fieldset>

<fieldset>
  <legend>Radio</legend>
  <div>
    <input type="radio" id="radio1" name="radioButton" value="val1" />
    <label for="radio1">First radio label starts out lime.</label>
  </div>
  <div>
    <input type="radio" id="radio2" name="radioButton" value="val2" />
    <label for="radio2">Second radio label also starts out lime.</label>
  </div>
</fieldset>

CSS

css
input:indeterminate + label {
  background: lime;
}

JavaScript

js
const inputs = document.getElementsByTagName("input");

for (const input of inputs) {
  input.indeterminate = true;
}

结果

进度条

HTML

html
<progress></progress>

CSS

css
progress {
  margin: 4px;
}

progress:indeterminate {
  width: 80vw;
  height: 20px;
}

结果

规范

规范
HTML
# 选择器-不确定
选择器 Level 4
# 不确定伪类

浏览器兼容性

另见