この質問で説明されているように、チェックボックスにカスタム画像を使用しようとしています:
私のチェックボックスはグリッドに表示されるので、ラベルを関連付ける必要はありません。ただし、ラベルが存在しない場合、その質問で説明されている手法は機能しないことがわかりました。
たとえば、関連する CSS は次のとおりです。
input[type=checkbox] {
opacity: 0;
}
input[type=checkbox] {
height: 17px;
width: 17px;
background: url('/resources/img/checkBox-unchecked.png') 0 0px no-repeat;
}
input[type=checkbox]:checked {
background: url('/resources/img/checkBox-checked.png') 0 0px no-repeat;
height: 17px;
width: 17px;
padding: 0 0 0 0px;
}
シンプルに宣言input
しても、画像はレンダリングされません。
<input type="checkbox">
ただし、ソリューションで説明されているように、css を変更して+ label
を追加し、マークアップにラベルを追加すると、次のように機能します。
input[type=checkbox] {
opacity: 0;
}
input[type=checkbox] + label {
height: 17px;
width: 17px;
background: url('/resources/img/checkBox-unchecked.png') 0 0px no-repeat;
}
input[type=checkbox]:checked + label {
background: url('/resources/img/checkBox-checked.png') 0 0px no-repeat;
height: 17px;
width: 17px;
padding: 0 0 0 0px;
}
<input type="checkbox"><label></label>
これは期待どおりにレンダリングされます。
最初の解決策が機能しないのはなぜですか?
不要なラベルを追加せずにこれを達成するにはどうすればよいですか?