:indeterminate
:indeterminate は CSS の擬似クラスセレクターで、未確定の状態にあるフォーム要素を表します。例えばチェックボックスで HTML の indeterminate 属性が true に設定されたもの、ラジオボタンでグループ内がすべて選択されていないもの、 <progress> 要素で中間の状態などです。
css
/* 未確定の状態にある <input> をすべて選択 */
input:indeterminate {
background: lime;
}
このセレクターが対象とする要素は以下の通りです。
<input type="checkbox">要素で、JavaScript によってindeterminateプロパティがtrueに設定されている場合<input type="radio">要素で、フォーム内の同じnameの値を持つすべてのラジオボタンが未選択である場合<progress>要素で、中間の状態の場合
構文
: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">
<label for="radio1">First radio label starts out lime.</label>
</div>
<div>
<input type="radio" id="radio2" name="radioButton">
<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 (let i = 0; i < inputs.length; i++) {
inputs[i].indeterminate = true;
}
結果
プログレスバー
HTML
html
<progress></progress>
CSS
css
progress {
margin: 4px;
}
progress:indeterminate {
width:80vw;
height:20px;
}
結果
仕様書
| Specification |
|---|
| HTML Standard # selector-indeterminate |
| Selectors Level 4 # indeterminate |
ブラウザーの互換性
BCD tables only load in the browser
関連情報
- ウェブフォーム — ユーザーデータでの作業
- ウェブフォームの整形
<input type="checkbox">要素のindeterminate属性<input>およびそれを実装しているHTMLInputElementインターフェイス:checkedセレクターは、チェックボックスがチェックされているかどうかでスタイル付けすることができます