使用纯CSS美化radio和checkbox

使用纯CSS美化radio和checkbox
源码:

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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<style>
.demo{width:360px;margin:50px auto 10px auto;padding:10px;}
.demo img{width:90%}
.demo h3{font-size:1.5em;line-height:1.9em}
 
.col{margin-top:20px}
.col h4{height:40px;line-height:40px}
.opt{height:30px;line-height:24px}
@keyframes hover-color {
  from {
    border-color#c0c0c0; }
  to {
    border-color#3e97eb; } }
 
.magic-radio,
.magic-checkbox {
  positionabsolute;
  displaynone; }
 
.magic-radio[disabled],
.magic-checkbox[disabled] {
  cursor: not-allowed; }
 
.magic-radio + label,
.magic-checkbox + label {
  positionrelative;
  displayblock;
  padding-left30px;
  cursorpointer;
  vertical-alignmiddle; }
  .magic-radio + label:hover:before,
  .magic-checkbox + label:hover:before {
    animation-duration: 0.4s;
    animation-fill-mode: both;
    animation-name: hover-color; }
  .magic-radio + label:before,
  .magic-checkbox + label:before {
    positionabsolute;
    top0;
    left0;
    display: inline-block;
    width20px;
    height20px;
    content'';
    border1px solid #c0c0c0; }
  .magic-radio + label:after,
  .magic-checkbox + label:after {
    positionabsolute;
    displaynone;
    content''; }
 
.magic-radio[disabled] + label,
.magic-checkbox[disabled] + label {
  cursor: not-allowed;
  color#e4e4e4; }
  .magic-radio[disabled] + label:hover, .magic-radio[disabled] + label:before, .magic-radio[disabled] + label:after,
  .magic-checkbox[disabled] + label:hover,
  .magic-checkbox[disabled] + label:before,
  .magic-checkbox[disabled] + label:after {
    cursor: not-allowed; }
  .magic-radio[disabled] + label:hover:before,
  .magic-checkbox[disabled] + label:hover:before {
    border1px solid #e4e4e4;
    animation-name: none; }
  .magic-radio[disabled] + label:before,
  .magic-checkbox[disabled] + label:before {
    border-color#e4e4e4; }
 
.magic-radio:checked + label:before,
.magic-checkbox:checked + label:before {
  animation-name: none; }
 
.magic-radio:checked + label:after,
.magic-checkbox:checked + label:after {
  displayblock; }
 
.magic-radio + label:before {
  border-radius: 50%; }
 
.magic-radio + label:after {
  top7px;
  left7px;
  width8px;
  height8px;
  border-radius: 50%;
  background#3e97eb; }
 
.magic-radio:checked + label:before {
  border1px solid #3e97eb; }
 
.magic-radio:checked[disabled] + label:before {
  border1px solid #c9e2f9; }
 
.magic-radio:checked[disabled] + label:after {
  background#c9e2f9; }
 
.magic-checkbox + label:before {
  border-radius: 3px; }
 
.magic-checkbox + label:after {
  top2px;
  left7px;
  box-sizing: border-box;
  width6px;
  height12px;
  transform: rotate(45deg);
  border-width2px;
  border-stylesolid;
  border-color#fff;
  border-top0;
  border-left0; }
 
.magic-checkbox:checked + label:before {
  border#3e97eb;
  background#3e97eb; }
 
.magic-checkbox:checked[disabled] + label:before {
  border#c9e2f9;
  background#c9e2f9; }
 
</style>
<div class="demo">
        <div class="col">
          <h4>Checkbox</h4>
             
            <div class="opt">
                <input class="magic-checkbox" type="checkbox" name="layout" id="c1">
                <label for="c1">Normal</label>
            </div>
            <div class="opt">
                <input class="magic-checkbox" type="checkbox" name="layout" id="c2" checked>
                <label for="c2">Checked</label>
            </div>
            <div class="opt">
                <input class="magic-checkbox" type="checkbox" name="layout" id="c3" value="option2" disabled>
                <label for="c3" style="color:#ccc">Disabled</label>
            </div>
            <div class="opt">
                <input class="magic-checkbox" type="checkbox" name="layout" id="c4" checked disabled>
                <label for="c4" style="color:#ccc">Checked && Disabled</label>
            </div>
 
 
 
        </div>
 
        <div class="col">
            <h4>Radio</h4>
            <div class="opt">
                <input class="magic-radio" type="radio" name="radio" id="r1" value="option1">
                <label for="r1">Normal</label>
            </div>
            <div class="opt">
                <input class="magic-radio" type="radio" name="radio" id="r2" value="option2" checked>
                <label for="r2">Checked</label>
            </div>
            <div class="opt">
                <input class="magic-radio" type="radio" name="radio" id="r3" value="option3" disabled>
                <label for="r3" style="color:#ccc">禁用</label>
            </div>
            <div class="opt">
                <input class="magic-radio" type="radio" id="r4" value="option4" checked disabled>
                <label for="r4" style="color:#ccc">Checked && Disabled</label>
            </div>
        </div>
    </div>
虚拟主机
《你不知道的JavaScript(中卷)》PDF
《JavaScript高级程序设计(第3版)》PDF
《HTML5从入门到精通》PDF
《JavaScript DOM编程艺术》(第二版)PDF
广告也精彩