-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
218 lines (192 loc) · 6.2 KB
/
index.html
File metadata and controls
218 lines (192 loc) · 6.2 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta
name="viewport"
content="width=device-width"
>
<title>Rating Scale Input - Custom Radio Buttons with SVG and CSS</title>
<!--Typography via Adobe Fonts -->
<link
rel="stylesheet"
href="https://use.typekit.net/hqg7rfa.css"
>
<link
href="style.css"
rel="stylesheet"
type="text/css"
/>
</head>
<body>
<h2>Rating Scale Input</h2>
<h3>Custom Radio Buttons with SVG and CSS</h3>
<form>
<fieldset id="water">
<legend>Thirsty?</legend>
<div class="droplets">
<input
type="radio"
id="w5"
name="water"
value="5"
/>
<label for="w5">
<svg viewbox="0 0 24 24">
<use
class="inactive"
href="#inactive"
/>
<use
class="active"
href="#active"
/>
</svg>
</label>
<input
type="radio"
id="w4"
name="water"
value="4"
/>
<label for="w4">
<svg viewbox="0 0 24 24">
<use
class="inactive"
href="#inactive"
/>
<use
class="active"
href="#active"
/>
</svg>
</label>
<input
type="radio"
id="w3"
name="water"
value="3"
/>
<label for="w3">
<svg viewbox="0 0 24 24">
<use
class="inactive"
href="#inactive"
/>
<use
class="active"
href="#active"
/>
</svg>
</label>
<input
type="radio"
id="w2"
name="water"
value="2"
/>
<label for="w2">
<svg viewbox="0 0 24 24">
<use
class="inactive"
href="#inactive"
/>
<use
class="active"
href="#active"
/>
</svg>
</label>
<input
type="radio"
id="w1"
name="water"
value="1"
checked
/>
<label for="w1">
<svg viewbox="0 0 24 24">
<use
class="inactive"
href="#inactive"
/>
<use
class="active"
href="#active"
/>
</svg>
</label>
</div>
</fieldset>
<div id="status">Rating is 1</div>
</form>
<p>This page uses standard <a
target="_blank"
href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/radio"
>radio buttons</a> to implement a user interface for rating on a scale of one to five. Thematic water drops riff on
the more conventional "star". </p>
<p>Vector artwork is defined directly in index.html as <a
target="_blank"
href="https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/SVG_In_HTML_Introduction"
>inline SVG</a> markup. Specifically, we create a set of <a
target="_blank"
href="https://developer.mozilla.org/en-US/docs/Web/SVG/Element/symbol"
>SVG symbols</a>, strategically named <b>#active</b> and <b>#inactive</b>. These symbols are re-used several times
(in our radio button labels) via the SVG <a
target="_blank"
href="https://developer.mozilla.org/en-US/docs/Web/SVG/Element/use"
>use</a> tag. Importantly, <a
target="_blank"
href="https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/SVG_In_HTML_Introduction"
>Inline SVGs</a> are adjustable via CSS (e.g. we can change the <a
target="_blank"
href="https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/Fills_and_Strokes"
>fill</a> on <a
target="_blank"
href="https://developer.mozilla.org/en-US/docs/Web/CSS/:hover"
>:hover</a>).</p>
<p>Droplets appear in descending order (5-4-3-2-1) in HTML, but this arrangement is flipped in CSS via <a
target="_blank"
href="https://developer.mozilla.org/en-US/docs/Web/CSS/flex-direction"
>flex-direction: row-reverse</a>. This approach makes it possible to exploit <a
target="_blank"
href="https://developer.mozilla.org/en-US/docs/Web/CSS/Subsequent-sibling_combinator"
>Subsequent Sibling</a> (<a
target="_blank"
href="https://developer.mozilla.org/en-US/docs/Web/CSS/Subsequent-sibling_combinator"
>~</a>) logic in CSS. <i>Note that CSS lacks a "prior sibling" combinator</i>. To show / hide the inactive / active
SVG symbols, we use the <a
target="_blank"
href="https://developer.mozilla.org/en-US/docs/Web/CSS/:checked"
>:checked</a> and <a
target="_blank"
href="https://developer.mozilla.org/en-US/docs/Web/CSS/:hover"
>:hover</a> psuedo-classes to toggle <b>display: none</b> and <b>display: block</b>.</p>
<p> As a caviat, keep in mind that this sort of adaptation involves tradeoffs between visual appearance, brand,
usability, and accessibility.</p>
<p>For comparison (and further inspiration) note that it's also possible to do this without SVGs. For example, <a
target="_blank"
href="https://codepen.io/jamesbarnett/pen/najzYK"
>this codepen by James Barnett</a> employs Font Awesome icons and the <a
target="_blank"
href="https://developer.mozilla.org/en-US/docs/Web/CSS/::before"
>::before</a> pseudo-element. Remember that there are many ways to solve a problem. What other CSS or
JavaScript-based approaches can you find?</p>
<p>This demo was created by <a
target="_blank"
href="https://nsitu.ca"
>Harold Sikkema</a>. </p>
<!-- Below we define a set of SVG symbols for active and inactive -->
<svg>
<symbol id="inactive">
<path
d="M12 2c-5.33 4.55-8 8.48-8 11.8 0 4.98 3.8 8.2 8 8.2s8-3.22 8-8.2c0-3.32-2.67-7.25-8-11.8zm0 18c-3.35 0-6-2.57-6-6.2 0-2.34 1.95-5.44 6-9.14 4.05 3.7 6 6.79 6 9.14 0 3.63-2.65 6.2-6 6.2z"
/>
</symbol>
<symbol id="active">
<path d="M12 2c-5.33 4.55-8 8.48-8 11.8 0 4.98 3.8 8.2 8 8.2s8-3.22 8-8.2c0-3.32-2.67-7.25-8-11.8z" />
</symbol>
</svg>
<script src="script.js"></script>
</body>
</html>