Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
---
2
layout: docs
3
title: Forms
4
description: Examples and usage guidelines for form control styles, layout options, and custom components for creating a wide variety of forms.
5
group: components
6
toc: true
7
---
8
 
9
## Overview
10
 
11
Bootstrap's form controls expand on [our Rebooted form styles]({{< docsref "/content/reboot#forms" >}}) with classes. Use these classes to opt into their customized displays for a more consistent rendering across browsers and devices.
12
 
13
Be sure to use an appropriate `type` attribute on all inputs (e.g., `email` for email address or `number` for numerical information) to take advantage of newer input controls like email verification, number selection, and more.
14
 
15
Here's a quick example to demonstrate Bootstrap's form styles. Keep reading for documentation on required classes, form layout, and more.
16
 
17
{{< example >}}
18
<form>
19
  <div class="form-group">
20
    <label for="exampleInputEmail1">Email address</label>
21
    <input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp">
22
    <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
23
  </div>
24
  <div class="form-group">
25
    <label for="exampleInputPassword1">Password</label>
26
    <input type="password" class="form-control" id="exampleInputPassword1">
27
  </div>
28
  <div class="form-group form-check">
29
    <input type="checkbox" class="form-check-input" id="exampleCheck1">
30
    <label class="form-check-label" for="exampleCheck1">Check me out</label>
31
  </div>
32
  <button type="submit" class="btn btn-primary">Submit</button>
33
</form>
34
{{< /example >}}
35
 
36
## Form controls
37
 
38
Textual form controls—like `<input>`s, `<select>`s, and `<textarea>`s—are styled with the `.form-control` class. Included are styles for general appearance, focus state, sizing, and more.
39
 
40
Be sure to explore our [custom forms](#custom-forms) to further style `<select>`s.
41
 
42
{{< example >}}
43
<form>
44
  <div class="form-group">
45
    <label for="exampleFormControlInput1">Email address</label>
46
    <input type="email" class="form-control" id="exampleFormControlInput1" placeholder="name@example.com">
47
  </div>
48
  <div class="form-group">
49
    <label for="exampleFormControlSelect1">Example select</label>
50
    <select class="form-control" id="exampleFormControlSelect1">
51
      <option>1</option>
52
      <option>2</option>
53
      <option>3</option>
54
      <option>4</option>
55
      <option>5</option>
56
    </select>
57
  </div>
58
  <div class="form-group">
59
    <label for="exampleFormControlSelect2">Example multiple select</label>
60
    <select multiple class="form-control" id="exampleFormControlSelect2">
61
      <option>1</option>
62
      <option>2</option>
63
      <option>3</option>
64
      <option>4</option>
65
      <option>5</option>
66
    </select>
67
  </div>
68
  <div class="form-group">
69
    <label for="exampleFormControlTextarea1">Example textarea</label>
70
    <textarea class="form-control" id="exampleFormControlTextarea1" rows="3"></textarea>
71
  </div>
72
</form>
73
{{< /example >}}
74
 
75
For file inputs, swap the `.form-control` for `.form-control-file`.
76
 
77
{{< example >}}
78
<form>
79
  <div class="form-group">
80
    <label for="exampleFormControlFile1">Example file input</label>
81
    <input type="file" class="form-control-file" id="exampleFormControlFile1">
82
  </div>
83
</form>
84
{{< /example >}}
85
 
86
### Sizing
87
 
88
Set heights using classes like `.form-control-lg` and `.form-control-sm`.
89
 
90
{{< example >}}
91
<input class="form-control form-control-lg" type="text" placeholder=".form-control-lg">
92
<input class="form-control" type="text" placeholder="Default input">
93
<input class="form-control form-control-sm" type="text" placeholder=".form-control-sm">
94
{{< /example >}}
95
 
96
{{< example >}}
97
<select class="form-control form-control-lg">
98
  <option>Large select</option>
99
</select>
100
<select class="form-control">
101
  <option>Default select</option>
102
</select>
103
<select class="form-control form-control-sm">
104
  <option>Small select</option>
105
</select>
106
{{< /example >}}
107
 
108
### Readonly
109
 
110
Add the `readonly` boolean attribute on an input to prevent modification of the input's value. Read-only inputs appear lighter (just like disabled inputs), but retain the standard cursor.
111
 
112
{{< example >}}
113
<input class="form-control" type="text" placeholder="Readonly input here..." readonly>
114
{{< /example >}}
115
 
116
### Readonly plain text
117
 
118
If you want to have `<input readonly>` elements in your form styled as plain text, use the `.form-control-plaintext` class to remove the default form field styling and preserve the correct margin and padding.
119
 
120
{{< example >}}
121
<form>
122
  <div class="form-group row">
123
    <label for="staticEmail" class="col-sm-2 col-form-label">Email</label>
124
    <div class="col-sm-10">
125
      <input type="text" readonly class="form-control-plaintext" id="staticEmail" value="email@example.com">
126
    </div>
127
  </div>
128
  <div class="form-group row">
129
    <label for="inputPassword" class="col-sm-2 col-form-label">Password</label>
130
    <div class="col-sm-10">
131
      <input type="password" class="form-control" id="inputPassword">
132
    </div>
133
  </div>
134
</form>
135
{{< /example >}}
136
 
137
{{< example >}}
138
<form class="form-inline">
139
  <div class="form-group mb-2">
140
    <label for="staticEmail2" class="sr-only">Email</label>
141
    <input type="text" readonly class="form-control-plaintext" id="staticEmail2" value="email@example.com">
142
  </div>
143
  <div class="form-group mx-sm-3 mb-2">
144
    <label for="inputPassword2" class="sr-only">Password</label>
145
    <input type="password" class="form-control" id="inputPassword2" placeholder="Password">
146
  </div>
147
  <button type="submit" class="btn btn-primary mb-2">Confirm identity</button>
148
</form>
149
{{< /example >}}
150
 
151
## Range Inputs
152
 
153
Set horizontally scrollable range inputs using `.form-control-range`.
154
 
155
{{< example >}}
156
<form>
157
  <div class="form-group">
158
    <label for="formControlRange">Example Range input</label>
159
    <input type="range" class="form-control-range" id="formControlRange">
160
  </div>
161
</form>
162
{{< /example >}}
163
 
164
## Checkboxes and radios
165
 
166
Default checkboxes and radios are improved upon with the help of `.form-check`, **a single class for both input types that improves the layout and behavior of their HTML elements**. Checkboxes are for selecting one or several options in a list, while radios are for selecting one option from many.
167
 
168
Disabled checkboxes and radios are supported. The `disabled` attribute will apply a lighter color to help indicate the input's state.
169
 
170
Checkboxes and radio buttons support HTML-based form validation and provide concise, accessible labels. As such, our `<input>`s and `<label>`s are sibling elements as opposed to an `<input>` within a `<label>`. This is slightly more verbose as you must specify `id` and `for` attributes to relate the `<input>` and `<label>`.
171
 
172
### Default (stacked)
173
 
174
By default, any number of checkboxes and radios that are immediate sibling will be vertically stacked and appropriately spaced with `.form-check`.
175
 
176
{{< example >}}
177
<div class="form-check">
178
  <input class="form-check-input" type="checkbox" value="" id="defaultCheck1">
179
  <label class="form-check-label" for="defaultCheck1">
180
    Default checkbox
181
  </label>
182
</div>
183
<div class="form-check">
184
  <input class="form-check-input" type="checkbox" value="" id="defaultCheck2" disabled>
185
  <label class="form-check-label" for="defaultCheck2">
186
    Disabled checkbox
187
  </label>
188
</div>
189
{{< /example >}}
190
 
191
{{< example >}}
192
<div class="form-check">
193
  <input class="form-check-input" type="radio" name="exampleRadios" id="exampleRadios1" value="option1" checked>
194
  <label class="form-check-label" for="exampleRadios1">
195
    Default radio
196
  </label>
197
</div>
198
<div class="form-check">
199
  <input class="form-check-input" type="radio" name="exampleRadios" id="exampleRadios2" value="option2">
200
  <label class="form-check-label" for="exampleRadios2">
201
    Second default radio
202
  </label>
203
</div>
204
<div class="form-check">
205
  <input class="form-check-input" type="radio" name="exampleRadios" id="exampleRadios3" value="option3" disabled>
206
  <label class="form-check-label" for="exampleRadios3">
207
    Disabled radio
208
  </label>
209
</div>
210
{{< /example >}}
211
 
212
### Inline
213
 
214
Group checkboxes or radios on the same horizontal row by adding `.form-check-inline` to any `.form-check`.
215
 
216
{{< example >}}
217
<div class="form-check form-check-inline">
218
  <input class="form-check-input" type="checkbox" id="inlineCheckbox1" value="option1">
219
  <label class="form-check-label" for="inlineCheckbox1">1</label>
220
</div>
221
<div class="form-check form-check-inline">
222
  <input class="form-check-input" type="checkbox" id="inlineCheckbox2" value="option2">
223
  <label class="form-check-label" for="inlineCheckbox2">2</label>
224
</div>
225
<div class="form-check form-check-inline">
226
  <input class="form-check-input" type="checkbox" id="inlineCheckbox3" value="option3" disabled>
227
  <label class="form-check-label" for="inlineCheckbox3">3 (disabled)</label>
228
</div>
229
{{< /example >}}
230
 
231
{{< example >}}
232
<div class="form-check form-check-inline">
233
  <input class="form-check-input" type="radio" name="inlineRadioOptions" id="inlineRadio1" value="option1">
234
  <label class="form-check-label" for="inlineRadio1">1</label>
235
</div>
236
<div class="form-check form-check-inline">
237
  <input class="form-check-input" type="radio" name="inlineRadioOptions" id="inlineRadio2" value="option2">
238
  <label class="form-check-label" for="inlineRadio2">2</label>
239
</div>
240
<div class="form-check form-check-inline">
241
  <input class="form-check-input" type="radio" name="inlineRadioOptions" id="inlineRadio3" value="option3" disabled>
242
  <label class="form-check-label" for="inlineRadio3">3 (disabled)</label>
243
</div>
244
{{< /example >}}
245
 
246
### Without labels
247
 
248
Add `.position-static` to inputs within `.form-check` that don't have any label text. Remember to still provide some form of accessible name for assistive technologies (for instance, using `aria-label`).
249
 
250
{{< example >}}
251
<div class="form-check">
252
  <input class="form-check-input position-static" type="checkbox" id="blankCheckbox" value="option1" aria-label="...">
253
</div>
254
<div class="form-check">
255
  <input class="form-check-input position-static" type="radio" name="blankRadio" id="blankRadio1" value="option1" aria-label="...">
256
</div>
257
{{< /example >}}
258
 
259
## Layout
260
 
261
Since Bootstrap applies `display: block` and `width: 100%` to almost all our form controls, forms will by default stack vertically. Additional classes can be used to vary this layout on a per-form basis.
262
 
263
### Form groups
264
 
265
The `.form-group` class is the easiest way to add some structure to forms. It provides a flexible class that encourages proper grouping of labels, controls, optional help text, and form validation messaging. By default it only applies `margin-bottom`, but it picks up additional styles in `.form-inline` as needed. Use it with `<fieldset>`s, `<div>`s, or nearly any other element.
266
 
267
{{< example >}}
268
<form>
269
  <div class="form-group">
270
    <label for="formGroupExampleInput">Example label</label>
271
    <input type="text" class="form-control" id="formGroupExampleInput" placeholder="Example input placeholder">
272
  </div>
273
  <div class="form-group">
274
    <label for="formGroupExampleInput2">Another label</label>
275
    <input type="text" class="form-control" id="formGroupExampleInput2" placeholder="Another input placeholder">
276
  </div>
277
</form>
278
{{< /example >}}
279
 
280
### Form grid
281
 
282
More complex forms can be built using our grid classes. Use these for form layouts that require multiple columns, varied widths, and additional alignment options.
283
 
284
{{< example >}}
285
<form>
286
  <div class="row">
287
    <div class="col">
288
      <input type="text" class="form-control" placeholder="First name">
289
    </div>
290
    <div class="col">
291
      <input type="text" class="form-control" placeholder="Last name">
292
    </div>
293
  </div>
294
</form>
295
{{< /example >}}
296
 
297
#### Form row
298
 
299
You may also swap `.row` for `.form-row`, a variation of our standard grid row that overrides the default column gutters for tighter and more compact layouts.
300
 
301
{{< example >}}
302
<form>
303
  <div class="form-row">
304
    <div class="col">
305
      <input type="text" class="form-control" placeholder="First name">
306
    </div>
307
    <div class="col">
308
      <input type="text" class="form-control" placeholder="Last name">
309
    </div>
310
  </div>
311
</form>
312
{{< /example >}}
313
 
314
More complex layouts can also be created with the grid system.
315
 
316
{{< example >}}
317
<form>
318
  <div class="form-row">
319
    <div class="form-group col-md-6">
320
      <label for="inputEmail4">Email</label>
321
      <input type="email" class="form-control" id="inputEmail4">
322
    </div>
323
    <div class="form-group col-md-6">
324
      <label for="inputPassword4">Password</label>
325
      <input type="password" class="form-control" id="inputPassword4">
326
    </div>
327
  </div>
328
  <div class="form-group">
329
    <label for="inputAddress">Address</label>
330
    <input type="text" class="form-control" id="inputAddress" placeholder="1234 Main St">
331
  </div>
332
  <div class="form-group">
333
    <label for="inputAddress2">Address 2</label>
334
    <input type="text" class="form-control" id="inputAddress2" placeholder="Apartment, studio, or floor">
335
  </div>
336
  <div class="form-row">
337
    <div class="form-group col-md-6">
338
      <label for="inputCity">City</label>
339
      <input type="text" class="form-control" id="inputCity">
340
    </div>
341
    <div class="form-group col-md-4">
342
      <label for="inputState">State</label>
343
      <select id="inputState" class="form-control">
344
        <option selected>Choose...</option>
345
        <option>...</option>
346
      </select>
347
    </div>
348
    <div class="form-group col-md-2">
349
      <label for="inputZip">Zip</label>
350
      <input type="text" class="form-control" id="inputZip">
351
    </div>
352
  </div>
353
  <div class="form-group">
354
    <div class="form-check">
355
      <input class="form-check-input" type="checkbox" id="gridCheck">
356
      <label class="form-check-label" for="gridCheck">
357
        Check me out
358
      </label>
359
    </div>
360
  </div>
361
  <button type="submit" class="btn btn-primary">Sign in</button>
362
</form>
363
{{< /example >}}
364
 
365
#### Horizontal form
366
 
367
Create horizontal forms with the grid by adding the `.row` class to form groups and using the `.col-*-*` classes to specify the width of your labels and controls. Be sure to add `.col-form-label` to your `<label>`s as well so they're vertically centered with their associated form controls.
368
 
369
At times, you maybe need to use margin or padding utilities to create that perfect alignment you need. For example, we've removed the `padding-top` on our stacked radio inputs label to better align the text baseline.
370
 
371
{{< example >}}
372
<form>
373
  <div class="form-group row">
374
    <label for="inputEmail3" class="col-sm-2 col-form-label">Email</label>
375
    <div class="col-sm-10">
376
      <input type="email" class="form-control" id="inputEmail3">
377
    </div>
378
  </div>
379
  <div class="form-group row">
380
    <label for="inputPassword3" class="col-sm-2 col-form-label">Password</label>
381
    <div class="col-sm-10">
382
      <input type="password" class="form-control" id="inputPassword3">
383
    </div>
384
  </div>
385
  <fieldset class="form-group row">
386
    <legend class="col-form-label col-sm-2 float-sm-left pt-0">Radios</legend>
387
    <div class="col-sm-10">
388
      <div class="form-check">
389
        <input class="form-check-input" type="radio" name="gridRadios" id="gridRadios1" value="option1" checked>
390
        <label class="form-check-label" for="gridRadios1">
391
          First radio
392
        </label>
393
      </div>
394
      <div class="form-check">
395
        <input class="form-check-input" type="radio" name="gridRadios" id="gridRadios2" value="option2">
396
        <label class="form-check-label" for="gridRadios2">
397
          Second radio
398
        </label>
399
      </div>
400
      <div class="form-check disabled">
401
        <input class="form-check-input" type="radio" name="gridRadios" id="gridRadios3" value="option3" disabled>
402
        <label class="form-check-label" for="gridRadios3">
403
          Third disabled radio
404
        </label>
405
      </div>
406
    </div>
407
  </fieldset>
408
  <div class="form-group row">
409
    <div class="col-sm-10 offset-sm-2">
410
      <div class="form-check">
411
        <input class="form-check-input" type="checkbox" id="gridCheck1">
412
        <label class="form-check-label" for="gridCheck1">
413
          Example checkbox
414
        </label>
415
      </div>
416
    </div>
417
  </div>
418
  <div class="form-group row">
419
    <div class="col-sm-10">
420
      <button type="submit" class="btn btn-primary">Sign in</button>
421
    </div>
422
  </div>
423
</form>
424
{{< /example >}}
425
 
426
##### Horizontal form label sizing
427
 
428
Be sure to use `.col-form-label-sm` or `.col-form-label-lg` to your `<label>`s or `<legend>`s to correctly follow the size of `.form-control-lg` and `.form-control-sm`.
429
 
430
{{< example >}}
431
<form>
432
  <div class="form-group row">
433
    <label for="colFormLabelSm" class="col-sm-2 col-form-label col-form-label-sm">Email</label>
434
    <div class="col-sm-10">
435
      <input type="email" class="form-control form-control-sm" id="colFormLabelSm" placeholder="col-form-label-sm">
436
    </div>
437
  </div>
438
  <div class="form-group row">
439
    <label for="colFormLabel" class="col-sm-2 col-form-label">Email</label>
440
    <div class="col-sm-10">
441
      <input type="email" class="form-control" id="colFormLabel" placeholder="col-form-label">
442
    </div>
443
  </div>
444
  <div class="form-group row">
445
    <label for="colFormLabelLg" class="col-sm-2 col-form-label col-form-label-lg">Email</label>
446
    <div class="col-sm-10">
447
      <input type="email" class="form-control form-control-lg" id="colFormLabelLg" placeholder="col-form-label-lg">
448
    </div>
449
  </div>
450
</form>
451
{{< /example >}}
452
 
453
#### Column sizing
454
 
455
As shown in the previous examples, our grid system allows you to place any number of `.col`s within a `.row` or `.form-row`. They'll split the available width equally between them. You may also pick a subset of your columns to take up more or less space, while the remaining `.col`s equally split the rest, with specific column classes like `.col-7`.
456
 
457
{{< example >}}
458
<form>
459
  <div class="form-row">
460
    <div class="col-7">
461
      <input type="text" class="form-control" placeholder="City">
462
    </div>
463
    <div class="col">
464
      <input type="text" class="form-control" placeholder="State">
465
    </div>
466
    <div class="col">
467
      <input type="text" class="form-control" placeholder="Zip">
468
    </div>
469
  </div>
470
</form>
471
{{< /example >}}
472
 
473
#### Auto-sizing
474
 
475
The example below uses a flexbox utility to vertically center the contents and changes `.col` to `.col-auto` so that your columns only take up as much space as needed. Put another way, the column sizes itself based on the contents.
476
 
477
{{< example >}}
478
<form>
479
  <div class="form-row align-items-center">
480
    <div class="col-auto">
481
      <label class="sr-only" for="inlineFormInput">Name</label>
482
      <input type="text" class="form-control mb-2" id="inlineFormInput" placeholder="Jane Doe">
483
    </div>
484
    <div class="col-auto">
485
      <label class="sr-only" for="inlineFormInputGroup">Username</label>
486
      <div class="input-group mb-2">
487
        <div class="input-group-prepend">
488
          <div class="input-group-text">@</div>
489
        </div>
490
        <input type="text" class="form-control" id="inlineFormInputGroup" placeholder="Username">
491
      </div>
492
    </div>
493
    <div class="col-auto">
494
      <div class="form-check mb-2">
495
        <input class="form-check-input" type="checkbox" id="autoSizingCheck">
496
        <label class="form-check-label" for="autoSizingCheck">
497
          Remember me
498
        </label>
499
      </div>
500
    </div>
501
    <div class="col-auto">
502
      <button type="submit" class="btn btn-primary mb-2">Submit</button>
503
    </div>
504
  </div>
505
</form>
506
{{< /example >}}
507
 
508
You can then remix that once again with size-specific column classes.
509
 
510
{{< example >}}
511
<form>
512
  <div class="form-row align-items-center">
513
    <div class="col-sm-3 my-1">
514
      <label class="sr-only" for="inlineFormInputName">Name</label>
515
      <input type="text" class="form-control" id="inlineFormInputName" placeholder="Jane Doe">
516
    </div>
517
    <div class="col-sm-3 my-1">
518
      <label class="sr-only" for="inlineFormInputGroupUsername">Username</label>
519
      <div class="input-group">
520
        <div class="input-group-prepend">
521
          <div class="input-group-text">@</div>
522
        </div>
523
        <input type="text" class="form-control" id="inlineFormInputGroupUsername" placeholder="Username">
524
      </div>
525
    </div>
526
    <div class="col-auto my-1">
527
      <div class="form-check">
528
        <input class="form-check-input" type="checkbox" id="autoSizingCheck2">
529
        <label class="form-check-label" for="autoSizingCheck2">
530
          Remember me
531
        </label>
532
      </div>
533
    </div>
534
    <div class="col-auto my-1">
535
      <button type="submit" class="btn btn-primary">Submit</button>
536
    </div>
537
  </div>
538
</form>
539
{{< /example >}}
540
 
541
And of course [custom form controls](#custom-forms) are supported.
542
 
543
{{< example >}}
544
<form>
545
  <div class="form-row align-items-center">
546
    <div class="col-auto my-1">
547
      <label class="mr-sm-2 sr-only" for="inlineFormCustomSelect">Preference</label>
548
      <select class="custom-select mr-sm-2" id="inlineFormCustomSelect">
549
        <option selected>Choose...</option>
550
        <option value="1">One</option>
551
        <option value="2">Two</option>
552
        <option value="3">Three</option>
553
      </select>
554
    </div>
555
    <div class="col-auto my-1">
556
      <div class="custom-control custom-checkbox mr-sm-2">
557
        <input type="checkbox" class="custom-control-input" id="customControlAutosizing">
558
        <label class="custom-control-label" for="customControlAutosizing">Remember my preference</label>
559
      </div>
560
    </div>
561
    <div class="col-auto my-1">
562
      <button type="submit" class="btn btn-primary">Submit</button>
563
    </div>
564
  </div>
565
</form>
566
{{< /example >}}
567
 
568
### Inline forms
569
 
570
Use the `.form-inline` class to display a series of labels, form controls, and buttons on a single horizontal row. Form controls within inline forms vary slightly from their default states.
571
 
572
- Controls are `display: flex`, collapsing any HTML white space and allowing you to provide alignment control with [spacing]({{< docsref "/utilities/spacing" >}}) and [flexbox]({{< docsref "/utilities/flex" >}}) utilities.
573
- Controls and input groups receive `width: auto` to override the Bootstrap default `width: 100%`.
574
- Controls **only appear inline in viewports that are at least 576px wide** to account for narrow viewports on mobile devices.
575
 
576
You may need to manually address the width and alignment of individual form controls with [spacing utilities]({{< docsref "/utilities/spacing" >}}) (as shown below). Lastly, be sure to always include a `<label>` with each form control, even if you need to hide it from non-screenreader visitors with `.sr-only`.
577
 
578
{{< example >}}
579
<form class="form-inline">
580
  <label class="sr-only" for="inlineFormInputName2">Name</label>
581
  <input type="text" class="form-control mb-2 mr-sm-2" id="inlineFormInputName2" placeholder="Jane Doe">
582
 
583
  <label class="sr-only" for="inlineFormInputGroupUsername2">Username</label>
584
  <div class="input-group mb-2 mr-sm-2">
585
    <div class="input-group-prepend">
586
      <div class="input-group-text">@</div>
587
    </div>
588
    <input type="text" class="form-control" id="inlineFormInputGroupUsername2" placeholder="Username">
589
  </div>
590
 
591
  <div class="form-check mb-2 mr-sm-2">
592
    <input class="form-check-input" type="checkbox" id="inlineFormCheck">
593
    <label class="form-check-label" for="inlineFormCheck">
594
      Remember me
595
    </label>
596
  </div>
597
 
598
  <button type="submit" class="btn btn-primary mb-2">Submit</button>
599
</form>
600
{{< /example >}}
601
 
602
Custom form controls and selects are also supported.
603
 
604
{{< example >}}
605
<form class="form-inline">
606
  <label class="my-1 mr-2" for="inlineFormCustomSelectPref">Preference</label>
607
  <select class="custom-select my-1 mr-sm-2" id="inlineFormCustomSelectPref">
608
    <option selected>Choose...</option>
609
    <option value="1">One</option>
610
    <option value="2">Two</option>
611
    <option value="3">Three</option>
612
  </select>
613
 
614
  <div class="custom-control custom-checkbox my-1 mr-sm-2">
615
    <input type="checkbox" class="custom-control-input" id="customControlInline">
616
    <label class="custom-control-label" for="customControlInline">Remember my preference</label>
617
  </div>
618
 
619
  <button type="submit" class="btn btn-primary my-1">Submit</button>
620
</form>
621
{{< /example >}}
622
 
623
{{< callout warning >}}
624
##### Alternatives to hidden labels
625
Assistive technologies such as screen readers will have trouble with your forms if you don't include a label for every input. For these inline forms, you can hide the labels using the `.sr-only` class. There are further alternative methods of providing a label for assistive technologies, such as the `aria-label`, `aria-labelledby` or `title` attribute. If none of these are present, assistive technologies may resort to using the `placeholder` attribute, if present, but note that use of `placeholder` as a replacement for other labelling methods is not advised.
626
{{< /callout >}}
627
 
628
## Help text
629
 
630
Block-level help text in forms can be created using `.form-text` (previously known as `.help-block` in v3). Inline help text can be flexibly implemented using any inline HTML element and utility classes like `.text-muted`.
631
 
632
{{< callout warning >}}
633
##### Associating help text with form controls
634
 
635
Help text should be explicitly associated with the form control it relates to using the `aria-describedby` attribute. This will ensure that assistive technologies—such as screen readers—will announce this help text when the user focuses or enters the control.
636
{{< /callout >}}
637
 
638
Help text below inputs can be styled with `.form-text`. This class includes `display: block` and adds some top margin for easy spacing from the inputs above.
639
 
640
{{< example >}}
641
<label for="inputPassword5">Password</label>
642
<input type="password" id="inputPassword5" class="form-control" aria-describedby="passwordHelpBlock">
643
<small id="passwordHelpBlock" class="form-text text-muted">
644
  Your password must be 8-20 characters long, contain letters and numbers, and must not contain spaces, special characters, or emoji.
645
</small>
646
{{< /example >}}
647
 
648
Inline text can use any typical inline HTML element (be it a `<small>`, `<span>`, or something else) with nothing more than a utility class.
649
 
650
{{< example >}}
651
<form class="form-inline">
652
  <div class="form-group">
653
    <label for="inputPassword6">Password</label>
654
    <input type="password" id="inputPassword6" class="form-control mx-sm-3" aria-describedby="passwordHelpInline">
655
    <small id="passwordHelpInline" class="text-muted">
656
      Must be 8-20 characters long.
657
    </small>
658
  </div>
659
</form>
660
{{< /example >}}
661
 
662
## Disabled forms
663
 
664
Add the `disabled` boolean attribute on an input to prevent user interactions and make it appear lighter.
665
 
666
```html
667
<input class="form-control" id="disabledInput" type="text" placeholder="Disabled input here..." disabled>
668
```
669
 
670
Add the `disabled` attribute to a `<fieldset>` to disable all the controls within.
671
 
672
{{< example >}}
673
<form>
674
  <fieldset disabled>
675
    <legend>Disabled fieldset example</legend>
676
    <div class="form-group">
677
      <label for="disabledTextInput">Disabled input</label>
678
      <input type="text" id="disabledTextInput" class="form-control" placeholder="Disabled input">
679
    </div>
680
    <div class="form-group">
681
      <label for="disabledSelect">Disabled select menu</label>
682
      <select id="disabledSelect" class="form-control">
683
        <option>Disabled select</option>
684
      </select>
685
    </div>
686
    <div class="form-group">
687
      <div class="form-check">
688
        <input class="form-check-input" type="checkbox" id="disabledFieldsetCheck" disabled>
689
        <label class="form-check-label" for="disabledFieldsetCheck">
690
          Can't check this
691
        </label>
692
      </div>
693
    </div>
694
    <button type="submit" class="btn btn-primary">Submit</button>
695
  </fieldset>
696
</form>
697
{{< /example >}}
698
 
699
{{< callout warning >}}
700
##### Caveat with anchors
701
 
702
Browsers treat all native form controls (`<input>`, `<select>`, and `<button>` elements) inside a `<fieldset disabled>` as disabled, preventing both keyboard and mouse interactions on them.
703
 
704
However, if your form also includes custom button-like elements such as `<a ... class="btn btn-*">`, these will only be given a style of `pointer-events: none`. As noted in the section about [disabled state for buttons]({{< docsref "/components/buttons#disabled-state" >}}) (and specifically in the sub-section for anchor elements), this CSS property is not yet standardized and isn't fully supported in Internet Explorer 10. The anchor-based controls will also still be focusable and operable using the keyboard. You must manually modify these controls by adding `tabindex="-1"` to prevent them from receiving focus and `aria-disabled="disabled"` to signal their state to assistive technologies.
705
{{< /callout >}}
706
 
707
{{< callout danger >}}
708
#### Cross-browser compatibility
709
 
710
While Bootstrap will apply these styles in all browsers, Internet Explorer 11 and below don't fully support the `disabled` attribute on a `<fieldset>`. Use custom JavaScript to disable the fieldset in these browsers.
711
{{< /callout >}}
712
 
713
## Validation
714
 
715
Provide valuable, actionable feedback to your users with HTML5 form validation–[available in all our supported browsers](https://caniuse.com/form-validation). Choose from the browser default validation feedback, or implement custom messages with our built-in classes and starter JavaScript.
716
 
717
{{< callout warning >}}
718
We are aware that currently the client-side custom validation styles and tooltips are not accessible, since they are not exposed to assistive technologies. While we work on a solution, we'd recommend either using the server-side option or the default browser validation method.
719
{{< /callout >}}
720
 
721
### How it works
722
 
723
Here's how form validation works with Bootstrap:
724
 
725
- HTML form validation is applied via CSS's two pseudo-classes, `:invalid` and `:valid`. It applies to `<input>`, `<select>`, and `<textarea>` elements.
726
- Bootstrap scopes the `:invalid` and `:valid` styles to parent `.was-validated` class, usually applied to the `<form>`. Otherwise, any required field without a value shows up as invalid on page load. This way, you may choose when to activate them (typically after form submission is attempted).
727
- To reset the appearance of the form (for instance, in the case of dynamic form submissions using AJAX), remove the `.was-validated` class from the `<form>` again after submission.
728
- As a fallback, `.is-invalid` and `.is-valid` classes may be used instead of the pseudo-classes for [server side validation](#server-side). They do not require a `.was-validated` parent class.
729
- Due to constraints in how CSS works, we cannot (at present) apply styles to a `<label>` that comes before a form control in the DOM without the help of custom JavaScript.
730
- All modern browsers support the [constraint validation API](https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#the-constraint-validation-api), a series of JavaScript methods for validating form controls.
731
- Feedback messages may utilize the [browser defaults](#browser-defaults) (different for each browser, and unstylable via CSS) or our custom feedback styles with additional HTML and CSS.
732
- You may provide custom validity messages with `setCustomValidity` in JavaScript.
733
 
734
With that in mind, consider the following demos for our custom form validation styles, optional server side classes, and browser defaults.
735
 
736
### Custom styles
737
 
738
For custom Bootstrap form validation messages, you'll need to add the `novalidate` boolean attribute to your `<form>`. This disables the browser default feedback tooltips, but still provides access to the form validation APIs in JavaScript. Try to submit the form below; our JavaScript will intercept the submit button and relay feedback to you. When attempting to submit, you'll see the `:invalid` and `:valid` styles applied to your form controls.
739
 
740
Custom feedback styles apply custom colors, borders, focus styles, and background icons to better communicate feedback. Background icons for `<select>`s are only available with `.custom-select`, and not `.form-control`.
741
 
742
{{< example >}}
743
<form class="needs-validation" novalidate>
744
  <div class="form-row">
745
    <div class="col-md-6 mb-3">
746
      <label for="validationCustom01">First name</label>
747
      <input type="text" class="form-control" id="validationCustom01" value="Mark" required>
748
      <div class="valid-feedback">
749
        Looks good!
750
      </div>
751
    </div>
752
    <div class="col-md-6 mb-3">
753
      <label for="validationCustom02">Last name</label>
754
      <input type="text" class="form-control" id="validationCustom02" value="Otto" required>
755
      <div class="valid-feedback">
756
        Looks good!
757
      </div>
758
    </div>
759
  </div>
760
  <div class="form-row">
761
    <div class="col-md-6 mb-3">
762
      <label for="validationCustom03">City</label>
763
      <input type="text" class="form-control" id="validationCustom03" required>
764
      <div class="invalid-feedback">
765
        Please provide a valid city.
766
      </div>
767
    </div>
768
    <div class="col-md-3 mb-3">
769
      <label for="validationCustom04">State</label>
770
      <select class="custom-select" id="validationCustom04" required>
771
        <option selected disabled value="">Choose...</option>
772
        <option>...</option>
773
      </select>
774
      <div class="invalid-feedback">
775
        Please select a valid state.
776
      </div>
777
    </div>
778
    <div class="col-md-3 mb-3">
779
      <label for="validationCustom05">Zip</label>
780
      <input type="text" class="form-control" id="validationCustom05" required>
781
      <div class="invalid-feedback">
782
        Please provide a valid zip.
783
      </div>
784
    </div>
785
  </div>
786
  <div class="form-group">
787
    <div class="form-check">
788
      <input class="form-check-input" type="checkbox" value="" id="invalidCheck" required>
789
      <label class="form-check-label" for="invalidCheck">
790
        Agree to terms and conditions
791
      </label>
792
      <div class="invalid-feedback">
793
        You must agree before submitting.
794
      </div>
795
    </div>
796
  </div>
797
  <button class="btn btn-primary" type="submit">Submit form</button>
798
</form>
799
 
800
<script>
801
// Example starter JavaScript for disabling form submissions if there are invalid fields
802
(function() {
803
  'use strict';
804
  window.addEventListener('load', function() {
805
    // Fetch all the forms we want to apply custom Bootstrap validation styles to
806
    var forms = document.getElementsByClassName('needs-validation');
807
    // Loop over them and prevent submission
808
    var validation = Array.prototype.filter.call(forms, function(form) {
809
      form.addEventListener('submit', function(event) {
810
        if (form.checkValidity() === false) {
811
          event.preventDefault();
812
          event.stopPropagation();
813
        }
814
        form.classList.add('was-validated');
815
      }, false);
816
    });
817
  }, false);
818
})();
819
</script>
820
{{< /example >}}
821
 
822
### Browser defaults
823
 
824
Not interested in custom validation feedback messages or writing JavaScript to change form behaviors? All good, you can use the browser defaults. Try submitting the form below. Depending on your browser and OS, you'll see a slightly different style of feedback.
825
 
826
While these feedback styles cannot be styled with CSS, you can still customize the feedback text through JavaScript.
827
 
828
{{< example >}}
829
<form>
830
  <div class="form-row">
831
    <div class="col-md-6 mb-3">
832
      <label for="validationDefault01">First name</label>
833
      <input type="text" class="form-control" id="validationDefault01" value="Mark" required>
834
    </div>
835
    <div class="col-md-6 mb-3">
836
      <label for="validationDefault02">Last name</label>
837
      <input type="text" class="form-control" id="validationDefault02" value="Otto" required>
838
    </div>
839
  </div>
840
  <div class="form-row">
841
    <div class="col-md-6 mb-3">
842
      <label for="validationDefault03">City</label>
843
      <input type="text" class="form-control" id="validationDefault03" required>
844
    </div>
845
    <div class="col-md-3 mb-3">
846
      <label for="validationDefault04">State</label>
847
      <select class="custom-select" id="validationDefault04" required>
848
        <option selected disabled value="">Choose...</option>
849
        <option>...</option>
850
      </select>
851
    </div>
852
    <div class="col-md-3 mb-3">
853
      <label for="validationDefault05">Zip</label>
854
      <input type="text" class="form-control" id="validationDefault05" required>
855
    </div>
856
  </div>
857
  <div class="form-group">
858
    <div class="form-check">
859
      <input class="form-check-input" type="checkbox" value="" id="invalidCheck2" required>
860
      <label class="form-check-label" for="invalidCheck2">
861
        Agree to terms and conditions
862
      </label>
863
    </div>
864
  </div>
865
  <button class="btn btn-primary" type="submit">Submit form</button>
866
</form>
867
{{< /example >}}
868
 
869
### Server side
870
 
871
We recommend using client-side validation, but in case you require server-side validation, you can indicate invalid and valid form fields with `.is-invalid` and `.is-valid`. Note that `.invalid-feedback` is also supported with these classes.
872
 
873
For invalid fields, ensure that the invalid feedback/error message is associated with the relevant form field using `aria-describedby`. This attribute allows more than one `id` to be referenced, in case the field already points to additional form text.
874
 
875
{{< example >}}
876
<form>
877
  <div class="form-row">
878
    <div class="col-md-6 mb-3">
879
      <label for="validationServer01">First name</label>
880
      <input type="text" class="form-control is-valid" id="validationServer01" value="Mark" required>
881
      <div class="valid-feedback">
882
        Looks good!
883
      </div>
884
    </div>
885
    <div class="col-md-6 mb-3">
886
      <label for="validationServer02">Last name</label>
887
      <input type="text" class="form-control is-valid" id="validationServer02" value="Otto" required>
888
      <div class="valid-feedback">
889
        Looks good!
890
      </div>
891
    </div>
892
  </div>
893
  <div class="form-row">
894
    <div class="col-md-6 mb-3">
895
      <label for="validationServer03">City</label>
896
      <input type="text" class="form-control is-invalid" id="validationServer03" aria-describedby="validationServer03Feedback" required>
897
      <div id="validationServer03Feedback" class="invalid-feedback">
898
        Please provide a valid city.
899
      </div>
900
    </div>
901
    <div class="col-md-3 mb-3">
902
      <label for="validationServer04">State</label>
903
      <select class="custom-select is-invalid" id="validationServer04" aria-describedby="validationServer04Feedback" required>
904
        <option selected disabled value="">Choose...</option>
905
        <option>...</option>
906
      </select>
907
      <div id="validationServer04Feedback" class="invalid-feedback">
908
        Please select a valid state.
909
      </div>
910
    </div>
911
    <div class="col-md-3 mb-3">
912
      <label for="validationServer05">Zip</label>
913
      <input type="text" class="form-control is-invalid" id="validationServer05" aria-describedby="validationServer05Feedback" required>
914
      <div id="validationServer05Feedback" class="invalid-feedback">
915
        Please provide a valid zip.
916
      </div>
917
    </div>
918
  </div>
919
  <div class="form-group">
920
    <div class="form-check">
921
      <input class="form-check-input is-invalid" type="checkbox" value="" id="invalidCheck3" aria-describedby="invalidCheck3Feedback" required>
922
      <label class="form-check-label" for="invalidCheck3">
923
        Agree to terms and conditions
924
      </label>
925
      <div  id="invalidCheck3Feedback" class="invalid-feedback">
926
        You must agree before submitting.
927
      </div>
928
    </div>
929
  </div>
930
  <button class="btn btn-primary" type="submit">Submit form</button>
931
</form>
932
{{< /example >}}
933
 
934
### Supported elements
935
 
936
Validation styles are available for the following form controls and components:
937
 
938
- `<input>`s and `<textarea>`s with `.form-control`
939
- `<select>`s with `.form-control` or `.custom-select`
940
- `.form-check`s
941
- `.custom-checkbox`s and `.custom-radio`s
942
- `.custom-file`
943
 
944
{{< example >}}
945
<form class="was-validated">
946
  <div class="mb-3">
947
    <label for="validationTextarea">Textarea</label>
948
    <textarea class="form-control is-invalid" id="validationTextarea" placeholder="Required example textarea" required></textarea>
949
    <div class="invalid-feedback">
950
      Please enter a message in the textarea.
951
    </div>
952
  </div>
953
 
954
  <div class="custom-control custom-checkbox mb-3">
955
    <input type="checkbox" class="custom-control-input" id="customControlValidation1" required>
956
    <label class="custom-control-label" for="customControlValidation1">Check this custom checkbox</label>
957
    <div class="invalid-feedback">Example invalid feedback text</div>
958
  </div>
959
 
960
  <div class="custom-control custom-radio">
961
    <input type="radio" class="custom-control-input" id="customControlValidation2" name="radio-stacked" required>
962
    <label class="custom-control-label" for="customControlValidation2">Toggle this custom radio</label>
963
  </div>
964
  <div class="custom-control custom-radio mb-3">
965
    <input type="radio" class="custom-control-input" id="customControlValidation3" name="radio-stacked" required>
966
    <label class="custom-control-label" for="customControlValidation3">Or toggle this other custom radio</label>
967
    <div class="invalid-feedback">More example invalid feedback text</div>
968
  </div>
969
 
970
  <div class="mb-3">
971
    <select class="custom-select" required>
972
      <option value="">Choose...</option>
973
      <option value="1">One</option>
974
      <option value="2">Two</option>
975
      <option value="3">Three</option>
976
    </select>
977
    <div class="invalid-feedback">Example invalid custom select feedback</div>
978
  </div>
979
 
980
  <div class="custom-file mb-3">
981
    <input type="file" class="custom-file-input" id="validatedCustomFile" required>
982
    <label class="custom-file-label" for="validatedCustomFile">Choose file...</label>
983
    <div class="invalid-feedback">Example invalid custom file feedback</div>
984
  </div>
985
 
986
  <div class="mb-3">
987
    <div class="input-group is-invalid">
988
      <div class="input-group-prepend">
989
        <span class="input-group-text" id="validatedInputGroupPrepend">@</span>
990
      </div>
991
      <input type="text" class="form-control is-invalid" aria-describedby="validatedInputGroupPrepend" required>
992
    </div>
993
    <div class="invalid-feedback">
994
      Example invalid input group feedback
995
    </div>
996
  </div>
997
 
998
  <div class="mb-3">
999
    <div class="input-group is-invalid">
1000
      <div class="input-group-prepend">
1001
        <label class="input-group-text" for="validatedInputGroupSelect">Options</label>
1002
      </div>
1003
      <select class="custom-select" id="validatedInputGroupSelect" required>
1004
        <option value="">Choose...</option>
1005
        <option value="1">One</option>
1006
        <option value="2">Two</option>
1007
        <option value="3">Three</option>
1008
      </select>
1009
    </div>
1010
    <div class="invalid-feedback">
1011
      Example invalid input group feedback
1012
    </div>
1013
  </div>
1014
 
1015
  <div class="input-group is-invalid">
1016
    <div class="custom-file">
1017
      <input type="file" class="custom-file-input" id="validatedInputGroupCustomFile" required>
1018
      <label class="custom-file-label" for="validatedInputGroupCustomFile">Choose file...</label>
1019
    </div>
1020
    <div class="input-group-append">
1021
       <button class="btn btn-outline-secondary" type="button">Button</button>
1022
    </div>
1023
  </div>
1024
  <div class="invalid-feedback">
1025
    Example invalid input group feedback
1026
  </div>
1027
</form>
1028
{{< /example >}}
1029
 
1030
### Tooltips
1031
 
1032
If your form layout allows it, you can swap the `.{valid|invalid}-feedback` classes for `.{valid|invalid}-tooltip` classes to display validation feedback in a styled tooltip. Be sure to have a parent with `position: relative` on it for tooltip positioning. In the example below, our column classes have this already, but your project may require an alternative setup.
1033
 
1034
{{< example >}}
1035
<form class="needs-validation" novalidate>
1036
  <div class="form-row">
1037
    <div class="col-md-6 mb-3">
1038
      <label for="validationTooltip01">First name</label>
1039
      <input type="text" class="form-control" id="validationTooltip01" value="Mark" required>
1040
      <div class="valid-tooltip">
1041
        Looks good!
1042
      </div>
1043
    </div>
1044
    <div class="col-md-6 mb-3">
1045
      <label for="validationTooltip02">Last name</label>
1046
      <input type="text" class="form-control" id="validationTooltip02" value="Otto" required>
1047
      <div class="valid-tooltip">
1048
        Looks good!
1049
      </div>
1050
    </div>
1051
  </div>
1052
  <div class="form-row">
1053
    <div class="col-md-6 mb-3">
1054
      <label for="validationTooltip03">City</label>
1055
      <input type="text" class="form-control" id="validationTooltip03" required>
1056
      <div class="invalid-tooltip">
1057
        Please provide a valid city.
1058
      </div>
1059
    </div>
1060
    <div class="col-md-3 mb-3">
1061
      <label for="validationTooltip04">State</label>
1062
      <select class="custom-select" id="validationTooltip04" required>
1063
        <option selected disabled value="">Choose...</option>
1064
        <option>...</option>
1065
      </select>
1066
      <div class="invalid-tooltip">
1067
        Please select a valid state.
1068
      </div>
1069
    </div>
1070
    <div class="col-md-3 mb-3">
1071
      <label for="validationTooltip05">Zip</label>
1072
      <input type="text" class="form-control" id="validationTooltip05" required>
1073
      <div class="invalid-tooltip">
1074
        Please provide a valid zip.
1075
      </div>
1076
    </div>
1077
  </div>
1078
  <button class="btn btn-primary" type="submit">Submit form</button>
1079
</form>
1080
{{< /example >}}
1081
 
1082
### Customizing
1083
 
1084
Validation states can be customized via Sass with the `$form-validation-states` map. Located in our `_variables.scss` file, this Sass map is looped over to generate the default `valid`/`invalid` validation states. Included is a nested map for customizing each state's color and icon. While no other states are supported by browsers, those using custom styles can easily add more complex form feedback.
1085
 
1086
Please note that we do not recommend customizing these values without also modifying the `form-validation-state` mixin.
1087
 
1088
```scss
1089
// Sass map from `_variables.scss`
1090
// Override this and recompile your Sass to generate different states
1091
$form-validation-states: map-merge(
1092
  (
1093
    "valid": (
1094
      "color": $form-feedback-valid-color,
1095
      "icon": $form-feedback-icon-valid
1096
    ),
1097
    "invalid": (
1098
      "color": $form-feedback-invalid-color,
1099
      "icon": $form-feedback-icon-invalid
1100
    )
1101
  ),
1102
  $form-validation-states
1103
);
1104
 
1105
// Loop from `_forms.scss`
1106
// Any modifications to the above Sass map will be reflected in your compiled
1107
// CSS via this loop.
1108
@each $state, $data in $form-validation-states {
1109
  @include form-validation-state($state, map-get($data, color), map-get($data, icon));
1110
}
1111
```
1112
 
1113
### Input group validation
1114
 
1115
To detect what elements need rounded corners inside an input group with validation, an input group requires an additional `.has-validation` class.
1116
 
1117
```html
1118
<div class="input-group has-validation">
1119
  <div class="input-group-prepend">
1120
    <span class="input-group-text">@</span>
1121
  </div>
1122
  <input type="text" class="form-control" required>
1123
  <div class="invalid-feedback">
1124
    Please choose a username.
1125
  </div>
1126
</div>
1127
```
1128
 
1129
<div class="bd-example bd-example-forms-input-group-workaround">
1130
  <div class="input-group has-validation">
1131
    <div class="input-group-prepend">
1132
      <span class="input-group-text">@</span>
1133
    </div>
1134
    <input type="text" class="form-control is-invalid" required>
1135
    <div class="invalid-feedback">
1136
      Please choose a username.
1137
    </div>
1138
  </div>
1139
</div>
1140
 
1141
## Custom forms
1142
 
1143
For even more customization and cross browser consistency, use our completely custom form elements to replace the browser defaults. They're built on top of semantic and accessible markup, so they're solid replacements for any default form control.
1144
 
1145
### Checkboxes and radios
1146
 
1147
Each checkbox and radio `<input>` and `<label>` pairing is wrapped in a `<div>` to create our custom control. Structurally, this is the same approach as our default `.form-check`.
1148
 
1149
We use the sibling selector (`~`) for all our `<input>` states—like `:checked`—to properly style our custom form indicator. When combined with the `.custom-control-label` class, we can also style the text for each item based on the `<input>`'s state.
1150
 
1151
We hide the default `<input>` with `opacity` and use the `.custom-control-label` to build a new custom form indicator in its place with `::before` and `::after`. Unfortunately we can't build a custom one from just the `<input>` because CSS's `content` doesn't work on that element.
1152
 
1153
In the checked states, we use **base64 embedded SVG icons** from [Open Iconic](https://github.com/iconic/open-iconic). This provides us the best control for styling and positioning across browsers and devices.
1154
 
1155
#### Checkboxes
1156
 
1157
{{< example >}}
1158
<div class="custom-control custom-checkbox">
1159
  <input type="checkbox" class="custom-control-input" id="customCheck1">
1160
  <label class="custom-control-label" for="customCheck1">Check this custom checkbox</label>
1161
</div>
1162
{{< /example >}}
1163
 
1164
Custom checkboxes can also utilize the `:indeterminate` pseudo class when manually set via JavaScript (there is no available HTML attribute for specifying it).
1165
 
1166
<div class="bd-example bd-example-indeterminate">
1167
  <div class="custom-control custom-checkbox">
1168
    <input type="checkbox" class="custom-control-input" id="customCheck2">
1169
    <label class="custom-control-label" for="customCheck2">Check this custom checkbox</label>
1170
  </div>
1171
</div>
1172
 
1173
If you're using jQuery, something like this should suffice:
1174
 
1175
```js
1176
$('.your-checkbox').prop('indeterminate', true)
1177
```
1178
 
1179
#### Radios
1180
 
1181
{{< example >}}
1182
<div class="custom-control custom-radio">
1183
  <input type="radio" id="customRadio1" name="customRadio" class="custom-control-input">
1184
  <label class="custom-control-label" for="customRadio1">Toggle this custom radio</label>
1185
</div>
1186
<div class="custom-control custom-radio">
1187
  <input type="radio" id="customRadio2" name="customRadio" class="custom-control-input">
1188
  <label class="custom-control-label" for="customRadio2">Or toggle this other custom radio</label>
1189
</div>
1190
{{< /example >}}
1191
 
1192
#### Inline
1193
 
1194
{{< example >}}
1195
<div class="custom-control custom-radio custom-control-inline">
1196
  <input type="radio" id="customRadioInline1" name="customRadioInline" class="custom-control-input">
1197
  <label class="custom-control-label" for="customRadioInline1">Toggle this custom radio</label>
1198
</div>
1199
<div class="custom-control custom-radio custom-control-inline">
1200
  <input type="radio" id="customRadioInline2" name="customRadioInline" class="custom-control-input">
1201
  <label class="custom-control-label" for="customRadioInline2">Or toggle this other custom radio</label>
1202
</div>
1203
{{< /example >}}
1204
 
1205
#### Disabled
1206
 
1207
Custom checkboxes and radios can also be disabled. Add the `disabled` boolean attribute to the `<input>` and the custom indicator and label description will be automatically styled.
1208
 
1209
{{< example >}}
1210
<div class="custom-control custom-checkbox">
1211
  <input type="checkbox" class="custom-control-input" id="customCheckDisabled1" disabled>
1212
  <label class="custom-control-label" for="customCheckDisabled1">Check this custom checkbox</label>
1213
</div>
1214
 
1215
<div class="custom-control custom-radio">
1216
  <input type="radio" name="radioDisabled" id="customRadioDisabled2" class="custom-control-input" disabled>
1217
  <label class="custom-control-label" for="customRadioDisabled2">Toggle this custom radio</label>
1218
</div>
1219
{{< /example >}}
1220
 
1221
### Switches
1222
 
1223
A switch has the markup of a custom checkbox but uses the `.custom-switch` class to render a toggle switch. Switches also support the `disabled` attribute.
1224
 
1225
{{< example >}}
1226
<div class="custom-control custom-switch">
1227
  <input type="checkbox" class="custom-control-input" id="customSwitch1">
1228
  <label class="custom-control-label" for="customSwitch1">Toggle this switch element</label>
1229
</div>
1230
<div class="custom-control custom-switch">
1231
  <input type="checkbox" class="custom-control-input" disabled id="customSwitch2">
1232
  <label class="custom-control-label" for="customSwitch2">Disabled switch element</label>
1233
</div>
1234
{{< /example >}}
1235
 
1236
### Select menu
1237
 
1238
Custom `<select>` menus need only a custom class, `.custom-select` to trigger the custom styles. Custom styles are limited to the `<select>`'s initial appearance and cannot modify the `<option>`s due to browser limitations.
1239
 
1240
{{< example >}}
1241
<select class="custom-select">
1242
  <option selected>Open this select menu</option>
1243
  <option value="1">One</option>
1244
  <option value="2">Two</option>
1245
  <option value="3">Three</option>
1246
</select>
1247
{{< /example >}}
1248
 
1249
You may also choose from small and large custom selects to match our similarly sized text inputs.
1250
 
1251
{{< example >}}
1252
<select class="custom-select custom-select-lg mb-3">
1253
  <option selected>Open this select menu</option>
1254
  <option value="1">One</option>
1255
  <option value="2">Two</option>
1256
  <option value="3">Three</option>
1257
</select>
1258
 
1259
<select class="custom-select custom-select-sm">
1260
  <option selected>Open this select menu</option>
1261
  <option value="1">One</option>
1262
  <option value="2">Two</option>
1263
  <option value="3">Three</option>
1264
</select>
1265
{{< /example >}}
1266
 
1267
The `multiple` attribute is also supported:
1268
 
1269
{{< example >}}
1270
<select class="custom-select" multiple>
1271
  <option selected>Open this select menu</option>
1272
  <option value="1">One</option>
1273
  <option value="2">Two</option>
1274
  <option value="3">Three</option>
1275
</select>
1276
{{< /example >}}
1277
 
1278
As is the `size` attribute:
1279
 
1280
{{< example >}}
1281
<select class="custom-select" size="3">
1282
  <option selected>Open this select menu</option>
1283
  <option value="1">One</option>
1284
  <option value="2">Two</option>
1285
  <option value="3">Three</option>
1286
</select>
1287
{{< /example >}}
1288
 
1289
### Range
1290
 
1291
Create custom `<input type="range">` controls with `.custom-range`. The track (the background) and thumb (the value) are both styled to appear the same across browsers. As only IE and Firefox support "filling" their track from the left or right of the thumb as a means to visually indicate progress, we do not currently support it.
1292
 
1293
{{< example >}}
1294
<label for="customRange1">Example range</label>
1295
<input type="range" class="custom-range" id="customRange1">
1296
{{< /example >}}
1297
 
1298
Range inputs have implicit values for `min` and `max`—`0` and `100`, respectively. You may specify new values for those using the `min` and `max` attributes.
1299
 
1300
{{< example >}}
1301
<label for="customRange2">Example range</label>
1302
<input type="range" class="custom-range" min="0" max="5" id="customRange2">
1303
{{< /example >}}
1304
 
1305
By default, range inputs "snap" to integer values. To change this, you can specify a `step` value. In the example below, we double the number of steps by using `step="0.5"`.
1306
 
1307
{{< example >}}
1308
<label for="customRange3">Example range</label>
1309
<input type="range" class="custom-range" min="0" max="5" step="0.5" id="customRange3">
1310
{{< /example >}}
1311
 
1312
### File browser
1313
 
1314
{{< callout info >}}
1315
The recommended plugin to animate custom file input: [bs-custom-file-input](https://www.npmjs.com/package/bs-custom-file-input), that's what we are using currently here in our docs.
1316
{{< /callout >}}
1317
 
1318
The file input is the most gnarly of the bunch and requires additional JavaScript if you'd like to hook them up with functional *Choose file...* and selected file name text.
1319
 
1320
{{< example >}}
1321
<div class="custom-file">
1322
  <input type="file" class="custom-file-input" id="customFile">
1323
  <label class="custom-file-label" for="customFile">Choose file</label>
1324
</div>
1325
{{< /example >}}
1326
 
1327
We hide the default file `<input>` via `opacity` and instead style the `<label>`. The button is generated and positioned with `::after`. Lastly, we declare a `width` and `height` on the `<input>` for proper spacing for surrounding content.
1328
 
1329
#### Translating or customizing the strings with SCSS
1330
 
1331
The [`:lang()` pseudo-class](https://developer.mozilla.org/en-US/docs/Web/CSS/:lang) is used to allow for translation of the "Browse" text into other languages. Override or add entries to the `$custom-file-text` Sass variable with the relevant [language tag](https://en.wikipedia.org/wiki/IETF_language_tag) and localized strings. The English strings can be customized the same way. For example, here's how one might add a Spanish translation (Spanish's language code is `es`):
1332
 
1333
```scss
1334
$custom-file-text: (
1335
  en: "Browse",
1336
  es: "Elegir"
1337
);
1338
```
1339
 
1340
Here's `lang(es)` in action on the custom file input for a Spanish translation:
1341
 
1342
{{< example >}}
1343
<div class="custom-file">
1344
  <input type="file" class="custom-file-input" id="customFileLang" lang="es">
1345
  <label class="custom-file-label" for="customFileLang">Seleccionar Archivo</label>
1346
</div>
1347
{{< /example >}}
1348
 
1349
You'll need to set the language of your document (or subtree thereof) correctly in order for the correct text to be shown. This can be done using [the `lang` attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/lang) on the `<html>` element or the [`Content-Language` HTTP header](https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.12), among other methods.
1350
 
1351
#### Translating or customizing the strings with HTML
1352
 
1353
Bootstrap also provides a way to translate the "Browse" text in HTML with the `data-browse` attribute which can be added to the custom input label (example in Dutch):
1354
 
1355
{{< example >}}
1356
<div class="custom-file">
1357
  <input type="file" class="custom-file-input" id="customFileLangHTML">
1358
  <label class="custom-file-label" for="customFileLangHTML" data-browse="Bestand kiezen">Voeg je document toe</label>
1359
</div>
1360
{{< /example >}}