Forms
Layout
Stack fields, put them on the grid, or lay them out horizontally and inline — on the 0–8 gutter scale.
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 — including native validation, which this theme relies on.
- New to browser forms? Consider reviewing the MDN forms docs for an overview and complete list of available attributes.
<button>s within a<form>default totype="submit", so strive to be specific and always include atype.- You can disable every form element within a form with the
disabledattribute on the<form>'s<fieldset>.
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.
This theme's spacing scale is 0–8 on a 4 px base, so the "usual" 1 rem gap is .mb-4
(.mb-3 is .75rem). See Spacing.
HTML
<div class="mb-4">
<label for="layout-email" class="form-label">Email address</label>
<input type="email" class="form-control" id="layout-email" placeholder="[email protected]" />
</div>
<div class="mb-4">
<label for="layout-textarea" class="form-label">Promo</label>
<textarea class="form-control" id="layout-textarea" rows="3"></textarea>
</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).
HTML
<div class="row">
<div class="col">
<label for="layout-grid-first" class="visually-hidden">First name</label>
<input type="text" class="form-control" id="layout-grid-first" placeholder="First name" />
</div>
<div class="col">
<label for="layout-grid-last" class="visually-hidden">Last name</label>
<input type="text" class="form-control" id="layout-grid-last" placeholder="Last name" />
</div>
</div>Gutters
By adding gutter modifier classes, you can have control over the gutter width in as well the inline as block
direction. The scale is the theme's 0–8: .g-1 is .25rem, .g-4 is
1 rem, .g-8 is 4 rem. .gx-* and .gy-* set one axis each.
Also requires the $enable-grid-classes Sass variable to be enabled (on by
default).
HTML
<div class="row g-4">
<div class="col">
<label for="layout-gutter-first" class="visually-hidden">First name</label>
<input type="text" class="form-control" id="layout-gutter-first" placeholder="First name" />
</div>
<div class="col">
<label for="layout-gutter-last" class="visually-hidden">Last name</label>
<input type="text" class="form-control" id="layout-gutter-last" placeholder="Last name" />
</div>
</div>More complex layouts can also be created with the grid system.
HTML
<form class="row g-4">
<div class="col-md-6">
<label for="layout-grid-email" class="form-label">Email</label>
<input type="email" class="form-control" id="layout-grid-email" />
</div>
<div class="col-md-6">
<label for="layout-grid-password" class="form-label">Password</label>
<input type="password" class="form-control" id="layout-grid-password" />
</div>
<div class="col-12">
<label for="layout-grid-address" class="form-label">Address</label>
<input type="text" class="form-control" id="layout-grid-address" placeholder="1234 Main St" />
</div>
<div class="col-12">
<label for="layout-grid-address2" class="form-label">Address 2</label>
<input type="text" class="form-control" id="layout-grid-address2" placeholder="Apartment, studio, or floor" />
</div>
<div class="col-md-6">
<label for="layout-grid-city" class="form-label">City</label>
<input type="text" class="form-control" id="layout-grid-city" />
</div>
<div class="col-md-4">
<label for="layout-grid-state" class="form-label">State</label>
<select id="layout-grid-state" class="form-select">
<option selected>Choose…</option>
<option>…</option>
</select>
</div>
<div class="col-md-2">
<label for="layout-grid-zip" class="form-label">Zip</label>
<input type="text" class="form-control" id="layout-grid-zip" />
</div>
<div class="col-12">
<div class="form-check">
<input class="form-check-input" type="checkbox" id="layout-grid-check" />
<label class="form-check-label" for="layout-grid-check">Check me out</label>
</div>
</div>
<div class="col-12">
<button type="submit" class="btn btn-primary">Sign in</button>
</div>
</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.
HTML
<form>
<div class="row mb-4">
<label for="layout-h-email" class="col-sm-2 col-form-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="layout-h-email" />
</div>
</div>
<div class="row mb-4">
<label for="layout-h-password" class="col-sm-2 col-form-label">Password</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="layout-h-password" />
</div>
</div>
<fieldset class="row mb-4">
<legend class="col-form-label col-sm-2 pt-0">Alignment</legend>
<div class="col-sm-10">
<div class="form-check">
<input class="form-check-input" type="radio" name="layout-h-radios" id="layout-h-radio-1" value="face" checked />
<label class="form-check-label" for="layout-h-radio-1">Face</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" name="layout-h-radios" id="layout-h-radio-2" value="heel" />
<label class="form-check-label" for="layout-h-radio-2">Heel</label>
</div>
<div class="form-check disabled">
<input class="form-check-input" type="radio" name="layout-h-radios" id="layout-h-radio-3" value="tweener" disabled />
<label class="form-check-label" for="layout-h-radio-3">Tweener (disabled)</label>
</div>
</div>
</fieldset>
<div class="row mb-4">
<div class="col-sm-10 offset-sm-2">
<div class="form-check">
<input class="form-check-input" type="checkbox" id="layout-h-check" />
<label class="form-check-label" for="layout-h-check">Example checkbox</label>
</div>
</div>
</div>
<button type="submit" class="btn btn-primary">Sign in</button>
</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.
HTML
<div class="row mb-4">
<label for="layout-size-sm" class="col-sm-2 col-form-label col-form-label-sm">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control form-control-sm" id="layout-size-sm" placeholder="col-form-label-sm" />
</div>
</div>
<div class="row mb-4">
<label for="layout-size-default" class="col-sm-2 col-form-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="layout-size-default" placeholder="col-form-label" />
</div>
</div>
<div class="row">
<label for="layout-size-lg" class="col-sm-2 col-form-label col-form-label-lg">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control form-control-lg" id="layout-size-lg" placeholder="col-form-label-lg" />
</div>
</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.
HTML
<div class="row g-4">
<div class="col-sm-7">
<label for="layout-col-city" class="visually-hidden">City</label>
<input type="text" class="form-control" id="layout-col-city" placeholder="City" />
</div>
<div class="col-sm">
<label for="layout-col-state" class="visually-hidden">State</label>
<input type="text" class="form-control" id="layout-col-state" placeholder="State" />
</div>
<div class="col-sm">
<label for="layout-col-zip" class="visually-hidden">Zip</label>
<input type="text" class="form-control" id="layout-col-zip" placeholder="Zip" />
</div>
</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.
HTML
<form class="row gy-2 gx-4 align-items-center">
<div class="col-auto">
<label class="visually-hidden" for="layout-auto-name">Name</label>
<input type="text" class="form-control" id="layout-auto-name" placeholder="Ring name" />
</div>
<div class="col-auto">
<label class="visually-hidden" for="layout-auto-username">Username</label>
<div class="input-group">
<div class="input-group-text">@</div>
<input type="text" class="form-control" id="layout-auto-username" placeholder="Username" />
</div>
</div>
<div class="col-auto">
<div class="form-check">
<input class="form-check-input" type="checkbox" id="layout-auto-check" />
<label class="form-check-label" for="layout-auto-check">Remember me</label>
</div>
</div>
<div class="col-auto">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>You can then remix that once again with size-specific column classes.
HTML
<form class="row gx-4 gy-2 align-items-center">
<div class="col-sm-3">
<label class="visually-hidden" for="layout-auto-name-2">Name</label>
<input type="text" class="form-control" id="layout-auto-name-2" placeholder="Ring name" />
</div>
<div class="col-sm-3">
<label class="visually-hidden" for="layout-auto-username-2">Username</label>
<div class="input-group">
<div class="input-group-text">@</div>
<input type="text" class="form-control" id="layout-auto-username-2" placeholder="Username" />
</div>
</div>
<div class="col-sm-3">
<label class="visually-hidden" for="layout-auto-select">Preference</label>
<select class="form-select" id="layout-auto-select">
<option selected>Choose…</option>
<option value="1">Face</option>
<option value="2">Heel</option>
<option value="3">Tweener</option>
</select>
</div>
<div class="col-auto">
<div class="form-check">
<input class="form-check-input" type="checkbox" id="layout-auto-check-2" />
<label class="form-check-label" for="layout-auto-check-2">Remember me</label>
</div>
</div>
<div class="col-auto">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>Inline forms
Use the .row-cols-* classes to create responsive horizontal layouts. By adding gutter modifier
classes, we'll have gutters in horizontal and vertical directions. On narrow mobile viewports, the
.col-12 helps stack the form controls and more. The .align-items-center aligns the
form elements to the middle, making the .form-check align properly.
HTML
<form class="row row-cols-lg-auto g-4 align-items-center">
<div class="col-12">
<label class="visually-hidden" for="layout-inline-username">Username</label>
<div class="input-group">
<div class="input-group-text">@</div>
<input type="text" class="form-control" id="layout-inline-username" placeholder="Username" />
</div>
</div>
<div class="col-12">
<label class="visually-hidden" for="layout-inline-select">Preference</label>
<select class="form-select" id="layout-inline-select">
<option selected>Choose…</option>
<option value="1">Face</option>
<option value="2">Heel</option>
<option value="3">Tweener</option>
</select>
</div>
<div class="col-12">
<div class="form-check">
<input class="form-check-input" type="checkbox" id="layout-inline-check" />
<label class="form-check-label" for="layout-inline-check">Remember me</label>
</div>
</div>
<div class="col-12">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
For the grid itself — breakpoints, column classes and the .grid / .g-col-* CSS
grid alternative — see Grid and Gutters.