@@ -6,11 +6,16 @@ When creating a form, you can use the following elements: `<form>`, `<label>`, `
6
6
7
7
Labels are typically placed on top or to the left of the form fields:
8
8
9
- ``` html
10
- <form action =" /dataCollectionLocation" method =" post" autocomplete =' on ' >
11
- <div v-for =" item in formItems" :key =" item.id" class =' form-item' >
9
+ ``` html
10
+ <form action =" /dataCollectionLocation" method =" post" autocomplete =" on " >
11
+ <div v-for =" item in formItems" :key =" item.id" class =" form-item" >
12
12
<label :for =" item.id" >{{ item.label }}: </label >
13
- <input :type =" item.type" :id =" item.id" :name =" item.id" v-model =" item.value" >
13
+ <input
14
+ :type =" item.type"
15
+ :id =" item.id"
16
+ :name =" item.id"
17
+ v-model =" item.value"
18
+ />
14
19
</div >
15
20
<button type =" submit" >Submit</button >
16
21
</form >
@@ -30,8 +35,8 @@ Notice how you can include `autocomplete='on'` on the form element and it will a
30
35
Provide labels to describe the purpose of all form control; linking ` for ` and ` id ` :
31
36
32
37
``` html
33
- <label for =" name" >Name</label >
34
- <input type =" text" name =" name" id =" name" v-model =" name" />
38
+ <label for =" name" >Name</label >
39
+ <input type =" text" name =" name" id =" name" v-model =" name" />
35
40
```
36
41
37
42
<p class =" codepen " data-height =" 265 " data-theme-id =" light " data-default-tab =" js,result " data-user =" mlama007 " data-slug-hash =" wvMrGqz " style =" height : 265px ; box-sizing : border-box ; display : flex ; align-items : center ; justify-content : center ; border : 2px solid ; margin : 1em 0 ; padding : 1em ;" data-pen-title =" Form Label " >
@@ -51,7 +56,7 @@ Though you might have seen labels wrapping the input fields like this:
51
56
``` html
52
57
<label >
53
58
Name:
54
- <input type =" text" name =" name" id =" name" v-model =" name" />
59
+ <input type =" text" name =" name" id =" name" v-model =" name" />
55
60
</label >
56
61
```
57
62
@@ -64,7 +69,13 @@ You can also give the input an accessible name with [`aria-label`](https://devel
64
69
65
70
``` html
66
71
<label for =" name" >Name</label >
67
- <input type =" text" name =" name" id =" name" v-model =" name" :aria-label =" nameLabel" />
72
+ <input
73
+ type =" text"
74
+ name =" name"
75
+ id =" name"
76
+ v-model =" name"
77
+ :aria-label =" nameLabel"
78
+ />
68
79
```
69
80
70
81
<p class =" codepen " data-height =" 265 " data-theme-id =" light " data-default-tab =" js,result " data-user =" mlama007 " data-slug-hash =" jOWGqgz " style =" height : 265px ; box-sizing : border-box ; display : flex ; align-items : center ; justify-content : center ; border : 2px solid ; margin : 1em 0 ; padding : 1em ;" data-pen-title =" Form ARIA label " >
@@ -83,11 +94,22 @@ Feel free to inspect this element in Chrome DevTools to see how the accessible n
83
94
Using [ ` aria-labelledby ` ] ( https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_aria-labelledby_attribute ) is similar to ` aria-label ` expect it is used if the label text is visible on screen. It is paired to other elements by their ` id ` and you can link multiple ` id ` s:
84
95
85
96
``` html
86
- <form class =" demo" action =" /dataCollectionLocation" method =" post" autocomplete =" on" >
97
+ <form
98
+ class =" demo"
99
+ action =" /dataCollectionLocation"
100
+ method =" post"
101
+ autocomplete =" on"
102
+ >
87
103
<h1 id =" billing" >Billing</h1 >
88
104
<div class =" form-item" >
89
105
<label for =" name" >Name:</label >
90
- <input type =" text" name =" name" id =" name" v-model =" name" aria-labelledby =" billing name" />
106
+ <input
107
+ type =" text"
108
+ name =" name"
109
+ id =" name"
110
+ v-model =" name"
111
+ aria-labelledby =" billing name"
112
+ />
91
113
</div >
92
114
<button type =" submit" >Submit</button >
93
115
</form >
@@ -107,11 +129,23 @@ Using [`aria-labelledby`](https://developer.mozilla.org/en-US/docs/Web/Accessibi
107
129
[ aria-describedby] ( https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_aria-describedby_attribute ) is used the same way as ` aria-labelledby ` expect provides a description with additional information that the user might need. This can be used to describe the criteria for any input:
108
130
109
131
``` html
110
- <form class =" demo" action =" /dataCollectionLocation" method =" post" autocomplete =" on" >
132
+ <form
133
+ class =" demo"
134
+ action =" /dataCollectionLocation"
135
+ method =" post"
136
+ autocomplete =" on"
137
+ >
111
138
<h1 id =" billing" >Billing</h1 >
112
139
<div class =" form-item" >
113
140
<label for =" name" >Full Name:</label >
114
- <input type =" text" name =" name" id =" name" v-model =" name" aria-labelledby =" billing name" aria-describedby =" nameDescription" />
141
+ <input
142
+ type =" text"
143
+ name =" name"
144
+ id =" name"
145
+ v-model =" name"
146
+ aria-labelledby =" billing name"
147
+ aria-describedby =" nameDescription"
148
+ />
115
149
<p id =" nameDescription" >Please provide first and last name.</p >
116
150
</div >
117
151
<button type =" submit" >Submit</button >
@@ -150,23 +184,28 @@ When adding instructions for your input fields, make sure to link it correctly t
150
184
You can provide additional instructions and bind multiple ids inside an [ ` aria-labelledby ` ] ( https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_aria-labelledby_attribute ) . This allows for more flexible design.
151
185
152
186
``` html
153
- <fieldset >
154
- <legend >Using aria-labelledby</legend >
155
- <label id =" date-label" for =" date" >Current Date:</label >
156
- <input type =" date" name =" date" id =" date" aria-labelledby =" date-label date-instructions" />
157
- <p id =" date-instructions" >MM/DD/YYYY</p >
158
- </fieldset >
187
+ <fieldset >
188
+ <legend >Using aria-labelledby</legend >
189
+ <label id =" date-label" for =" date" >Current Date:</label >
190
+ <input
191
+ type =" date"
192
+ name =" date"
193
+ id =" date"
194
+ aria-labelledby =" date-label date-instructions"
195
+ />
196
+ <p id =" date-instructions" >MM/DD/YYYY</p >
197
+ </fieldset >
159
198
```
160
199
161
200
Alternatively, you can attach the instructions to the input with [ ` aria-describedby ` ] ( https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_aria-describedby_attribute ) :
162
201
163
202
``` html
164
- <fieldset >
165
- <legend >Using aria-describedby</legend >
166
- <label id =" dob" for =" dob" >Date of Birth:</label >
167
- <input type =" date" name =" dob" id =" dob" aria-describedby =" dob-instructions" />
168
- <p id =" dob-instructions" >MM/DD/YYYY</p >
169
- </fieldset >
203
+ <fieldset >
204
+ <legend >Using aria-describedby</legend >
205
+ <label id =" dob" for =" dob" >Date of Birth:</label >
206
+ <input type =" date" name =" dob" id =" dob" aria-describedby =" dob-instructions" />
207
+ <p id =" dob-instructions" >MM/DD/YYYY</p >
208
+ </fieldset >
170
209
```
171
210
172
211
<p class =" codepen " data-height =" 265 " data-theme-id =" light " data-default-tab =" js,result " data-user =" mlama007 " data-slug-hash =" GRoMqYy " style =" height : 265px ; box-sizing : border-box ; display : flex ; align-items : center ; justify-content : center ; border : 2px solid ; margin : 1em 0 ; padding : 1em ;" data-pen-title =" Form Instructions " >
@@ -182,12 +221,11 @@ Usually it is not recommended to visually hide labels, even if the input has an
182
221
183
222
Let's look at this search field:
184
223
185
- ``` html
224
+ ``` html
186
225
<form role =" search" >
187
226
<label for =" search" class =" hidden-visually" >Search: </label >
188
227
<input type =" text" name =" search" id =" search" v-model =" search" />
189
- </div >
190
- <button type =" submit" >Search</button >
228
+ <button type =" submit" >Search</button >
191
229
</form >
192
230
```
193
231
@@ -231,15 +269,14 @@ When using buttons inside a form, you must set the type to prevent submitting th
231
269
You can also use an input to create buttons:
232
270
233
271
``` html
234
- <form action =" /dataCollectionLocation" method =" post" autocomplete =' on ' >
272
+ <form action =" /dataCollectionLocation" method =" post" autocomplete =" on " >
235
273
<!-- Buttons -->
236
274
<button type =" button" >Cancel</button >
237
275
<button type =" submit" >Submit</button >
238
-
239
276
240
277
<!-- Input buttons -->
241
- <input type =" button" value =" Cancel" >
242
- <input type =" submit" value =" Submit" >
278
+ <input type =" button" value =" Cancel" / >
279
+ <input type =" submit" value =" Submit" / >
243
280
</form >
244
281
```
245
282
@@ -255,28 +292,34 @@ You can also use an input to create buttons:
255
292
You can use this technique to create functional images.
256
293
257
294
- Input fields
295
+
258
296
- These images will act as a submit type button on forms
259
-
297
+
260
298
``` html
261
299
<form role =" search" >
262
300
<label for =" search" class =" hidden-visually" >Search: </label >
263
- <input type =" text" name =" search" id =" search" v-model =" search" >
264
- <input type =" image" class =" btnImg" src =" https://img.icons8.com/search" alt =" Search" >
301
+ <input type =" text" name =" search" id =" search" v-model =" search" />
302
+ <input
303
+ type =" image"
304
+ class =" btnImg"
305
+ src =" https://img.icons8.com/search"
306
+ alt =" Search"
307
+ />
265
308
</form >
266
309
```
267
310
268
311
- Icons
269
-
312
+
270
313
``` html
271
314
<form role =" search" >
272
315
<label for =" searchIcon" class =" hidden-visually" >Search: </label >
273
- <input type =" text" name =" searchIcon" id =" searchIcon" v-model =" searchIcon" >
316
+ <input type =" text" name =" searchIcon" id =" searchIcon" v-model =" searchIcon" / >
274
317
<button type =" submit" >
275
318
<i class =" fas fa-search" aria-hidden =" true" ></i >
276
319
<span class =" hidden-visually" >Search</span >
277
320
</button >
278
321
</form >
279
- ````
322
+ ```
280
323
281
324
<p class =" codepen " data-height =" 265 " data-theme-id =" light " data-default-tab =" js,result " data-user =" mlama007 " data-slug-hash =" NWxXeqY " style =" height : 265px ; box-sizing : border-box ; display : flex ; align-items : center ; justify-content : center ; border : 2px solid ; margin : 1em 0 ; padding : 1em ;" data-pen-title =" Functional Images " >
282
325
<span >See the Pen <a href =" https://codepen.io/mlama007/pen/NWxXeqY " >
0 commit comments