• New to browser forms? Consider reviewing 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

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.

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

  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.

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. </label>
  27. </div>
  28. <div class="form-check disabled">
  29. <input class="form-check-input" type="radio" name="gridRadios" id="gridRadios3" value="option3" disabled>
  30. <label class="form-check-label" for="gridRadios3">
  31. Third disabled radio
  32. </label>
  33. </div>
  34. </div>
  35. </fieldset>
  36. <div class="row mb-3">
  37. <div class="col-sm-10 offset-sm-2">
  38. <div class="form-check">
  39. <input class="form-check-input" type="checkbox" id="gridCheck1">
  40. <label class="form-check-label" for="gridCheck1">
  41. Example checkbox
  42. </label>
  43. </div>
  44. </div>
  45. </div>
  46. <button type="submit" class="btn btn-primary">Sign in</button>
  47. </form>

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.

  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

Layout - 图7

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.

  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. <input type="text" class="form-control" id="autoSizingInputGroup" placeholder="Username">
  10. </div>
  11. </div>
  12. <label class="visually-hidden" for="autoSizingSelect">Preference</label>
  13. <select class="form-select" id="autoSizingSelect">
  14. <option selected>Choose...</option>
  15. <option value="1">One</option>
  16. <option value="2">Two</option>
  17. <option value="3">Three</option>
  18. </select>
  19. </div>
  20. <div class="col-auto">
  21. <div class="form-check">
  22. <input class="form-check-input" type="checkbox" id="autoSizingCheck">
  23. <label class="form-check-label" for="autoSizingCheck">
  24. Remember me
  25. </label>
  26. </div>
  27. </div>
  28. <div class="col-auto">
  29. <button type="submit" class="btn btn-primary">Submit</button>
  30. </div>
  31. </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>

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.