はい、次の HTML を考えると:
<input type="checkbox" id="test" /><label for="test">Click to check</label>
そしてCSS:
label {
color: #f00;
}
input {
display: none;
}
input[type=checkbox]:checked + label {
color: #0f0;
}
label {
color: #f00;
}
input {
display: none;
}
input[type=checkbox]:checked + label {
color: #0f0;
}
<input type="checkbox" id="test" checked/>
<label for="test">Click to check</label>
JS フィドルのデモ。
:checkedもちろん、これは疑似クラスを実装するブラウザーに依存します。
チェック済みの にlabel続く要素を指定するために attribute-equals 表記を使用しましたが、選択できるのはorのみであるため、その特定性は安全に省略できます。inputtype="checkbox"checkboxradio
::afterまた、準拠ブラウザでは、疑似要素とそのプロパティと連携して同じアプローチを使用しcontent、チェックボックスの状態を反映するようにテキストを変更することもできます。たとえば、次の HTML を使用します。
<input type="checkbox" id="test" /><label for="test">Click to</label>
そしてCSS:
label {
color: #f00;
}
input {
display: none;
}
input[type=checkbox]:checked + label {
color: #0f0;
}
input[type=checkbox] + label::after {
content: ' check';
}
input[type=checkbox]:checked + label::after {
content: ' uncheck';
}
label {
color: #f00;
}
input {
display: none;
}
input[type=checkbox]:checked + label {
color: #0f0;
}
input[type=checkbox] + label::after {
content: ' check';
}
input[type=checkbox]:checked + label::after {
content: ' uncheck';
}
<input type="checkbox" id="test" checked/>
<label for="test">Click to</label>
参考文献: