# CSS美化单选框 radio 、多选框 checkbox 和 switch开关按钮

返回:一般常见样式处理及开发技巧

radio、checkbox和switch应该是一个比较常用的html标签,尤其是在中后台ERP系统里面更为常见。不过浏览器自带的样式不怎么好看,而且不同浏览器效果也不一样。出于美化和统一视觉效果的需求,自定义样式就被提出来了

# 实现思路

纯css实现的主要手段是利用label标签的模拟功能。label的for属性可以关联一个具体的input元素,即使这个input本身不可被用户可见,有个与它对应的label后,用户可以直接通过和label标签交互来替代原生的input——而这给我们的样式模拟留下了空间。简而言之就是:

隐藏原生input,样式定义的过程留给label (那为什么不直接改变checkbox的样式?因为checkbox作为浏览器默认组件,样式更改上并没有label那么方便,很多属性对checkbox都是不起作用的,比如background,而label在样式上基本和div一样'任人宰割')

而在选择事件上,由于css的“相邻选择符(E+F)”的存在,让我们可以直接利用html的默认checkbox,免去了js模拟选择的麻烦。

# 开始

 <!-- input的id必须有,这个是label进行元素匹配所必需的 -->
 <!-- 可以看到每个input的id和label的“for”属性对应同一字符串 -->
<input type="checkbox" id="checkbox01" />
<label for="checkbox01"></label>

<input type="checkbox" id="checkbox02" />
<label for="checkbox02"></label>

<input type="checkbox" id="checkbox03" />
<label for="checkbox03"></label>

<input type="checkbox" id="checkbox04" />
<label for="checkbox04"></label>
1
2
3
4
5
6
7
8
9
10
11
12
13
/* 隐藏所有checkbox */
input[type='checkbox'] {
 display: none;
}
/* 对label进行模拟.背景图片随便拼凑的,不要吐槽品味*/
/* transition效果是做个背景切换效果,这里单纯演示而已,实际上这个过渡不加更自然*/
label {
 display: inline-block;
 width: 60px;
 height: 60px;
 position: relative;
 background: url(//www.chitanda.me/images/blank.png);
 background-position: 0 0px;
 -webkit-transition: background 0.5s linear;
}
/* 利用相邻选择符和checkbox`:checked`的状态伪类来模拟默认选中效果(就是点击后那个勾号的效果) */
/*如果这段代码注释,点击后将没有任何反馈给用户*/
/*因为label本身是没有点击后被选中的状态的,checkbox被隐藏后,这个状态只能手动模拟*/
input[type='checkbox']:checked+label {
 background-position: 0 -60px;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/* 伪元素的生效很简单,定义`content`就好,其余的属性和普通div一样 */
label::after {
 content: attr(data-name);
 /*利用attr可以减少css代码量,data-name写在html部分的label属性里*/
 display: inline-block;
 position: relative;
 width: 120px;
 height: 60px;
 left: 100%;
 vertical-align: middle;
 margin: 10px;
}
1
2
3
4
5
6
7
8
9
10
11
12

# 开关实例

<label><input class="mui-switch" type="checkbox"> 默认未选中</label>

<label><input class="mui-switch" type="checkbox" checked> 默认选中</label>

<label><input class="mui-switch mui-switch-animbg" type="checkbox"> 默认未选中,简单的背景过渡效果,加mui-switch-animbg类即可</label>

<label><input class="mui-switch mui-switch-animbg" type="checkbox" checked> 默认选中</label>

<label><input class="mui-switch mui-switch-anim" type="checkbox"> 默认未选中,过渡效果,加 mui-switch-anim
类即可</label>

<label><input class="mui-switch mui-switch-anim" type="checkbox" checked> 默认选中</label>
1
2
3
4
5
6
7
8
9
10
11
12
<style>
 .mui-switch {
 width: 52px;
 height: 31px;
 position: relative;
 border: 1px solid #dfdfdf;
 background-color: #fdfdfd;
 box-shadow: #dfdfdf 0 0 0 0 inset;
 border-radius: 20px;
 border-top-left-radius: 20px;
 border-top-right-radius: 20px;
 border-bottom-left-radius: 20px;
 border-bottom-right-radius: 20px;
 background-clip: content-box;
 display: inline-block;
 -webkit-appearance: none;
 user-select: none;
 outline: none;
 }
 .mui-switch:before {
 content: '';
 width: 29px;
 height: 29px;
 position: absolute;
 top: 0px;
 left: 0;
 border-radius: 20px;
 border-top-left-radius: 20px;
 border-top-right-radius: 20px;
 border-bottom-left-radius: 20px;
 border-bottom-right-radius: 20px;
 background-color: #fff;
 box-shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
 }
 .mui-switch:checked {
 border-color: #64bd63;
 box-shadow: #64bd63 0 0 0 16px inset;
 background-color: #64bd63;
 }
 .mui-switch:checked:before {
 left: 21px;
 }
 .mui-switch.mui-switch-animbg {
 transition: background-color ease 0.4s;
 }
 .mui-switch.mui-switch-animbg:before {
 transition: left 0.3s;
 }
 .mui-switch.mui-switch-animbg:checked {
 box-shadow: #dfdfdf 0 0 0 0 inset;
 background-color: #64bd63;
 transition: border-color 0.4s, background-color ease 0.4s;
 }
 .mui-switch.mui-switch-animbg:checked:before {
 transition: left 0.3s;
 }
 .mui-switch.mui-switch-anim {
 transition: border cubic-bezier(0, 0, 0, 1) 0.4s, box-shadow cubic-bezier(0, 0, 0, 1) 0.4s;
 }
 .mui-switch.mui-switch-anim:before {
 transition: left 0.3s;
 }
 .mui-switch.mui-switch-anim:checked {
 box-shadow: #64bd63 0 0 0 16px inset;
 background-color: #64bd63;
 transition: border ease 0.4s, box-shadow ease 0.4s, background-color ease 1.2s;
 }
 .mui-switch.mui-switch-anim:checked:before {
 transition: left 0.3s;
 }
 /*# sourceMappingURL=mui-switch.css.map */
</style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72