用纯CSS自定义radio样式

<label></label>标签的 for属性 + :checked 做,纯CSS

单选项示例如下:

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
<!-- HTML -->
<div class="radio-beauty-container">
<label>
<span class="radio-name">radio1</span>
<input type="radio" name="radioName" id="radioName1" hidden/>
<label for="radioName1" class="radio-beauty"></label>
</label>
<label>
<span class="radio-name">radio2</span>
<input type="radio" name="radioName" id="radioName2" hidden/>
<label for="radioName2" class="radio-beauty"></label>
</label>
<label>
<span class="radio-name">radio3</span>
<input type="radio" name="radioName" id="radioName3" hidden/>
<label for="radioName3" class="radio-beauty"></label>
</label>
</div>
/* CSS */
.radio-beauty-container {
font-size: 0;
}
.radio-beauty-container .radio-beauty:hover, .radio-beauty-container input[type="radio"]:checked + .radio-beauty {
padding: 2px;
background-color: green;
background-clip: content-box;
}
.radio-beauty-container .radio-name {
vertical-align: middle;
font-size: 16px;
}
.radio-beauty-container .radio-beauty {
width: 18px;
height: 18px;
box-sizing: border-box;
display: inline-block;
border: 1px solid green;
vertical-align: middle;
margin: 0 15px 0 3px;
border-radius: 50%;
}
.radio-beauty-container .radio-beauty:hover {
box-shadow: 0 0 7px green;
}

效果: