输入的任何样式都会影响每个输入元素。是否有一种方法可以指定样式只应用于复选框,而不对每个复选框元素应用类?
当前回答
使用css2,你可以这样做:
input[type='checkbox'] { ... }
到目前为止,这应该得到了相当广泛的支持。参见浏览器支持
其他回答
感谢所有对这篇文章做出贡献的人。 看看我使用纯CSS完成了什么。
input[type=checkbox] { position: absolute; cursor: pointer; width: 0px; height: 0px; } input[type=checkbox]:checked:before { content: ""; display: block; position: absolute; width: 34px; height: 34px; border: 4px solid #FFCB9A; border-radius: 20px; background-color: #445768; transition: all 0.2s linear; } input[type=checkbox]:before { content: ""; display: block; position: absolute; width: 34px; height: 34px; border: 4px solid #FFCB9A; border-radius: 3px; background-color: #445768; } input[type=checkbox]:after { content: ""; display: block; width: 0px; height: 0px; border: solid #FFCB9A; border-width: 0 0px 0px 0; -webkit-transform: rotate(180deg); -ms-transform: rotate(180deg); transform: rotate(180deg); position: absolute; top: 0px; left: 50px; transition: all 0.2s linear; } input[type=checkbox]:checked:after { content: ""; display: block; width: 12px; height: 21px; border: solid #FFCB9A; border-width: 0 5px 5px 0; -webkit-transform: rotate(45deg); -ms-transform: rotate(45deg); transform: rotate(45deg); position: absolute; top: 2px; left: 14px; } <input type="checkbox">
总结
因此,下面控制实际的复选框,我们通过添加h:0和w:0来隐藏它。
input[type=checkbox] {...}
其余部分控制虚拟复选框。
当勾选时,它控制虚拟复选框的背景。
input[type=checkbox]:checked:before {...}
当未选中复选框时,这将控制虚拟复选框的背景。
input[type=checkbox]:before {...}
当未选中复选框时,这将控制虚拟复选框的前台。
input[type=checkbox]:after {...}
当勾选时,它控制虚拟复选框的前台。
input[type=checkbox]:checked:after {...}
我真的希望它能像帮助我一样帮助你:)
虽然CSS确实为您提供了一种方法来执行特定于复选框类型或其他类型的样式,但不支持这种样式的浏览器将会出现问题。
我认为在这种情况下,您唯一的选择是将类应用到复选框中。
只需将class="checkbox"添加到您的复选框中。
然后在css代码中创建该样式。
你可以这样做:
main.css
input[type="checkbox"] { /* css code here */ }
ie.css
.checkbox{ /* css code here for ie */ }
然后使用IE特定的css包括:
<!--[if lt IE 7]>
<link rel="stylesheet" type="text/css" href="ie.css" />
<![endif]-->
您仍然需要添加类,以便它在IE中工作,并且它不能在其他不支持IE的非IE浏览器中工作。但是它会使你的网站具有前瞻性的css代码,随着IE得到支持,你将能够从复选框中删除IE特定的css代码和css类。
Trident为复选框和单选按钮控件提供了::-ms-check伪元素。例如:
<input type="checkbox">
<input type="radio">
::-ms-check {
color: red;
background: black;
padding: 1em;
}
Windows 8操作系统的IE10界面显示如下:
我创造自己的解决方案,没有标签
input[type=checkbox] { position: relative; cursor: pointer; } input[type=checkbox]:before { content: ""; display: block; position: absolute; width: 16px; height: 16px; top: 0; left: 0; border: 2px solid #555555; border-radius: 3px; background-color: white; } input[type=checkbox]:checked:after { content: ""; display: block; width: 5px; height: 10px; border: solid black; border-width: 0 2px 2px 0; -webkit-transform: rotate(45deg); -ms-transform: rotate(45deg); transform: rotate(45deg); position: absolute; top: 2px; left: 6px; } <input type="checkbox" name="a"> <input type="checkbox" name="a"> <input type="checkbox" name="a"> <input type="checkbox" name="a">
input[type=checkbox] { position: relative; cursor: pointer; } input[type=checkbox]:before { content: ""; display: block; position: absolute; width: 20px; height: 20px; top: 0; left: 0; background-color:#e9e9e9; } input[type=checkbox]:checked:before { content: ""; display: block; position: absolute; width: 20px; height: 20px; top: 0; left: 0; background-color:#1E80EF; } input[type=checkbox]:checked:after { content: ""; display: block; width: 5px; height: 10px; border: solid white; border-width: 0 2px 2px 0; -webkit-transform: rotate(45deg); -ms-transform: rotate(45deg); transform: rotate(45deg); position: absolute; top: 2px; left: 6px; } <input type="checkbox" name="a"> <input type="checkbox" name="a"> <input type="checkbox" name="a"> <input type="checkbox" name="a">
课程也很好用,比如:
<style>
form input .checkbox
{
/* your checkbox styling */
}
</style>
<form>
<input class="checkbox" type="checkbox" />
</form>