Forms

Every group of form fields should reside in a <form> element. Bootstrap provides no default styling for the <form> element, but there are some powerful browser features that are provided by default.

  • New to browser forms? Consider reviewing the MDN form docs for an overview and complete list of available attributes.
  • <button>s within a <form> default to type="submit", so strive to be specific and always include a type.
  • You can disable every form element within a form with the disabled attribute on the <form>.

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.

Utilities

Margin utilities are the easiest way to add some structure to forms. They provide basic grouping of labels, controls, optional form text, and form validation messaging. We recommend sticking to margin-bottom utilities, and using a single direction throughout the form for consistency.

Feel free to build your forms however you like, with <fieldset>s, <div>s, or nearly any other element.

Layout - 图1

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

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. Requires the $enable-grid-classes Sass variable to be enabled (on by default).

Layout - 图2

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

Gutters

By adding gutter modifier classes, you can have control over the gutter width in as well the inline as block direction. Also requires the $enable-grid-classes Sass variable to be enabled (on by default).

Layout - 图3

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

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

Layout - 图4

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

Layout - 图5

  1. <form>
  2. <div class="row mb-3">
  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="row mb-3">
  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="row mb-3">
  15. <legend class="col-form-label col-sm-2 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="row mb-3">
  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. <button type="submit" class="btn btn-primary">Sign in</button>
  48. </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.

Layout - 图6

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

Column sizing

As shown in the previous examples, our grid system allows you to place any number of .cols within a .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-sm-7.

Layout - 图7

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

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.

Layout - 图8

  1. <form class="row gy-2 gx-3 align-items-center">
  2. <div class="col-auto">
  3. <label class="visually-hidden" for="autoSizingInput">Name</label>
  4. <input type="text" class="form-control" id="autoSizingInput" placeholder="Jane Doe">
  5. </div>
  6. <div class="col-auto">
  7. <label class="visually-hidden" for="autoSizingInputGroup">Username</label>
  8. <div class="input-group">
  9. <div class="input-group-text">@</div>
  10. <input type="text" class="form-control" id="autoSizingInputGroup" placeholder="Username">
  11. </div>
  12. </div>
  13. <div class="col-auto">
  14. <label class="visually-hidden" for="autoSizingSelect">Preference</label>
  15. <select class="form-select" id="autoSizingSelect">
  16. <option selected>Choose...</option>
  17. <option value="1">One</option>
  18. <option value="2">Two</option>
  19. <option value="3">Three</option>
  20. </select>
  21. </div>
  22. <div class="col-auto">
  23. <div class="form-check">
  24. <input class="form-check-input" type="checkbox" id="autoSizingCheck">
  25. <label class="form-check-label" for="autoSizingCheck">
  26. Remember me
  27. </label>
  28. </div>
  29. </div>
  30. <div class="col-auto">
  31. <button type="submit" class="btn btn-primary">Submit</button>
  32. </div>
  33. </form>

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

Layout - 图9

  1. <form class="row gx-3 gy-2 align-items-center">
  2. <div class="col-sm-3">
  3. <label class="visually-hidden" for="specificSizeInputName">Name</label>
  4. <input type="text" class="form-control" id="specificSizeInputName" placeholder="Jane Doe">
  5. </div>
  6. <div class="col-sm-3">
  7. <label class="visually-hidden" for="specificSizeInputGroupUsername">Username</label>
  8. <div class="input-group">
  9. <div class="input-group-text">@</div>
  10. <input type="text" class="form-control" id="specificSizeInputGroupUsername" placeholder="Username">
  11. </div>
  12. </div>
  13. <div class="col-sm-3">
  14. <label class="visually-hidden" for="specificSizeSelect">Preference</label>
  15. <select class="form-select" id="specificSizeSelect">
  16. <option selected>Choose...</option>
  17. <option value="1">One</option>
  18. <option value="2">Two</option>
  19. <option value="3">Three</option>
  20. </select>
  21. </div>
  22. <div class="col-auto">
  23. <div class="form-check">
  24. <input class="form-check-input" type="checkbox" id="autoSizingCheck2">
  25. <label class="form-check-label" for="autoSizingCheck2">
  26. Remember me
  27. </label>
  28. </div>
  29. </div>
  30. <div class="col-auto">
  31. <button type="submit" class="btn btn-primary">Submit</button>
  32. </div>
  33. </form>

Inline forms

Use the .col-auto class to create horizontal layouts. By adding gutter modifier classes, we’ll have gutters in horizontal and vertical directions. The .align-items-center aligns the form elements to the middle, making the .form-checkbox align properly.

Layout - 图10

  1. <form class="row row-cols-lg-auto g-3 align-items-center">
  2. <div class="col-12">
  3. <label class="visually-hidden" for="inlineFormInputGroupUsername">Username</label>
  4. <div class="input-group">
  5. <div class="input-group-text">@</div>
  6. <input type="text" class="form-control" id="inlineFormInputGroupUsername" placeholder="Username">
  7. </div>
  8. </div>
  9. <div class="col-12">
  10. <label class="visually-hidden" for="inlineFormSelectPref">Preference</label>
  11. <select class="form-select" id="inlineFormSelectPref">
  12. <option selected>Choose...</option>
  13. <option value="1">One</option>
  14. <option value="2">Two</option>
  15. <option value="3">Three</option>
  16. </select>
  17. </div>
  18. <div class="col-12">
  19. <div class="form-check">
  20. <input class="form-check-input" type="checkbox" id="inlineFormCheck">
  21. <label class="form-check-label" for="inlineFormCheck">
  22. Remember me
  23. </label>
  24. </div>
  25. </div>
  26. <div class="col-12">
  27. <button type="submit" class="btn btn-primary">Submit</button>
  28. </div>
  29. </form>