什么是语义正确的图像 HTML (HTML 5) 标记,可以选择或取消选择并且应该在表单中表现为复选框。从语义上讲,图像不仅仅是复选框的样式,而是被选中的实际对象。
最佳答案
我会将图像和复选框都放在 label
元素中:
<label>
<input type="checkbox" />
<img src="//cdn.sstatic.net/stackoverflow/img/favicon.ico" />
</label>
通过这种方式,您可以将图像与复选框相关联,激活图像将切换复选框的状态。
您还可以隐藏复选框并使用 :selected + img
根据选中情况设置图像样式。
label {
float: left;
clear: left;
}
label > input {
opacity: 0;
}
label > img {
opacity: .4;
cursor: pointer;
}
label > :checked + img {
opacity: 1;
}
<label>
<input type="checkbox" />
<img src="//cdn.sstatic.net/stackoverflow/img/favicon.ico" />
</label>
<label>
<input type="checkbox" />
<img src="//cdn.sstatic.net/stackoverflow/img/favicon.ico" />
</label>
<label>
<input type="checkbox" />
<img src="//cdn.sstatic.net/stackoverflow/img/favicon.ico" />
</label>
<label>
<input type="checkbox" />
<img src="//cdn.sstatic.net/stackoverflow/img/favicon.ico" />
</label>
关于css - 图像复选框的语义正确 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33765202/