-
Notifications
You must be signed in to change notification settings - Fork 0
/
liltogl.html
117 lines (98 loc) · 3.71 KB
/
liltogl.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Lil Togl</title>
<style>
body {
font-family: system-ui, sans-serif;
}
label.toggle {
display: inline-block;
width: 20ch; /* optionally add width to right-align toggles */
}
/* Begin Toggle CSS */
label.toggle {
cursor: pointer;
user-select: none;
display: inline-block;
}
label.toggle > input[type="checkbox"] {
vertical-align: top;
margin-right: 1ex;
}
@supports(selector(:has(p))) {
/* label */
label.toggle {
--width: 2rem;
--height: calc(var(--width) / 2);
--border-radius: var(--height);
/* Customize style here */
--track-color: lightgrey;
--track-color-selected: blue;
--indicator-color: white;
--indicator-color-selected: white;
box-sizing: border-box;
position: relative;
padding-right: calc(var(--width) + 0.5rem);
}
/* checkbox */
label.toggle > input[type="checkbox"] {
display: none;
}
/* toggle track */
label.toggle::after {
content: "";
background-color: var(--track-color);
width: var(--width);
position: absolute;
right: 0;
height: calc(var(--height) + 2px);
border-radius: var(--border-radius);
border: 1px solid var(--track-color);
}
label.toggle:has( > input[type="checkbox"]:checked )::after {
background-color: var(--track-color-selected);
border-color: var(--track-color-selected);
}
/* toggle indicator */
label.toggle::before {
content: "";
position: absolute;
right: calc( (var(--width) / 2) );
top: 2px;
width: calc(var(--width) / 2);
height: var(--height);
background: white;
border-radius: var(--border-radius);
transition: 0.2s;
background-color: var(--indicator-color);
z-index: 5;
}
label.toggle:has( > input[type="checkbox"]:checked )::before {
right: 2px;
background-color: var(--indicator-color-selected);
}
}
/* End Toggle CSS */
</style>
</head>
<body>
<h1>Lil Togl</h1>
<p>Features:</p>
<ul>
<li>100% CSS toggle widget.</li>
<li>Uses the CSS <code>:has</code> pseudo element selector.</li>
<li>If that CSS feature is not available, falls back to semantically correct <code><label></code> and <code><input type="checkbox"></code>. No fluff elements.</li>
<li>Checkbox on left side of the label, toggle on right side of the label.</li>
<li>Customize with scoped CSS variables.</li>
</ul>
<form>
<p><label class="toggle" for="check1"><input type="checkbox" id="check1" name="check1">Feature 1</label></p>
<p><label class="toggle" for="check2"><input type="checkbox" id="check2" name="check2">Feature 2</label></p>
<p><label class="toggle" for="check3"><input type="checkbox" id="check3" name="check3" checked>Pre-selected</label></p>
<input type="submit">
</form>
</body>
</html>