CSS :indeterminate 擬似クラス
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年7月.
:indeterminate は CSS の擬似クラスセレクターで、未確定の状態にあるフォーム要素を表します。例えばチェックボックスで indeterminate 状態に設定されたもの、ラジオボタンでグループ内がすべて選択されていないもの、<progress> 要素で中間の状態などです。
css
/* 未確定の状態にある <input> をすべて選択 */
input:indeterminate {
background: lime;
}
このセレクターが対象とする要素は以下の通りです。
<input type="checkbox">要素で、indeterminateプロパティがtrueに設定されている場合<input type="radio">要素で、フォーム内の同じnameの値を持つすべてのラジオボタンがcheckedでない場合<progress>要素にvalueがなく、未確定の状態のもの
構文
css
:indeterminate {
/* ... */
}
例
>チェックボックスとラジオボタン
この例では中間の状態にあるフォームの要素に特殊なスタイルを適用します。
HTML
html
<fieldset>
<legend>チェックボックス</legend>
<div>
<input type="checkbox" id="checkbox" />
<label for="checkbox">このチェックボックスのラベルは、ライム色で始まります。</label>
</div>
</fieldset>
<fieldset>
<legend>ラジオボタン</legend>
<div>
<input type="radio" id="radio1" name="radioButton" value="val1" />
<label for="radio1">1 つ目のラジオボタンで、ライム色で始まります。</label>
</div>
<div>
<input type="radio" id="radio2" name="radioButton" value="val2" />
<label for="radio2">2 つ目のラジオボタンで、ライム色で始まります。</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;
}
結果
仕様書
| Specification |
|---|
| HTML> # selector-indeterminate> |
| Selectors Level 4> # indeterminate-pseudo> |
ブラウザーの互換性
関連情報
- ウェブフォーム — ユーザーデータでの作業
- ウェブフォームの整形
<input type="checkbox">要素のindeterminate属性<input>およびそれを実装しているHTMLInputElementインターフェイス:checkedセレクターは、チェックボックスがチェックされているかどうかでスタイル付けすることができます