0%

CSS自定义复选框

html

1
<input type="checkbox" class="checkbox"/>

实现思路:将原始样式用::before替换掉,使用::after的边框绘制打勾。

css(less)

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
.checkbox {
@act-color: #409eff;

width: 14px;
height: 14px;
vertical-align: sub;
overflow: visible;
visibility: hidden;
position: relative;

// 替换掉原始样式
&::before {
content: "";
visibility: visible;

display: inline-block;
position: relative;
border: 1px solid #dcdfe6;
border-radius: 2px;
box-sizing: border-box;
width: 14px;
height: 14px;
background-color: #FFF;
}

&:hover::before {
cursor: pointer;
border-color: @act-color;
}

&:checked {
&::before {
background-color: @act-color;
border: none;
}

// 打勾符号
&::after {
box-sizing: content-box;
content: "";
border: 1px solid #FFF;
border-left: 0;
border-top: 0;
height: 7px;
left: 5px;
position: absolute;
top: 2px;
transform: rotate(45deg) scaleY(1);
visibility: visible;
width: 3px;
transform-origin: center;
}
}
}