-
Notifications
You must be signed in to change notification settings - Fork 0
/
adv-css-pseudo-custom-checkbox.html
92 lines (82 loc) · 2.66 KB
/
adv-css-pseudo-custom-checkbox.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Custom Checkbox</title>
<style>
/* pseudo elements are not selected by the universal selector
so you must select individually */
*,
*::before,
*::after {
box-sizing: border-box;
}
.custom-checkbox + label {
display: flex;
align-items: center;
cursor: pointer;
}
.custom-checkbox {
/* display: none; hides checkbox but no longer visible to screen readers and tab */
/* display: none; */
/* opacity: 0; hides checkbox however still visible to screen readers and tab */
opacity: 0;
/* moves the checkbox off screen */
position: absolute;
left: -9999px;
}
/* now use pseudo element to create a custom checkbox */
/* since ::before is a child of the label we can align it using flexbox (above) */
/* select the sibling label::before of the hidden checkbox */
.custom-checkbox + label::before {
content: '';
/* use em so checkbox size scales with label font-size */
width: 1.1em;
height: 1.1em;
margin-right: 0.5em;
border-radius: 0.15em;
border: 0.05em solid black;
}
.custom-checkbox + label:hover::before {
/* background-color: #0af; */
background-color: hsl(200, 100%, 50%);
}
.custom-checkbox:focus + label::before {
box-shadow: 0 0 20px 0 black;
}
.custom-checkbox:checked + label::before {
/* https://www.toptal.com/designers/htmlarrows/symbols/ */
/* content: '\2714'; */
content: '✔';
display: flex;
justify-content: center;
align-items: center;
background-color: #069;
color: white;
}
.custom-checkbox:disabled + label {
color: #aaa;
cursor: not-allowed;
}
.custom-checkbox:disabled + label::before {
background-color: #ccc;
border-color: #999;
}
</style>
</head>
<body>
<input class="custom-checkbox" type="checkbox" id="cb1" disabled checked />
<label for="cb1">Checkbox 1</label>
<br />
<input class="custom-checkbox" type="checkbox" id="cb2" disabled />
<label for="cb2">Checkbox 2</label>
<br />
<input class="custom-checkbox" type="checkbox" id="cb3" />
<label for="cb3">Checkbox 3</label>
<br />
<input class="custom-checkbox" type="checkbox" id="cb4" />
<label for="cb4">Checkbox 4</label>
</body>
</html>