Forms

Examples and usage guidelines for form control styles, layout options, and custom components for creating a wide variety of forms.

Overview

Bootstrap’s form controls expand on our Rebooted form styles with classes. Use these classes to opt into their customized displays for a more consistent rendering across browsers and devices.

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.

Here’s a quick example to demonstrate Bootstrap’s form styles. Keep reading for documentation on required classes, form layout, and more.

Forms - 图1

  1. <form>
  2. <div class="form-group">
  3. <label for="exampleInputEmail1">Email address</label>
  4. <input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp">
  5. <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
  6. </div>
  7. <div class="form-group">
  8. <label for="exampleInputPassword1">Password</label>
  9. <input type="password" class="form-control" id="exampleInputPassword1">
  10. </div>
  11. <div class="form-group form-check">
  12. <input type="checkbox" class="form-check-input" id="exampleCheck1">
  13. <label class="form-check-label" for="exampleCheck1">Check me out</label>
  14. </div>
  15. <button type="submit" class="btn btn-primary">Submit</button>
  16. </form>

Form controls

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.

Be sure to explore our custom forms to further style <select>s.

Forms - 图2

  1. <form>
  2. <div class="form-group">
  3. <label for="exampleFormControlInput1">Email address</label>
  4. <input type="email" class="form-control" id="exampleFormControlInput1" placeholder="name@example.com">
  5. </div>
  6. <div class="form-group">
  7. <label for="exampleFormControlSelect1">Example select</label>
  8. <select class="form-control" id="exampleFormControlSelect1">
  9. <option>1</option>
  10. <option>2</option>
  11. <option>3</option>
  12. <option>4</option>
  13. <option>5</option>
  14. </select>
  15. </div>
  16. <div class="form-group">
  17. <label for="exampleFormControlSelect2">Example multiple select</label>
  18. <select multiple class="form-control" id="exampleFormControlSelect2">
  19. <option>1</option>
  20. <option>2</option>
  21. <option>3</option>
  22. <option>4</option>
  23. <option>5</option>
  24. </select>
  25. </div>
  26. <div class="form-group">
  27. <label for="exampleFormControlTextarea1">Example textarea</label>
  28. <textarea class="form-control" id="exampleFormControlTextarea1" rows="3"></textarea>
  29. </div>
  30. </form>

For file inputs, swap the .form-control for .form-control-file.

Forms - 图3

  1. <form>
  2. <div class="form-group">
  3. <label for="exampleFormControlFile1">Example file input</label>
  4. <input type="file" class="form-control-file" id="exampleFormControlFile1">
  5. </div>
  6. </form>

Sizing

Set heights using classes like .form-control-lg and .form-control-sm.

Forms - 图4

  1. <input class="form-control form-control-lg" type="text" placeholder=".form-control-lg">
  2. <input class="form-control" type="text" placeholder="Default input">
  3. <input class="form-control form-control-sm" type="text" placeholder=".form-control-sm">

Forms - 图5

  1. <select class="form-control form-control-lg">
  2. <option>Large select</option>
  3. </select>
  4. <select class="form-control">
  5. <option>Default select</option>
  6. </select>
  7. <select class="form-control form-control-sm">
  8. <option>Small select</option>
  9. </select>

Readonly

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.

Forms - 图6

  1. <input class="form-control" type="text" placeholder="Readonly input here..." readonly>

Readonly plain text

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.

Forms - 图7

  1. <form>
  2. <div class="form-group row">
  3. <label for="staticEmail" class="col-sm-2 col-form-label">Email</label>
  4. <div class="col-sm-10">
  5. <input type="text" readonly class="form-control-plaintext" id="staticEmail" value="email@example.com">
  6. </div>
  7. </div>
  8. <div class="form-group row">
  9. <label for="inputPassword" class="col-sm-2 col-form-label">Password</label>
  10. <div class="col-sm-10">
  11. <input type="password" class="form-control" id="inputPassword">
  12. </div>
  13. </div>
  14. </form>

Forms - 图8

  1. <form class="form-inline">
  2. <div class="form-group mb-2">
  3. <label for="staticEmail2" class="sr-only">Email</label>
  4. <input type="text" readonly class="form-control-plaintext" id="staticEmail2" value="email@example.com">
  5. </div>
  6. <div class="form-group mx-sm-3 mb-2">
  7. <label for="inputPassword2" class="sr-only">Password</label>
  8. <input type="password" class="form-control" id="inputPassword2" placeholder="Password">
  9. </div>
  10. <button type="submit" class="btn btn-primary mb-2">Confirm identity</button>
  11. </form>

Range Inputs

Set horizontally scrollable range inputs using .form-control-range.

Forms - 图9

  1. <form>
  2. <div class="form-group">
  3. <label for="formControlRange">Example Range input</label>
  4. <input type="range" class="form-control-range" id="formControlRange">
  5. </div>
  6. </form>

Checkboxes and radios

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.

Disabled checkboxes and radios are supported. The disabled attribute will apply a lighter color to help indicate the input’s state.

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>.

Default (stacked)

By default, any number of checkboxes and radios that are immediate sibling will be vertically stacked and appropriately spaced with .form-check.

Forms - 图10

  1. <div class="form-check">
  2. <input class="form-check-input" type="checkbox" value="" id="defaultCheck1">
  3. <label class="form-check-label" for="defaultCheck1">
  4. Default checkbox
  5. </label>
  6. </div>
  7. <div class="form-check">
  8. <input class="form-check-input" type="checkbox" value="" id="defaultCheck2" disabled>
  9. <label class="form-check-label" for="defaultCheck2">
  10. Disabled checkbox
  11. </label>
  12. </div>

Forms - 图11

  1. <div class="form-check">
  2. <input class="form-check-input" type="radio" name="exampleRadios" id="exampleRadios1" value="option1" checked>
  3. <label class="form-check-label" for="exampleRadios1">
  4. Default radio
  5. </label>
  6. </div>
  7. <div class="form-check">
  8. <input class="form-check-input" type="radio" name="exampleRadios" id="exampleRadios2" value="option2">
  9. <label class="form-check-label" for="exampleRadios2">
  10. Second default radio
  11. </label>
  12. </div>
  13. <div class="form-check">
  14. <input class="form-check-input" type="radio" name="exampleRadios" id="exampleRadios3" value="option3" disabled>
  15. <label class="form-check-label" for="exampleRadios3">
  16. Disabled radio
  17. </label>
  18. </div>

Inline

Group checkboxes or radios on the same horizontal row by adding .form-check-inline to any .form-check.

Forms - 图12

  1. <div class="form-check form-check-inline">
  2. <input class="form-check-input" type="checkbox" id="inlineCheckbox1" value="option1">
  3. <label class="form-check-label" for="inlineCheckbox1">1</label>
  4. </div>
  5. <div class="form-check form-check-inline">
  6. <input class="form-check-input" type="checkbox" id="inlineCheckbox2" value="option2">
  7. <label class="form-check-label" for="inlineCheckbox2">2</label>
  8. </div>
  9. <div class="form-check form-check-inline">
  10. <input class="form-check-input" type="checkbox" id="inlineCheckbox3" value="option3" disabled>
  11. <label class="form-check-label" for="inlineCheckbox3">3 (disabled)</label>
  12. </div>

Forms - 图13

  1. <div class="form-check form-check-inline">
  2. <input class="form-check-input" type="radio" name="inlineRadioOptions" id="inlineRadio1" value="option1">
  3. <label class="form-check-label" for="inlineRadio1">1</label>
  4. </div>
  5. <div class="form-check form-check-inline">
  6. <input class="form-check-input" type="radio" name="inlineRadioOptions" id="inlineRadio2" value="option2">
  7. <label class="form-check-label" for="inlineRadio2">2</label>
  8. </div>
  9. <div class="form-check form-check-inline">
  10. <input class="form-check-input" type="radio" name="inlineRadioOptions" id="inlineRadio3" value="option3" disabled>
  11. <label class="form-check-label" for="inlineRadio3">3 (disabled)</label>
  12. </div>

Without labels

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).

Forms - 图14

  1. <div class="form-check">
  2. <input class="form-check-input position-static" type="checkbox" id="blankCheckbox" value="option1" aria-label="...">
  3. </div>
  4. <div class="form-check">
  5. <input class="form-check-input position-static" type="radio" name="blankRadio" id="blankRadio1" value="option1" aria-label="...">
  6. </div>

Layout

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.

Form groups

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.

Forms - 图15

  1. <form>
  2. <div class="form-group">
  3. <label for="formGroupExampleInput">Example label</label>
  4. <input type="text" class="form-control" id="formGroupExampleInput" placeholder="Example input placeholder">
  5. </div>
  6. <div class="form-group">
  7. <label for="formGroupExampleInput2">Another label</label>
  8. <input type="text" class="form-control" id="formGroupExampleInput2" placeholder="Another input placeholder">
  9. </div>
  10. </form>

Form grid

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.

Forms - 图16

  1. <form>
  2. <div class="row">
  3. <div class="col">
  4. <input type="text" class="form-control" placeholder="First name">
  5. </div>
  6. <div class="col">
  7. <input type="text" class="form-control" placeholder="Last name">
  8. </div>
  9. </div>
  10. </form>

Form row

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.

Forms - 图17

  1. <form>
  2. <div class="form-row">
  3. <div class="col">
  4. <input type="text" class="form-control" placeholder="First name">
  5. </div>
  6. <div class="col">
  7. <input type="text" class="form-control" placeholder="Last name">
  8. </div>
  9. </div>
  10. </form>

More complex layouts can also be created with the grid system.

Forms - 图18

  1. <form>
  2. <div class="form-row">
  3. <div class="form-group col-md-6">
  4. <label for="inputEmail4">Email</label>
  5. <input type="email" class="form-control" id="inputEmail4">
  6. </div>
  7. <div class="form-group col-md-6">
  8. <label for="inputPassword4">Password</label>
  9. <input type="password" class="form-control" id="inputPassword4">
  10. </div>
  11. </div>
  12. <div class="form-group">
  13. <label for="inputAddress">Address</label>
  14. <input type="text" class="form-control" id="inputAddress" placeholder="1234 Main St">
  15. </div>
  16. <div class="form-group">
  17. <label for="inputAddress2">Address 2</label>
  18. <input type="text" class="form-control" id="inputAddress2" placeholder="Apartment, studio, or floor">
  19. </div>
  20. <div class="form-row">
  21. <div class="form-group col-md-6">
  22. <label for="inputCity">City</label>
  23. <input type="text" class="form-control" id="inputCity">
  24. </div>
  25. <div class="form-group col-md-4">
  26. <label for="inputState">State</label>
  27. <select id="inputState" class="form-control">
  28. <option selected>Choose...</option>
  29. <option>...</option>
  30. </select>
  31. </div>
  32. <div class="form-group col-md-2">
  33. <label for="inputZip">Zip</label>
  34. <input type="text" class="form-control" id="inputZip">
  35. </div>
  36. </div>
  37. <div class="form-group">
  38. <div class="form-check">
  39. <input class="form-check-input" type="checkbox" id="gridCheck">
  40. <label class="form-check-label" for="gridCheck">
  41. Check me out
  42. </label>
  43. </div>
  44. </div>
  45. <button type="submit" class="btn btn-primary">Sign in</button>
  46. </form>

Horizontal form

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.

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.

Forms - 图19

  1. <form>
  2. <div class="form-group row">
  3. <label for="inputEmail3" class="col-sm-2 col-form-label">Email</label>
  4. <div class="col-sm-10">
  5. <input type="email" class="form-control" id="inputEmail3">
  6. </div>
  7. </div>
  8. <div class="form-group row">
  9. <label for="inputPassword3" class="col-sm-2 col-form-label">Password</label>
  10. <div class="col-sm-10">
  11. <input type="password" class="form-control" id="inputPassword3">
  12. </div>
  13. </div>
  14. <fieldset class="form-group row">
  15. <legend class="col-form-label col-sm-2 float-sm-left pt-0">Radios</legend>
  16. <div class="col-sm-10">
  17. <div class="form-check">
  18. <input class="form-check-input" type="radio" name="gridRadios" id="gridRadios1" value="option1" checked>
  19. <label class="form-check-label" for="gridRadios1">
  20. First radio
  21. </label>
  22. </div>
  23. <div class="form-check">
  24. <input class="form-check-input" type="radio" name="gridRadios" id="gridRadios2" value="option2">
  25. <label class="form-check-label" for="gridRadios2">
  26. Second radio
  27. </label>
  28. </div>
  29. <div class="form-check disabled">
  30. <input class="form-check-input" type="radio" name="gridRadios" id="gridRadios3" value="option3" disabled>
  31. <label class="form-check-label" for="gridRadios3">
  32. Third disabled radio
  33. </label>
  34. </div>
  35. </div>
  36. </fieldset>
  37. <div class="form-group row">
  38. <div class="col-sm-10 offset-sm-2">
  39. <div class="form-check">
  40. <input class="form-check-input" type="checkbox" id="gridCheck1">
  41. <label class="form-check-label" for="gridCheck1">
  42. Example checkbox
  43. </label>
  44. </div>
  45. </div>
  46. </div>
  47. <div class="form-group row">
  48. <div class="col-sm-10">
  49. <button type="submit" class="btn btn-primary">Sign in</button>
  50. </div>
  51. </div>
  52. </form>
Horizontal form label sizing

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.

Forms - 图20

  1. <form>
  2. <div class="form-group row">
  3. <label for="colFormLabelSm" class="col-sm-2 col-form-label col-form-label-sm">Email</label>
  4. <div class="col-sm-10">
  5. <input type="email" class="form-control form-control-sm" id="colFormLabelSm" placeholder="col-form-label-sm">
  6. </div>
  7. </div>
  8. <div class="form-group row">
  9. <label for="colFormLabel" class="col-sm-2 col-form-label">Email</label>
  10. <div class="col-sm-10">
  11. <input type="email" class="form-control" id="colFormLabel" placeholder="col-form-label">
  12. </div>
  13. </div>
  14. <div class="form-group row">
  15. <label for="colFormLabelLg" class="col-sm-2 col-form-label col-form-label-lg">Email</label>
  16. <div class="col-sm-10">
  17. <input type="email" class="form-control form-control-lg" id="colFormLabelLg" placeholder="col-form-label-lg">
  18. </div>
  19. </div>
  20. </form>

Column sizing

As shown in the previous examples, our grid system allows you to place any number of .cols 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 .cols equally split the rest, with specific column classes like .col-7.

Forms - 图21

  1. <form>
  2. <div class="form-row">
  3. <div class="col-7">
  4. <input type="text" class="form-control" placeholder="City">
  5. </div>
  6. <div class="col">
  7. <input type="text" class="form-control" placeholder="State">
  8. </div>
  9. <div class="col">
  10. <input type="text" class="form-control" placeholder="Zip">
  11. </div>
  12. </div>
  13. </form>

Auto-sizing

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.

Forms - 图22

  1. <form>
  2. <div class="form-row align-items-center">
  3. <div class="col-auto">
  4. <label class="sr-only" for="inlineFormInput">Name</label>
  5. <input type="text" class="form-control mb-2" id="inlineFormInput" placeholder="Jane Doe">
  6. </div>
  7. <div class="col-auto">
  8. <label class="sr-only" for="inlineFormInputGroup">Username</label>
  9. <div class="input-group mb-2">
  10. <div class="input-group-prepend">
  11. <div class="input-group-text">@</div>
  12. </div>
  13. <input type="text" class="form-control" id="inlineFormInputGroup" placeholder="Username">
  14. </div>
  15. </div>
  16. <div class="col-auto">
  17. <div class="form-check mb-2">
  18. <input class="form-check-input" type="checkbox" id="autoSizingCheck">
  19. <label class="form-check-label" for="autoSizingCheck">
  20. Remember me
  21. </label>
  22. </div>
  23. </div>
  24. <div class="col-auto">
  25. <button type="submit" class="btn btn-primary mb-2">Submit</button>
  26. </div>
  27. </div>
  28. </form>

You can then remix that once again with size-specific column classes.

Forms - 图23

  1. <form>
  2. <div class="form-row align-items-center">
  3. <div class="col-sm-3 my-1">
  4. <label class="sr-only" for="inlineFormInputName">Name</label>
  5. <input type="text" class="form-control" id="inlineFormInputName" placeholder="Jane Doe">
  6. </div>
  7. <div class="col-sm-3 my-1">
  8. <label class="sr-only" for="inlineFormInputGroupUsername">Username</label>
  9. <div class="input-group">
  10. <div class="input-group-prepend">
  11. <div class="input-group-text">@</div>
  12. </div>
  13. <input type="text" class="form-control" id="inlineFormInputGroupUsername" placeholder="Username">
  14. </div>
  15. </div>
  16. <div class="col-auto my-1">
  17. <div class="form-check">
  18. <input class="form-check-input" type="checkbox" id="autoSizingCheck2">
  19. <label class="form-check-label" for="autoSizingCheck2">
  20. Remember me
  21. </label>
  22. </div>
  23. </div>
  24. <div class="col-auto my-1">
  25. <button type="submit" class="btn btn-primary">Submit</button>
  26. </div>
  27. </div>
  28. </form>

And of course custom form controls are supported.

Forms - 图24

  1. <form>
  2. <div class="form-row align-items-center">
  3. <div class="col-auto my-1">
  4. <label class="mr-sm-2 sr-only" for="inlineFormCustomSelect">Preference</label>
  5. <select class="custom-select mr-sm-2" id="inlineFormCustomSelect">
  6. <option selected>Choose...</option>
  7. <option value="1">One</option>
  8. <option value="2">Two</option>
  9. <option value="3">Three</option>
  10. </select>
  11. </div>
  12. <div class="col-auto my-1">
  13. <div class="custom-control custom-checkbox mr-sm-2">
  14. <input type="checkbox" class="custom-control-input" id="customControlAutosizing">
  15. <label class="custom-control-label" for="customControlAutosizing">Remember my preference</label>
  16. </div>
  17. </div>
  18. <div class="col-auto my-1">
  19. <button type="submit" class="btn btn-primary">Submit</button>
  20. </div>
  21. </div>
  22. </form>

Inline forms

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.

  • Controls are display: flex, collapsing any HTML white space and allowing you to provide alignment control with spacing and flexbox utilities.
  • Controls and input groups receive width: auto to override the Bootstrap default width: 100%.
  • Controls only appear inline in viewports that are at least 576px wide to account for narrow viewports on mobile devices.

You may need to manually address the width and alignment of individual form controls with spacing utilities (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.

Forms - 图25

  1. <form class="form-inline">
  2. <label class="sr-only" for="inlineFormInputName2">Name</label>
  3. <input type="text" class="form-control mb-2 mr-sm-2" id="inlineFormInputName2" placeholder="Jane Doe">
  4. <label class="sr-only" for="inlineFormInputGroupUsername2">Username</label>
  5. <div class="input-group mb-2 mr-sm-2">
  6. <div class="input-group-prepend">
  7. <div class="input-group-text">@</div>
  8. </div>
  9. <input type="text" class="form-control" id="inlineFormInputGroupUsername2" placeholder="Username">
  10. </div>
  11. <div class="form-check mb-2 mr-sm-2">
  12. <input class="form-check-input" type="checkbox" id="inlineFormCheck">
  13. <label class="form-check-label" for="inlineFormCheck">
  14. Remember me
  15. </label>
  16. </div>
  17. <button type="submit" class="btn btn-primary mb-2">Submit</button>
  18. </form>

Custom form controls and selects are also supported.

Forms - 图26

  1. <form class="form-inline">
  2. <label class="my-1 mr-2" for="inlineFormCustomSelectPref">Preference</label>
  3. <select class="custom-select my-1 mr-sm-2" id="inlineFormCustomSelectPref">
  4. <option selected>Choose...</option>
  5. <option value="1">One</option>
  6. <option value="2">Two</option>
  7. <option value="3">Three</option>
  8. </select>
  9. <div class="custom-control custom-checkbox my-1 mr-sm-2">
  10. <input type="checkbox" class="custom-control-input" id="customControlInline">
  11. <label class="custom-control-label" for="customControlInline">Remember my preference</label>
  12. </div>
  13. <button type="submit" class="btn btn-primary my-1">Submit</button>
  14. </form>
Alternatives to hidden labels

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.

Help text

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.

Associating help text with form controls

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.

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.

Forms - 图27

  1. <label for="inputPassword5">Password</label>
  2. <input type="password" id="inputPassword5" class="form-control" aria-describedby="passwordHelpBlock">
  3. <small id="passwordHelpBlock" class="form-text text-muted">
  4. Your password must be 8-20 characters long, contain letters and numbers, and must not contain spaces, special characters, or emoji.
  5. </small>

Inline text can use any typical inline HTML element (be it a <small>, <span>, or something else) with nothing more than a utility class.

Forms - 图28

  1. <form class="form-inline">
  2. <div class="form-group">
  3. <label for="inputPassword6">Password</label>
  4. <input type="password" id="inputPassword6" class="form-control mx-sm-3" aria-describedby="passwordHelpInline">
  5. <small id="passwordHelpInline" class="text-muted">
  6. Must be 8-20 characters long.
  7. </small>
  8. </div>
  9. </form>

Disabled forms

Add the disabled boolean attribute on an input to prevent user interactions and make it appear lighter.

  1. <input class="form-control" id="disabledInput" type="text" placeholder="Disabled input here..." disabled>

Add the disabled attribute to a <fieldset> to disable all the controls within.

Forms - 图29

  1. <form>
  2. <fieldset disabled>
  3. <legend>Disabled fieldset example</legend>
  4. <div class="form-group">
  5. <label for="disabledTextInput">Disabled input</label>
  6. <input type="text" id="disabledTextInput" class="form-control" placeholder="Disabled input">
  7. </div>
  8. <div class="form-group">
  9. <label for="disabledSelect">Disabled select menu</label>
  10. <select id="disabledSelect" class="form-control">
  11. <option>Disabled select</option>
  12. </select>
  13. </div>
  14. <div class="form-group">
  15. <div class="form-check">
  16. <input class="form-check-input" type="checkbox" id="disabledFieldsetCheck" disabled>
  17. <label class="form-check-label" for="disabledFieldsetCheck">
  18. Can't check this
  19. </label>
  20. </div>
  21. </div>
  22. <button type="submit" class="btn btn-primary">Submit</button>
  23. </fieldset>
  24. </form>
Caveat with anchors

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.

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 (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.

Cross-browser compatibility

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.

Validation

Provide valuable, actionable feedback to your users with HTML5 form validation–available in all our supported browsers. Choose from the browser default validation feedback, or implement custom messages with our built-in classes and starter JavaScript.

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.

How it works

Here’s how form validation works with Bootstrap:

  • HTML form validation is applied via CSS’s two pseudo-classes, :invalid and :valid. It applies to <input>, <select>, and <textarea> elements.
  • 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).
  • 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.
  • As a fallback, .is-invalid and .is-valid classes may be used instead of the pseudo-classes for server side validation. They do not require a .was-validated parent class.
  • 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.
  • All modern browsers support the constraint validation API, a series of JavaScript methods for validating form controls.
  • Feedback messages may utilize the browser defaults (different for each browser, and unstylable via CSS) or our custom feedback styles with additional HTML and CSS.
  • You may provide custom validity messages with setCustomValidity in JavaScript.

With that in mind, consider the following demos for our custom form validation styles, optional server side classes, and browser defaults.

Custom styles

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.

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.

Forms - 图30

  1. <form class="needs-validation" novalidate>
  2. <div class="form-row">
  3. <div class="col-md-6 mb-3">
  4. <label for="validationCustom01">First name</label>
  5. <input type="text" class="form-control" id="validationCustom01" value="Mark" required>
  6. <div class="valid-feedback">
  7. Looks good!
  8. </div>
  9. </div>
  10. <div class="col-md-6 mb-3">
  11. <label for="validationCustom02">Last name</label>
  12. <input type="text" class="form-control" id="validationCustom02" value="Otto" required>
  13. <div class="valid-feedback">
  14. Looks good!
  15. </div>
  16. </div>
  17. </div>
  18. <div class="form-row">
  19. <div class="col-md-6 mb-3">
  20. <label for="validationCustom03">City</label>
  21. <input type="text" class="form-control" id="validationCustom03" required>
  22. <div class="invalid-feedback">
  23. Please provide a valid city.
  24. </div>
  25. </div>
  26. <div class="col-md-3 mb-3">
  27. <label for="validationCustom04">State</label>
  28. <select class="custom-select" id="validationCustom04" required>
  29. <option selected disabled value="">Choose...</option>
  30. <option>...</option>
  31. </select>
  32. <div class="invalid-feedback">
  33. Please select a valid state.
  34. </div>
  35. </div>
  36. <div class="col-md-3 mb-3">
  37. <label for="validationCustom05">Zip</label>
  38. <input type="text" class="form-control" id="validationCustom05" required>
  39. <div class="invalid-feedback">
  40. Please provide a valid zip.
  41. </div>
  42. </div>
  43. </div>
  44. <div class="form-group">
  45. <div class="form-check">
  46. <input class="form-check-input" type="checkbox" value="" id="invalidCheck" required>
  47. <label class="form-check-label" for="invalidCheck">
  48. Agree to terms and conditions
  49. </label>
  50. <div class="invalid-feedback">
  51. You must agree before submitting.
  52. </div>
  53. </div>
  54. </div>
  55. <button class="btn btn-primary" type="submit">Submit form</button>
  56. </form>
  57. <script>
  58. // Example starter JavaScript for disabling form submissions if there are invalid fields
  59. (function() {
  60. 'use strict';
  61. window.addEventListener('load', function() {
  62. // Fetch all the forms we want to apply custom Bootstrap validation styles to
  63. var forms = document.getElementsByClassName('needs-validation');
  64. // Loop over them and prevent submission
  65. var validation = Array.prototype.filter.call(forms, function(form) {
  66. form.addEventListener('submit', function(event) {
  67. if (form.checkValidity() === false) {
  68. event.preventDefault();
  69. event.stopPropagation();
  70. }
  71. form.classList.add('was-validated');
  72. }, false);
  73. });
  74. }, false);
  75. })();
  76. </script>

Browser defaults

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.

While these feedback styles cannot be styled with CSS, you can still customize the feedback text through JavaScript.

Forms - 图31

  1. <form>
  2. <div class="form-row">
  3. <div class="col-md-6 mb-3">
  4. <label for="validationDefault01">First name</label>
  5. <input type="text" class="form-control" id="validationDefault01" value="Mark" required>
  6. </div>
  7. <div class="col-md-6 mb-3">
  8. <label for="validationDefault02">Last name</label>
  9. <input type="text" class="form-control" id="validationDefault02" value="Otto" required>
  10. </div>
  11. </div>
  12. <div class="form-row">
  13. <div class="col-md-6 mb-3">
  14. <label for="validationDefault03">City</label>
  15. <input type="text" class="form-control" id="validationDefault03" required>
  16. </div>
  17. <div class="col-md-3 mb-3">
  18. <label for="validationDefault04">State</label>
  19. <select class="custom-select" id="validationDefault04" required>
  20. <option selected disabled value="">Choose...</option>
  21. <option>...</option>
  22. </select>
  23. </div>
  24. <div class="col-md-3 mb-3">
  25. <label for="validationDefault05">Zip</label>
  26. <input type="text" class="form-control" id="validationDefault05" required>
  27. </div>
  28. </div>
  29. <div class="form-group">
  30. <div class="form-check">
  31. <input class="form-check-input" type="checkbox" value="" id="invalidCheck2" required>
  32. <label class="form-check-label" for="invalidCheck2">
  33. Agree to terms and conditions
  34. </label>
  35. </div>
  36. </div>
  37. <button class="btn btn-primary" type="submit">Submit form</button>
  38. </form>

Server side

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.

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.

Forms - 图32

  1. <form>
  2. <div class="form-row">
  3. <div class="col-md-6 mb-3">
  4. <label for="validationServer01">First name</label>
  5. <input type="text" class="form-control is-valid" id="validationServer01" value="Mark" required>
  6. <div class="valid-feedback">
  7. Looks good!
  8. </div>
  9. </div>
  10. <div class="col-md-6 mb-3">
  11. <label for="validationServer02">Last name</label>
  12. <input type="text" class="form-control is-valid" id="validationServer02" value="Otto" required>
  13. <div class="valid-feedback">
  14. Looks good!
  15. </div>
  16. </div>
  17. </div>
  18. <div class="form-row">
  19. <div class="col-md-6 mb-3">
  20. <label for="validationServer03">City</label>
  21. <input type="text" class="form-control is-invalid" id="validationServer03" aria-describedby="validationServer03Feedback" required>
  22. <div id="validationServer03Feedback" class="invalid-feedback">
  23. Please provide a valid city.
  24. </div>
  25. </div>
  26. <div class="col-md-3 mb-3">
  27. <label for="validationServer04">State</label>
  28. <select class="custom-select is-invalid" id="validationServer04" aria-describedby="validationServer04Feedback" required>
  29. <option selected disabled value="">Choose...</option>
  30. <option>...</option>
  31. </select>
  32. <div id="validationServer04Feedback" class="invalid-feedback">
  33. Please select a valid state.
  34. </div>
  35. </div>
  36. <div class="col-md-3 mb-3">
  37. <label for="validationServer05">Zip</label>
  38. <input type="text" class="form-control is-invalid" id="validationServer05" aria-describedby="validationServer05Feedback" required>
  39. <div id="validationServer05Feedback" class="invalid-feedback">
  40. Please provide a valid zip.
  41. </div>
  42. </div>
  43. </div>
  44. <div class="form-group">
  45. <div class="form-check">
  46. <input class="form-check-input is-invalid" type="checkbox" value="" id="invalidCheck3" aria-describedby="invalidCheck3Feedback" required>
  47. <label class="form-check-label" for="invalidCheck3">
  48. Agree to terms and conditions
  49. </label>
  50. <div id="invalidCheck3Feedback" class="invalid-feedback">
  51. You must agree before submitting.
  52. </div>
  53. </div>
  54. </div>
  55. <button class="btn btn-primary" type="submit">Submit form</button>
  56. </form>

Supported elements

Validation styles are available for the following form controls and components:

  • <input>s and <textarea>s with .form-control
  • <select>s with .form-control or .custom-select
  • .form-checks
  • .custom-checkboxs and .custom-radios
  • .custom-file

Forms - 图33

  1. <form class="was-validated">
  2. <div class="mb-3">
  3. <label for="validationTextarea">Textarea</label>
  4. <textarea class="form-control is-invalid" id="validationTextarea" placeholder="Required example textarea" required></textarea>
  5. <div class="invalid-feedback">
  6. Please enter a message in the textarea.
  7. </div>
  8. </div>
  9. <div class="custom-control custom-checkbox mb-3">
  10. <input type="checkbox" class="custom-control-input" id="customControlValidation1" required>
  11. <label class="custom-control-label" for="customControlValidation1">Check this custom checkbox</label>
  12. <div class="invalid-feedback">Example invalid feedback text</div>
  13. </div>
  14. <div class="custom-control custom-radio">
  15. <input type="radio" class="custom-control-input" id="customControlValidation2" name="radio-stacked" required>
  16. <label class="custom-control-label" for="customControlValidation2">Toggle this custom radio</label>
  17. </div>
  18. <div class="custom-control custom-radio mb-3">
  19. <input type="radio" class="custom-control-input" id="customControlValidation3" name="radio-stacked" required>
  20. <label class="custom-control-label" for="customControlValidation3">Or toggle this other custom radio</label>
  21. <div class="invalid-feedback">More example invalid feedback text</div>
  22. </div>
  23. <div class="mb-3">
  24. <select class="custom-select" required>
  25. <option value="">Choose...</option>
  26. <option value="1">One</option>
  27. <option value="2">Two</option>
  28. <option value="3">Three</option>
  29. </select>
  30. <div class="invalid-feedback">Example invalid custom select feedback</div>
  31. </div>
  32. <div class="custom-file mb-3">
  33. <input type="file" class="custom-file-input" id="validatedCustomFile" required>
  34. <label class="custom-file-label" for="validatedCustomFile">Choose file...</label>
  35. <div class="invalid-feedback">Example invalid custom file feedback</div>
  36. </div>
  37. <div class="mb-3">
  38. <div class="input-group is-invalid">
  39. <div class="input-group-prepend">
  40. <span class="input-group-text" id="validatedInputGroupPrepend">@</span>
  41. </div>
  42. <input type="text" class="form-control is-invalid" aria-describedby="validatedInputGroupPrepend" required>
  43. </div>
  44. <div class="invalid-feedback">
  45. Example invalid input group feedback
  46. </div>
  47. </div>
  48. <div class="mb-3">
  49. <div class="input-group is-invalid">
  50. <div class="input-group-prepend">
  51. <label class="input-group-text" for="validatedInputGroupSelect">Options</label>
  52. </div>
  53. <select class="custom-select" id="validatedInputGroupSelect" required>
  54. <option value="">Choose...</option>
  55. <option value="1">One</option>
  56. <option value="2">Two</option>
  57. <option value="3">Three</option>
  58. </select>
  59. </div>
  60. <div class="invalid-feedback">
  61. Example invalid input group feedback
  62. </div>
  63. </div>
  64. <div class="input-group is-invalid">
  65. <div class="custom-file">
  66. <input type="file" class="custom-file-input" id="validatedInputGroupCustomFile" required>
  67. <label class="custom-file-label" for="validatedInputGroupCustomFile">Choose file...</label>
  68. </div>
  69. <div class="input-group-append">
  70. <button class="btn btn-outline-secondary" type="button">Button</button>
  71. </div>
  72. </div>
  73. <div class="invalid-feedback">
  74. Example invalid input group feedback
  75. </div>
  76. </form>

Tooltips

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.

Forms - 图34

  1. <form class="needs-validation" novalidate>
  2. <div class="form-row">
  3. <div class="col-md-6 mb-3">
  4. <label for="validationTooltip01">First name</label>
  5. <input type="text" class="form-control" id="validationTooltip01" value="Mark" required>
  6. <div class="valid-tooltip">
  7. Looks good!
  8. </div>
  9. </div>
  10. <div class="col-md-6 mb-3">
  11. <label for="validationTooltip02">Last name</label>
  12. <input type="text" class="form-control" id="validationTooltip02" value="Otto" required>
  13. <div class="valid-tooltip">
  14. Looks good!
  15. </div>
  16. </div>
  17. </div>
  18. <div class="form-row">
  19. <div class="col-md-6 mb-3">
  20. <label for="validationTooltip03">City</label>
  21. <input type="text" class="form-control" id="validationTooltip03" required>
  22. <div class="invalid-tooltip">
  23. Please provide a valid city.
  24. </div>
  25. </div>
  26. <div class="col-md-3 mb-3">
  27. <label for="validationTooltip04">State</label>
  28. <select class="custom-select" id="validationTooltip04" required>
  29. <option selected disabled value="">Choose...</option>
  30. <option>...</option>
  31. </select>
  32. <div class="invalid-tooltip">
  33. Please select a valid state.
  34. </div>
  35. </div>
  36. <div class="col-md-3 mb-3">
  37. <label for="validationTooltip05">Zip</label>
  38. <input type="text" class="form-control" id="validationTooltip05" required>
  39. <div class="invalid-tooltip">
  40. Please provide a valid zip.
  41. </div>
  42. </div>
  43. </div>
  44. <button class="btn btn-primary" type="submit">Submit form</button>
  45. </form>

Customizing

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.

Please note that we do not recommend customizing these values without also modifying the form-validation-state mixin.

  1. // Sass map from `_variables.scss`
  2. // Override this and recompile your Sass to generate different states
  3. $form-validation-states: map-merge(
  4. (
  5. "valid": (
  6. "color": $form-feedback-valid-color,
  7. "icon": $form-feedback-icon-valid
  8. ),
  9. "invalid": (
  10. "color": $form-feedback-invalid-color,
  11. "icon": $form-feedback-icon-invalid
  12. )
  13. ),
  14. $form-validation-states
  15. );
  16. // Loop from `_forms.scss`
  17. // Any modifications to the above Sass map will be reflected in your compiled
  18. // CSS via this loop.
  19. @each $state, $data in $form-validation-states {
  20. @include form-validation-state($state, map-get($data, color), map-get($data, icon));
  21. }

Input group validation

To detect what elements need rounded corners inside an input group with validation, an input group requires an additional .has-validation class.

  1. <div class="input-group has-validation">
  2. <div class="input-group-prepend">
  3. <span class="input-group-text">@</span>
  4. </div>
  5. <input type="text" class="form-control" required>
  6. <div class="invalid-feedback">
  7. Please choose a username.
  8. </div>
  9. </div>

Forms - 图35

Custom forms

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.

Checkboxes and radios

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.

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.

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.

In the checked states, we use base64 embedded SVG icons from Open Iconic. This provides us the best control for styling and positioning across browsers and devices.

Checkboxes

Forms - 图36

  1. <div class="custom-control custom-checkbox">
  2. <input type="checkbox" class="custom-control-input" id="customCheck1">
  3. <label class="custom-control-label" for="customCheck1">Check this custom checkbox</label>
  4. </div>

Custom checkboxes can also utilize the :indeterminate pseudo class when manually set via JavaScript (there is no available HTML attribute for specifying it).

Forms - 图37

If you’re using jQuery, something like this should suffice:

  1. $('.your-checkbox').prop('indeterminate', true)

Radios

Forms - 图38

  1. <div class="custom-control custom-radio">
  2. <input type="radio" id="customRadio1" name="customRadio" class="custom-control-input">
  3. <label class="custom-control-label" for="customRadio1">Toggle this custom radio</label>
  4. </div>
  5. <div class="custom-control custom-radio">
  6. <input type="radio" id="customRadio2" name="customRadio" class="custom-control-input">
  7. <label class="custom-control-label" for="customRadio2">Or toggle this other custom radio</label>
  8. </div>

Inline

Forms - 图39

  1. <div class="custom-control custom-radio custom-control-inline">
  2. <input type="radio" id="customRadioInline1" name="customRadioInline" class="custom-control-input">
  3. <label class="custom-control-label" for="customRadioInline1">Toggle this custom radio</label>
  4. </div>
  5. <div class="custom-control custom-radio custom-control-inline">
  6. <input type="radio" id="customRadioInline2" name="customRadioInline" class="custom-control-input">
  7. <label class="custom-control-label" for="customRadioInline2">Or toggle this other custom radio</label>
  8. </div>

Disabled

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.

Forms - 图40

  1. <div class="custom-control custom-checkbox">
  2. <input type="checkbox" class="custom-control-input" id="customCheckDisabled1" disabled>
  3. <label class="custom-control-label" for="customCheckDisabled1">Check this custom checkbox</label>
  4. </div>
  5. <div class="custom-control custom-radio">
  6. <input type="radio" name="radioDisabled" id="customRadioDisabled2" class="custom-control-input" disabled>
  7. <label class="custom-control-label" for="customRadioDisabled2">Toggle this custom radio</label>
  8. </div>

Switches

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.

Forms - 图41

  1. <div class="custom-control custom-switch">
  2. <input type="checkbox" class="custom-control-input" id="customSwitch1">
  3. <label class="custom-control-label" for="customSwitch1">Toggle this switch element</label>
  4. </div>
  5. <div class="custom-control custom-switch">
  6. <input type="checkbox" class="custom-control-input" disabled id="customSwitch2">
  7. <label class="custom-control-label" for="customSwitch2">Disabled switch element</label>
  8. </div>

Select menu

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.

Forms - 图42

  1. <select class="custom-select">
  2. <option selected>Open this select menu</option>
  3. <option value="1">One</option>
  4. <option value="2">Two</option>
  5. <option value="3">Three</option>
  6. </select>

You may also choose from small and large custom selects to match our similarly sized text inputs.

Forms - 图43

  1. <select class="custom-select custom-select-lg mb-3">
  2. <option selected>Open this select menu</option>
  3. <option value="1">One</option>
  4. <option value="2">Two</option>
  5. <option value="3">Three</option>
  6. </select>
  7. <select class="custom-select custom-select-sm">
  8. <option selected>Open this select menu</option>
  9. <option value="1">One</option>
  10. <option value="2">Two</option>
  11. <option value="3">Three</option>
  12. </select>

The multiple attribute is also supported:

Forms - 图44

  1. <select class="custom-select" multiple>
  2. <option selected>Open this select menu</option>
  3. <option value="1">One</option>
  4. <option value="2">Two</option>
  5. <option value="3">Three</option>
  6. </select>

As is the size attribute:

Forms - 图45

  1. <select class="custom-select" size="3">
  2. <option selected>Open this select menu</option>
  3. <option value="1">One</option>
  4. <option value="2">Two</option>
  5. <option value="3">Three</option>
  6. </select>

Range

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.

Forms - 图46

  1. <label for="customRange1">Example range</label>
  2. <input type="range" class="custom-range" id="customRange1">

Range inputs have implicit values for min and max0 and 100, respectively. You may specify new values for those using the min and max attributes.

Forms - 图47

  1. <label for="customRange2">Example range</label>
  2. <input type="range" class="custom-range" min="0" max="5" id="customRange2">

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".

Forms - 图48

  1. <label for="customRange3">Example range</label>
  2. <input type="range" class="custom-range" min="0" max="5" step="0.5" id="customRange3">

File browser

The recommended plugin to animate custom file input: bs-custom-file-input, that’s what we are using currently here in our docs.

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.

Forms - 图49

  1. <div class="custom-file">
  2. <input type="file" class="custom-file-input" id="customFile">
  3. <label class="custom-file-label" for="customFile">Choose file</label>
  4. </div>

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.

Translating or customizing the strings with SCSS

The :lang() pseudo-class 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 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):

  1. $custom-file-text: (
  2. en: "Browse",
  3. es: "Elegir"
  4. );

Here’s lang(es) in action on the custom file input for a Spanish translation:

Forms - 图50

  1. <div class="custom-file">
  2. <input type="file" class="custom-file-input" id="customFileLang" lang="es">
  3. <label class="custom-file-label" for="customFileLang">Seleccionar Archivo</label>
  4. </div>

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 on the <html> element or the Content-Language HTTP header, among other methods.

Translating or customizing the strings with HTML

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):

Forms - 图51

  1. <div class="custom-file">
  2. <input type="file" class="custom-file-input" id="customFileLangHTML">
  3. <label class="custom-file-label" for="customFileLangHTML" data-browse="Bestand kiezen">Voeg je document toe</label>
  4. </div>