]> git.ipfire.org Git - ipfire.org.git/blame - src/scss/bootstrap-4.0.0-alpha.6/docs/components/forms.md
Introduce autotools
[ipfire.org.git] / src / scss / bootstrap-4.0.0-alpha.6 / docs / components / forms.md
CommitLineData
91e44d91
S
1---
2layout: docs
3title: Forms
4description: Examples and usage guidelines for form controls, form layouts, and custom forms.
5group: components
6---
7
8Bootstrap provides several form control styles, layout options, and custom components for creating a wide variety of forms.
9
10## Contents
11
12* Will be replaced with the ToC, excluding the "Contents" header
13{:toc}
14
15## Form controls
16
17Bootstrap's form controls expand on [our Rebooted form styles]({{ site.baseurl }}/content/reboot/#forms) with classes. Use these classes to opt into their customized displays for a more consistent rendering across browsers and devices. The example form below demonstrates common HTML form elements that receive updated styles from Bootstrap with additional classes.
18
19Remember, since Bootstrap utilizes the HTML5 doctype, **all inputs must have a `type` attribute**.
20
21{% example html %}
22<form>
23 <div class="form-group">
24 <label for="exampleInputEmail1">Email address</label>
25 <input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter email">
26 <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
27 </div>
28 <div class="form-group">
29 <label for="exampleInputPassword1">Password</label>
30 <input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
31 </div>
32 <div class="form-group">
33 <label for="exampleSelect1">Example select</label>
34 <select class="form-control" id="exampleSelect1">
35 <option>1</option>
36 <option>2</option>
37 <option>3</option>
38 <option>4</option>
39 <option>5</option>
40 </select>
41 </div>
42 <div class="form-group">
43 <label for="exampleSelect2">Example multiple select</label>
44 <select multiple class="form-control" id="exampleSelect2">
45 <option>1</option>
46 <option>2</option>
47 <option>3</option>
48 <option>4</option>
49 <option>5</option>
50 </select>
51 </div>
52 <div class="form-group">
53 <label for="exampleTextarea">Example textarea</label>
54 <textarea class="form-control" id="exampleTextarea" rows="3"></textarea>
55 </div>
56 <div class="form-group">
57 <label for="exampleInputFile">File input</label>
58 <input type="file" class="form-control-file" id="exampleInputFile" aria-describedby="fileHelp">
59 <small id="fileHelp" class="form-text text-muted">This is some placeholder block-level help text for the above input. It's a bit lighter and easily wraps to a new line.</small>
60 </div>
61 <fieldset class="form-group">
62 <legend>Radio buttons</legend>
63 <div class="form-check">
64 <label class="form-check-label">
65 <input type="radio" class="form-check-input" name="optionsRadios" id="optionsRadios1" value="option1" checked>
66 Option one is this and that&mdash;be sure to include why it's great
67 </label>
68 </div>
69 <div class="form-check">
70 <label class="form-check-label">
71 <input type="radio" class="form-check-input" name="optionsRadios" id="optionsRadios2" value="option2">
72 Option two can be something else and selecting it will deselect option one
73 </label>
74 </div>
75 <div class="form-check disabled">
76 <label class="form-check-label">
77 <input type="radio" class="form-check-input" name="optionsRadios" id="optionsRadios3" value="option3" disabled>
78 Option three is disabled
79 </label>
80 </div>
81 </fieldset>
82 <div class="form-check">
83 <label class="form-check-label">
84 <input type="checkbox" class="form-check-input">
85 Check me out
86 </label>
87 </div>
88 <button type="submit" class="btn btn-primary">Submit</button>
89</form>
90{% endexample %}
91
92Below is a complete list of the specific form controls supported by Bootstrap and the classes that customize them. Additional documentation is available for each group.
93
94<table>
95 <thead>
96 <tr>
97 <th>Classes</th>
98 <th>Used for</th>
99 <th>Supported variations</th>
100 </tr>
101 </thead>
102 <tbody>
103 <tr>
104 <td>
105 {% markdown %}`.form-group`{% endmarkdown %}
106 </td>
107 <td class="text-nowrap">
108 Any group of form controls
109 </td>
110 <td>
111 {% markdown %}Use with any block-level element like `<fieldset>` or `<div>`{% endmarkdown %}
112 </td>
113 </tr>
114 <tr>
115 <td rowspan="3">
116 {% markdown %}`.form-control`{% endmarkdown %}
117 </td>
118 <td>
119 Textual inputs
120 </td>
121 <td>
122 {% markdown %}`text`, `password`, `datetime-local`, `date`, `month`, `time`, `week`, `number`, `email`, `url`, `search`, `tel`, `color`{% endmarkdown %}
123 </td>
124 </tr>
125 <tr>
126 <td>
127 Select menus
128 </td>
129 <td>
130 {% markdown %}`multiple`, `size`{% endmarkdown %}
131 </td>
132 </tr>
133 <tr>
134 <td>
135 Textareas
136 </td>
137 <td>
138 <span class="text-muted">N/A</span>
139 </td>
140 </tr>
141 <tr>
142 <td class="text-nowrap">
143 {% markdown %}`.form-control-file`{% endmarkdown %}
144 </td>
145 <td>
146 File inputs
147 </td>
148 <td>
149 {% markdown %}`file`{% endmarkdown %}
150 </td>
151 </tr>
152 <tr>
153 <td class="text-nowrap">
154{% markdown %}
155`.form-check`
156{% endmarkdown %}
157 </td>
158 <td class="text-nowrap">
159 Checkboxes and radios
160 </td>
161 <td>
162 <span class="text-muted">N/A</span>
163 </td>
164 </tr>
165 </tbody>
166</table>
167
168### Textual inputs
169
170Here are examples of `.form-control` applied to each textual HTML5 `<input>` `type`.
171
172{% example html %}
173<div class="form-group row">
174 <label for="example-text-input" class="col-2 col-form-label">Text</label>
175 <div class="col-10">
176 <input class="form-control" type="text" value="Artisanal kale" id="example-text-input">
177 </div>
178</div>
179<div class="form-group row">
180 <label for="example-search-input" class="col-2 col-form-label">Search</label>
181 <div class="col-10">
182 <input class="form-control" type="search" value="How do I shoot web" id="example-search-input">
183 </div>
184</div>
185<div class="form-group row">
186 <label for="example-email-input" class="col-2 col-form-label">Email</label>
187 <div class="col-10">
188 <input class="form-control" type="email" value="bootstrap@example.com" id="example-email-input">
189 </div>
190</div>
191<div class="form-group row">
192 <label for="example-url-input" class="col-2 col-form-label">URL</label>
193 <div class="col-10">
194 <input class="form-control" type="url" value="https://getbootstrap.com" id="example-url-input">
195 </div>
196</div>
197<div class="form-group row">
198 <label for="example-tel-input" class="col-2 col-form-label">Telephone</label>
199 <div class="col-10">
200 <input class="form-control" type="tel" value="1-(555)-555-5555" id="example-tel-input">
201 </div>
202</div>
203<div class="form-group row">
204 <label for="example-password-input" class="col-2 col-form-label">Password</label>
205 <div class="col-10">
206 <input class="form-control" type="password" value="hunter2" id="example-password-input">
207 </div>
208</div>
209<div class="form-group row">
210 <label for="example-number-input" class="col-2 col-form-label">Number</label>
211 <div class="col-10">
212 <input class="form-control" type="number" value="42" id="example-number-input">
213 </div>
214</div>
215<div class="form-group row">
216 <label for="example-datetime-local-input" class="col-2 col-form-label">Date and time</label>
217 <div class="col-10">
218 <input class="form-control" type="datetime-local" value="2011-08-19T13:45:00" id="example-datetime-local-input">
219 </div>
220</div>
221<div class="form-group row">
222 <label for="example-date-input" class="col-2 col-form-label">Date</label>
223 <div class="col-10">
224 <input class="form-control" type="date" value="2011-08-19" id="example-date-input">
225 </div>
226</div>
227<div class="form-group row">
228 <label for="example-month-input" class="col-2 col-form-label">Month</label>
229 <div class="col-10">
230 <input class="form-control" type="month" value="2011-08" id="example-month-input">
231 </div>
232</div>
233<div class="form-group row">
234 <label for="example-week-input" class="col-2 col-form-label">Week</label>
235 <div class="col-10">
236 <input class="form-control" type="week" value="2011-W33" id="example-week-input">
237 </div>
238</div>
239<div class="form-group row">
240 <label for="example-time-input" class="col-2 col-form-label">Time</label>
241 <div class="col-10">
242 <input class="form-control" type="time" value="13:45:00" id="example-time-input">
243 </div>
244</div>
245<div class="form-group row">
246 <label for="example-color-input" class="col-2 col-form-label">Color</label>
247 <div class="col-10">
248 <input class="form-control" type="color" value="#563d7c" id="example-color-input">
249 </div>
250</div>
251{% endexample %}
252
253## Form layouts
254
255Since 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.
256
257### Form groups
258
259The `.form-group` class is the easiest way to add some structure to forms. Its only purpose is to provide `margin-bottom` around a label and control pairing. As a bonus, since it's a class you can use it with `<fieldset>`s, `<div>`s, or nearly any other element.
260
261{% example html %}
262<form>
263 <div class="form-group">
264 <label for="formGroupExampleInput">Example label</label>
265 <input type="text" class="form-control" id="formGroupExampleInput" placeholder="Example input">
266 </div>
267 <div class="form-group">
268 <label for="formGroupExampleInput2">Another label</label>
269 <input type="text" class="form-control" id="formGroupExampleInput2" placeholder="Another input">
270 </div>
271</form>
272{% endexample %}
273
274### Inline forms
275
276Use 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.
277
278- Controls are `display: flex`, collapsing any HTML white space and allowing you to provide alignment control with [spacing]({{ site.baseurl }}/utilities/spacing/) and [flexbox]({{ site.baseurl }}/utilities/flexbox/) utilities.
279- Controls and input groups receive `width: auto` to override the Bootstrap default `width: 100%`.
280- Controls **only appear inline in viewports that are at least 576px wide** to account for narrow viewports on mobile devices.
281
282You may need to manually address the width and alignment of individual form controls with [spacing utilities]({{ site.baseurl }}/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`.
283
284{% example html %}
285<form class="form-inline">
286 <label class="sr-only" for="inlineFormInput">Name</label>
287 <input type="text" class="form-control mb-2 mr-sm-2 mb-sm-0" id="inlineFormInput" placeholder="Jane Doe">
288
289 <label class="sr-only" for="inlineFormInputGroup">Username</label>
290 <div class="input-group mb-2 mr-sm-2 mb-sm-0">
291 <div class="input-group-addon">@</div>
292 <input type="text" class="form-control" id="inlineFormInputGroup" placeholder="Username">
293 </div>
294
295 <div class="form-check mb-2 mr-sm-2 mb-sm-0">
296 <label class="form-check-label">
297 <input class="form-check-input" type="checkbox"> Remember me
298 </label>
299 </div>
300
301 <button type="submit" class="btn btn-primary">Submit</button>
302</form>
303{% endexample %}
304
305Custom form controls and selects are also supported.
306
307{% example html %}
308<form class="form-inline">
309 <label class="mr-sm-2" for="inlineFormCustomSelect">Preference</label>
310 <select class="custom-select mb-2 mr-sm-2 mb-sm-0" id="inlineFormCustomSelect">
311 <option selected>Choose...</option>
312 <option value="1">One</option>
313 <option value="2">Two</option>
314 <option value="3">Three</option>
315 </select>
316
317 <label class="custom-control custom-checkbox mb-2 mr-sm-2 mb-sm-0">
318 <input type="checkbox" class="custom-control-input">
319 <span class="custom-control-indicator"></span>
320 <span class="custom-control-description">Remember my preference</span>
321 </label>
322
323 <button type="submit" class="btn btn-primary">Submit</button>
324</form>
325{% endexample %}
326
327{% callout warning %}
328#### Alternatives to hidden labels
329Assistive 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.
330{% endcallout %}
331
332### Using the Grid
333
334For more structured form layouts that are also responsive, you can utilize Bootstrap's [predefined grid classes]({{ site.baseurl }}/layout/grid/) or [mixins]({{ site.baseurl }}/layout/grid/#sass-mixins) to create horizontal forms. Add the `.row` class to form groups and use the `.col-*-*` classes to specify the width of your labels and controls.
335
336Be sure to add `.col-form-label` to your `<label>`s as well so they're vertically centered with their associated form controls. For `<legend>` elements, you can use `.col-form-legend` to make them appear similar to regular `<label>` elements.
337
338{% example html %}
339<div class="container">
340 <form>
341 <div class="form-group row">
342 <label for="inputEmail3" class="col-sm-2 col-form-label">Email</label>
343 <div class="col-sm-10">
344 <input type="email" class="form-control" id="inputEmail3" placeholder="Email">
345 </div>
346 </div>
347 <div class="form-group row">
348 <label for="inputPassword3" class="col-sm-2 col-form-label">Password</label>
349 <div class="col-sm-10">
350 <input type="password" class="form-control" id="inputPassword3" placeholder="Password">
351 </div>
352 </div>
353 <fieldset class="form-group row">
354 <legend class="col-form-legend col-sm-2">Radios</legend>
355 <div class="col-sm-10">
356 <div class="form-check">
357 <label class="form-check-label">
358 <input class="form-check-input" type="radio" name="gridRadios" id="gridRadios1" value="option1" checked>
359 Option one is this and that&mdash;be sure to include why it's great
360 </label>
361 </div>
362 <div class="form-check">
363 <label class="form-check-label">
364 <input class="form-check-input" type="radio" name="gridRadios" id="gridRadios2" value="option2">
365 Option two can be something else and selecting it will deselect option one
366 </label>
367 </div>
368 <div class="form-check disabled">
369 <label class="form-check-label">
370 <input class="form-check-input" type="radio" name="gridRadios" id="gridRadios3" value="option3" disabled>
371 Option three is disabled
372 </label>
373 </div>
374 </div>
375 </fieldset>
376 <div class="form-group row">
377 <label class="col-sm-2">Checkbox</label>
378 <div class="col-sm-10">
379 <div class="form-check">
380 <label class="form-check-label">
381 <input class="form-check-input" type="checkbox"> Check me out
382 </label>
383 </div>
384 </div>
385 </div>
386 <div class="form-group row">
387 <div class="offset-sm-2 col-sm-10">
388 <button type="submit" class="btn btn-primary">Sign in</button>
389 </div>
390 </div>
391 </form>
392</div>
393{% endexample %}
394
395Grid-based form layouts also support large and small inputs.
396
397{% example html %}
398<div class="container">
399 <form>
400 <div class="form-group row">
401 <label for="lgFormGroupInput" class="col-sm-2 col-form-label col-form-label-lg">Email</label>
402 <div class="col-sm-10">
403 <input type="email" class="form-control form-control-lg" id="lgFormGroupInput" placeholder="you@example.com">
404 </div>
405 </div>
406 <div class="form-group row">
407 <label for="smFormGroupInput" class="col-sm-2 col-form-label col-form-label-sm">Email</label>
408 <div class="col-sm-10">
409 <input type="email" class="form-control form-control-sm" id="smFormGroupInput" placeholder="you@example.com">
410 </div>
411 </div>
412 </form>
413</div>
414{% endexample %}
415
416
417## Checkboxes and radios
418
419Default 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.
420
421Disabled checkboxes and radios are supported, but to provide a `not-allowed` cursor on hover of the parent `<label>`, you'll need to add the `.disabled` class to the parent `.form-check`. The disabled class will also lighten the text color to help indicate the input's state.
422
423### Default (stacked)
424
425By default, any number of checkboxes and radios that are immediate sibling will be vertically stacked and appropriately spaced with `.form-check`.
426
427{% example html %}
428<div class="form-check">
429 <label class="form-check-label">
430 <input class="form-check-input" type="checkbox" value="">
431 Option one is this and that&mdash;be sure to include why it's great
432 </label>
433</div>
434<div class="form-check disabled">
435 <label class="form-check-label">
436 <input class="form-check-input" type="checkbox" value="" disabled>
437 Option two is disabled
438 </label>
439</div>
440{% endexample %}
441
442{% example html %}
443<div class="form-check">
444 <label class="form-check-label">
445 <input class="form-check-input" type="radio" name="exampleRadios" id="exampleRadios1" value="option1" checked>
446 Option one is this and that&mdash;be sure to include why it's great
447 </label>
448</div>
449<div class="form-check">
450 <label class="form-check-label">
451 <input class="form-check-input" type="radio" name="exampleRadios" id="exampleRadios2" value="option2">
452 Option two can be something else and selecting it will deselect option one
453 </label>
454</div>
455<div class="form-check disabled">
456 <label class="form-check-label">
457 <input class="form-check-input" type="radio" name="exampleRadios" id="exampleRadios3" value="option3" disabled>
458 Option three is disabled
459 </label>
460</div>
461{% endexample %}
462
463### Inline
464
465Group checkboxes or radios on the same horizontal row by adding `.form-check-inline` to any `.form-check`.
466
467{% example html %}
468<div class="form-check form-check-inline">
469 <label class="form-check-label">
470 <input class="form-check-input" type="checkbox" id="inlineCheckbox1" value="option1"> 1
471 </label>
472</div>
473<div class="form-check form-check-inline">
474 <label class="form-check-label">
475 <input class="form-check-input" type="checkbox" id="inlineCheckbox2" value="option2"> 2
476 </label>
477</div>
478<div class="form-check form-check-inline disabled">
479 <label class="form-check-label">
480 <input class="form-check-input" type="checkbox" id="inlineCheckbox3" value="option3" disabled> 3
481 </label>
482</div>
483{% endexample %}
484
485{% example html %}
486<div class="form-check form-check-inline">
487 <label class="form-check-label">
488 <input class="form-check-input" type="radio" name="inlineRadioOptions" id="inlineRadio1" value="option1"> 1
489 </label>
490</div>
491<div class="form-check form-check-inline">
492 <label class="form-check-label">
493 <input class="form-check-input" type="radio" name="inlineRadioOptions" id="inlineRadio2" value="option2"> 2
494 </label>
495</div>
496<div class="form-check form-check-inline disabled">
497 <label class="form-check-label">
498 <input class="form-check-input" type="radio" name="inlineRadioOptions" id="inlineRadio3" value="option3" disabled> 3
499 </label>
500</div>
501{% endexample %}
502
503### Without labels
504
505Should you have no text within the `<label>`, the input is positioned as you'd expect. **Currently only works on non-inline checkboxes and radios.** Remember to still provide some form of label for assistive technologies (for instance, using `aria-label`).
506
507{% example html %}
508<div class="form-check">
509 <label class="form-check-label">
510 <input class="form-check-input" type="checkbox" id="blankCheckbox" value="option1" aria-label="...">
511 </label>
512</div>
513<div class="form-check">
514 <label class="form-check-label">
515 <input class="form-check-input" type="radio" name="blankRadio" id="blankRadio1" value="option1" aria-label="...">
516 </label>
517</div>
518{% endexample %}
519
520## Static controls
521
522When you need to place plain text next to a form label within a form, use the `.form-control-static` class on an element of your choice.
523
524{% example html %}
525<form>
526 <div class="form-group row">
527 <label class="col-sm-2 col-form-label">Email</label>
528 <div class="col-sm-10">
529 <p class="form-control-static">email@example.com</p>
530 </div>
531 </div>
532 <div class="form-group row">
533 <label for="inputPassword" class="col-sm-2 col-form-label">Password</label>
534 <div class="col-sm-10">
535 <input type="password" class="form-control" id="inputPassword" placeholder="Password">
536 </div>
537 </div>
538</form>
539{% endexample %}
540
541{% example html %}
542<form class="form-inline">
543 <div class="form-group">
544 <label class="sr-only">Email</label>
545 <p class="form-control-static">email@example.com</p>
546 </div>
547 <div class="form-group mx-sm-3">
548 <label for="inputPassword2" class="sr-only">Password</label>
549 <input type="password" class="form-control" id="inputPassword2" placeholder="Password">
550 </div>
551 <button type="submit" class="btn btn-primary">Confirm identity</button>
552</form>
553{% endexample %}
554
555## Disabled states
556
557Add the `disabled` boolean attribute on an input to prevent user interactions. Disabled inputs appear lighter and add a `not-allowed` cursor.
558
559{% highlight html %}
560<input class="form-control" id="disabledInput" type="text" placeholder="Disabled input here..." disabled>
561{% endhighlight %}
562
563Add the `disabled` attribute to a `<fieldset>` to disable all the controls within.
564
565{% example html %}
566<form>
567 <fieldset disabled>
568 <div class="form-group">
569 <label for="disabledTextInput">Disabled input</label>
570 <input type="text" id="disabledTextInput" class="form-control" placeholder="Disabled input">
571 </div>
572 <div class="form-group">
573 <label for="disabledSelect">Disabled select menu</label>
574 <select id="disabledSelect" class="form-control">
575 <option>Disabled select</option>
576 </select>
577 </div>
578 <div class="checkbox">
579 <label>
580 <input type="checkbox"> Can't check this
581 </label>
582 </div>
583 <button type="submit" class="btn btn-primary">Submit</button>
584 </fieldset>
585</form>
586{% endexample %}
587
588{% callout warning %}
589#### Caveat about link functionality of `<a>`
590
591By default, browsers will treat all native form controls (`<input>`, `<select>` and `<button>` elements) inside a `<fieldset disabled>` as disabled, preventing both keyboard and mouse interactions on them. However, if your form also includes `<a ... class="btn btn-*">` elements, these will only be given a style of `pointer-events: none`. As noted in the section about [disabled state for buttons]({{ site.baseurl }}/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 Opera 18 and below, or in Internet Explorer 11, and won't prevent keyboard users from being able to focus or activate these links. So to be safe, use custom JavaScript to disable such links.
592{% endcallout %}
593
594{% callout danger %}
595#### Cross-browser compatibility
596
597While 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.
598{% endcallout %}
599
600## Readonly inputs
601
602Add 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.
603
604{% example html %}
605<input class="form-control" type="text" placeholder="Readonly input here…" readonly>
606{% endexample %}
607
608## Control sizing
609
610Set heights using classes like `.form-control-lg`, and set widths using grid column classes like `.col-lg-*`.
611
612{% example html %}
613<input class="form-control form-control-lg" type="text" placeholder=".form-control-lg">
614<input class="form-control" type="text" placeholder="Default input">
615<input class="form-control form-control-sm" type="text" placeholder=".form-control-sm">
616{% endexample %}
617
618{% example html %}
619<select class="form-control form-control-lg">
620 <option>Large select</option>
621</select>
622<select class="form-control">
623 <option>Default select</option>
624</select>
625<select class="form-control form-control-sm">
626 <option>Small select</option>
627</select>
628{% endexample %}
629
630## Column sizing
631
632Wrap inputs in grid columns, or any custom parent element, to easily enforce desired widths.
633
634{% example html %}
635<div class="row">
636 <div class="col-2">
637 <input type="text" class="form-control" placeholder=".col-2">
638 </div>
639 <div class="col-3">
640 <input type="text" class="form-control" placeholder=".col-3">
641 </div>
642 <div class="col-4">
643 <input type="text" class="form-control" placeholder=".col-4">
644 </div>
645</div>
646{% endexample %}
647
648## Help text
649
650Block-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`.
651
652{% callout warning %}
653#### Associating help text with form controls
654
655Help 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.
656{% endcallout %}
657
658### Block level
659
660Block help text—for below inputs or for longer lines of help text—can be easily achieved with `.form-text`. This class includes `display: block` and adds some top margin for easy spacing from the inputs above.
661
662{% example html %}
663<label for="inputPassword5">Password</label>
664<input type="password" id="inputPassword5" class="form-control" aria-describedby="passwordHelpBlock">
665<p id="passwordHelpBlock" class="form-text text-muted">
666 Your password must be 8-20 characters long, contain letters and numbers, and must not contain spaces, special characters, or emoji.
667</p>
668{% endexample %}
669
670### Inline
671
672Inline text can use any typical inline HTML element (be it a `<small>`, `<span>`, or something else).
673
674{% example html %}
675<form class="form-inline">
676 <div class="form-group">
677 <label for="inputPassword4">Password</label>
678 <input type="password" id="inputPassword4" class="form-control mx-sm-3" aria-describedby="passwordHelpInline">
679 <small id="passwordHelpInline" class="text-muted">
680 Must be 8-20 characters long.
681 </small>
682 </div>
683</form>
684{% endexample %}
685
686## Validation
687
688Bootstrap includes validation styles for danger, warning, and success states on most form controls.
689
690### How it works
691
692Here's a rundown of how they work:
693
694- To use, add `.has-warning`, `.has-danger`, or `.has-success` to the parent element. Any `.col-form-label`, `.form-control`, or custom form element will receive the validation styles.
695- Contextual validation text, in addition to your usual form field help text, can be added with the use of `.form-control-feedback`. This text will adapt to the parent `.has-*` class. By default it only includes a bit of `margin` for spacing and a modified `color` for each state.
696- Validation icons are `url()`s configured via Sass variables that are applied to `background-image` declarations for each state.
697- You may use your own base64 PNGs or SVGs by updating the Sass variables and recompiling.
698- Icons can also be disabled entirely by setting the variables to `none` or commenting out the source Sass.
699
700### Defining states
701
702Generally speaking, you'll want to use a particular state for specific types of feedback:
703
704- **Danger** is great for when there's a blocking or required field. A user *must* fill in this field properly to submit the form.
705- **Warning** works well for input values that are in progress, like password strength, or soft validation before a user attempts to submit a form.
706- And lastly, **success** is ideal for situations when you have per-field validation throughout a form and want to encourage a user through the rest of the fields.
707
708{% comment %}
709{% callout warning %}
710#### Conveying validation state to assistive technologies and colorblind users
711
712Using these validation styles to denote the state of a form control only provides a visual, color-based indication, which will not be conveyed to users of assistive technologies - such as screen readers - or to colorblind users.
713
714Ensure that an alternative indication of state is also provided. For instance, you can include a hint about state in the form control's `<label>` text itself (as is the case in the following code example), include a [Glyphicon]({{ site.baseurl }}/components/#glyphicons) (with appropriate alternative text using the `.sr-only` class - see the [Glyphicon examples]({{ site.baseurl }}/components/#glyphicons-examples)), or by providing an additional [help text](#forms-help-text) block. Specifically for assistive technologies, invalid form controls can also be assigned an `aria-invalid="true"` attribute.
715{% endcallout %}
716{% endcomment %}
717
718### Examples
719
720Here are some examples of the aforementioned classes in action. First up is your standard left-aligned fields with labels, help text, and validation messaging.
721
722{% example html %}
723<div class="form-group has-success">
724 <label class="form-control-label" for="inputSuccess1">Input with success</label>
725 <input type="text" class="form-control form-control-success" id="inputSuccess1">
726 <div class="form-control-feedback">Success! You've done it.</div>
727 <small class="form-text text-muted">Example help text that remains unchanged.</small>
728</div>
729<div class="form-group has-warning">
730 <label class="form-control-label" for="inputWarning1">Input with warning</label>
731 <input type="text" class="form-control form-control-warning" id="inputWarning1">
732 <div class="form-control-feedback">Shucks, check the formatting of that and try again.</div>
733 <small class="form-text text-muted">Example help text that remains unchanged.</small>
734</div>
735<div class="form-group has-danger">
736 <label class="form-control-label" for="inputDanger1">Input with danger</label>
737 <input type="text" class="form-control form-control-danger" id="inputDanger1">
738 <div class="form-control-feedback">Sorry, that username's taken. Try another?</div>
739 <small class="form-text text-muted">Example help text that remains unchanged.</small>
740</div>
741{% endexample %}
742
743Those same states can also be used with horizontal forms.
744
745{% example html %}
746<div class="container">
747 <form>
748 <div class="form-group row has-success">
749 <label for="inputHorizontalSuccess" class="col-sm-2 col-form-label">Email</label>
750 <div class="col-sm-10">
751 <input type="email" class="form-control form-control-success" id="inputHorizontalSuccess" placeholder="name@example.com">
752 <div class="form-control-feedback">Success! You've done it.</div>
753 <small class="form-text text-muted">Example help text that remains unchanged.</small>
754 </div>
755 </div>
756 <div class="form-group row has-warning">
757 <label for="inputHorizontalWarning" class="col-sm-2 col-form-label">Email</label>
758 <div class="col-sm-10">
759 <input type="email" class="form-control form-control-warning" id="inputHorizontalWarning" placeholder="name@example.com">
760 <div class="form-control-feedback">Shucks, check the formatting of that and try again.</div>
761 <small class="form-text text-muted">Example help text that remains unchanged.</small>
762 </div>
763 </div>
764 <div class="form-group row has-danger">
765 <label for="inputHorizontalDnger" class="col-sm-2 col-form-label">Email</label>
766 <div class="col-sm-10">
767 <input type="email" class="form-control form-control-danger" id="inputHorizontalDnger" placeholder="name@example.com">
768 <div class="form-control-feedback">Sorry, that username's taken. Try another?</div>
769 <small class="form-text text-muted">Example help text that remains unchanged.</small>
770 </div>
771 </div>
772 </form>
773</div>
774{% endexample %}
775
776Checkboxes and radios are also supported.
777
778{% example html %}
779<div class="form-check has-success">
780 <label class="form-check-label">
781 <input type="checkbox" class="form-check-input" id="checkboxSuccess" value="option1">
782 Checkbox with success
783 </label>
784</div>
785<div class="form-check has-warning">
786 <label class="form-check-label">
787 <input type="checkbox" class="form-check-input" id="checkboxWarning" value="option1">
788 Checkbox with warning
789 </label>
790</div>
791<div class="form-check has-danger">
792 <label class="form-check-label">
793 <input type="checkbox" class="form-check-input" id="checkboxDanger" value="option1">
794 Checkbox with danger
795 </label>
796</div>
797{% endexample %}
798
799## Custom forms
800
801For 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.
802
803### Checkboxes and radios
804
805Each checkbox and radio is wrapped in a `<label>` for three reasons:
806
807- It provides a larger hit areas for checking the control.
808- It provides a helpful and semantic wrapper to help us replace the default `<input>`s.
809- It triggers the state of the `<input>` automatically, meaning no JavaScript is required.
810
811We hide the default `<input>` with `opacity` and use the `.custom-control-indicator` to build a new custom form indicator in its place. Unfortunately we can't build a custom one from just the `<input>` because CSS's `content` doesn't work on that element.
812
813We use the sibling selector (`~`) for all our `<input>` states—like `:checked`—to properly style our custom form indicator. When combined with the `.custom-control-description` class, we can also style the text for each item based on the `<input>`'s state.
814
815In the checked states, we use **base64 embedded SVG icons** from [Open Iconic](https://useiconic.com/open). This provides us the best control for styling and positioning across browsers and devices.
816
817#### Checkboxes
818
819{% example html %}
820<label class="custom-control custom-checkbox">
821 <input type="checkbox" class="custom-control-input">
822 <span class="custom-control-indicator"></span>
823 <span class="custom-control-description">Check this custom checkbox</span>
824</label>
825{% endexample %}
826
827Custom checkboxes can also utilize the `:indeterminate` pseudo class when manually set via JavaScript (there is no available HTML attribute for specifying it).
828
829<div class="bd-example bd-example-indeterminate">
830 <label class="custom-control custom-checkbox">
831 <input type="checkbox" class="custom-control-input">
832 <span class="custom-control-indicator"></span>
833 <span class="custom-control-description">Check this custom checkbox</span>
834 </label>
835</div>
836
837If you're using jQuery, something like this should suffice:
838
839{% highlight js %}
840$('.your-checkbox').prop('indeterminate', true)
841{% endhighlight %}
842
843#### Radios
844
845{% example html %}
846<label class="custom-control custom-radio">
847 <input id="radio1" name="radio" type="radio" class="custom-control-input">
848 <span class="custom-control-indicator"></span>
849 <span class="custom-control-description">Toggle this custom radio</span>
850</label>
851<label class="custom-control custom-radio">
852 <input id="radio2" name="radio" type="radio" class="custom-control-input">
853 <span class="custom-control-indicator"></span>
854 <span class="custom-control-description">Or toggle this other custom radio</span>
855</label>
856{% endexample %}
857
858#### Disabled
859
860Custom 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.
861
862{% example html %}
863<label class="custom-control custom-checkbox">
864 <input type="checkbox" class="custom-control-input" disabled>
865 <span class="custom-control-indicator"></span>
866 <span class="custom-control-description">Check this custom checkbox</span>
867</label>
868
869<label class="custom-control custom-radio">
870 <input id="radio3" name="radioDisabled" type="radio" class="custom-control-input" disabled>
871 <span class="custom-control-indicator"></span>
872 <span class="custom-control-description">Toggle this custom radio</span>
873</label>
874{% endexample %}
875
876#### Validation states
877
878Add other states to your custom forms with our validation classes.
879
880{% example html %}
881<div class="form-group has-success">
882 <label class="custom-control custom-checkbox">
883 <input type="checkbox" class="custom-control-input">
884 <span class="custom-control-indicator"></span>
885 <span class="custom-control-description">Check this custom checkbox</span>
886 </label>
887</div>
888<div class="form-group has-warning">
889 <label class="custom-control custom-checkbox">
890 <input type="checkbox" class="custom-control-input">
891 <span class="custom-control-indicator"></span>
892 <span class="custom-control-description">Check this custom checkbox</span>
893 </label>
894</div>
895<div class="form-group has-danger mb-0">
896 <label class="custom-control custom-checkbox">
897 <input type="checkbox" class="custom-control-input">
898 <span class="custom-control-indicator"></span>
899 <span class="custom-control-description">Check this custom checkbox</span>
900 </label>
901</div>
902{% endexample %}
903
904#### Stacked
905
906Custom checkboxes and radios are inline to start. Add a parent with class `.custom-controls-stacked` to ensure each form control is on separate lines.
907
908{% example html %}
909<div class="custom-controls-stacked">
910 <label class="custom-control custom-radio">
911 <input id="radioStacked1" name="radio-stacked" type="radio" class="custom-control-input">
912 <span class="custom-control-indicator"></span>
913 <span class="custom-control-description">Toggle this custom radio</span>
914 </label>
915 <label class="custom-control custom-radio">
916 <input id="radioStacked2" name="radio-stacked" type="radio" class="custom-control-input">
917 <span class="custom-control-indicator"></span>
918 <span class="custom-control-description">Or toggle this other custom radio</span>
919 </label>
920</div>
921{% endexample %}
922
923### Select menu
924
925Custom `<select>` menus need only a custom class, `.custom-select` to trigger the custom styles.
926
927{% example html %}
928<select class="custom-select">
929 <option selected>Open this select menu</option>
930 <option value="1">One</option>
931 <option value="2">Two</option>
932 <option value="3">Three</option>
933</select>
934{% endexample %}
935
936### File browser
937
938The file input is the most gnarly of the bunch and require additional JavaScript if you'd like to hook them up with functional *Choose file...* and selected file name text.
939
940{% example html %}
941<label class="custom-file">
942 <input type="file" id="file" class="custom-file-input">
943 <span class="custom-file-control"></span>
944</label>
945{% endexample %}
946
947Here's how it works:
948
949- We wrap the `<input>` in a `<label>` so the custom control properly triggers the file browser.
950- We hide the default file `<input>` via `opacity`.
951- We use `:after` to generate a custom background and directive (*Choose file...*).
952- We use `:before` to generate and position the *Browse* button.
953- We declare a `height` on the `<input>` for proper spacing for surrounding content.
954
955In other words, it's an entirely custom element, all generated via CSS.
956
957#### Translating or customizing the strings
958
959The [`:lang()` pseudo-class](https://developer.mozilla.org/en-US/docs/Web/CSS/:lang) is used to allow for easy translation of the "Browse" and "Choose file..." text into other languages. Simply override or add entries to the `$custom-file-text` SCSS 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`):
960
961{% highlight scss %}
962$custom-file-text: (
963 placeholder: (
964 en: "Choose file...",
965 es: "Seleccionar archivo..."
966 ),
967 button-label: (
968 en: "Browse",
969 es: "Navegar"
970 )
971);
972{% endhighlight %}
973
974You'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) or the [`Content-Language` HTTP header](https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.12), among other methods.