Form layout
Widths
By default, .form-control
s are block-level elements set to be 100% width of their parent.
The width of form inputs should be adjusted to visually match the character size of the information expected for the user to enter. For example, the average email address is going to be lengthier than a postal code therefore their input widths should be adjusted accordingly. This type of consideration allows for a better user experience when filling in the form.
There are several ways the widths can be controlled. It is easiest to start when the form itself has a width applied. This sets the boundaries for all of the form inputs inside.
Form widths
Please DOApply either of the width classes form-width-md
or form-width-lg
to the <form>
element.
For demonstration purposes, the following <form>
s are given a dashed border to visualize their width boundaries.
The following form has the form-width-md
class applied.
The following form has the form-width-lg
class applied.
Code
<!-- Use `form-width-md` for a medium form width. Or use `form-width-lg` for a slightly larger form width. -->
<form class="{{ form-width-md | form-width-lg }}">
<div class="form-group">
<label for="{{ Input id }}">{{ Label }}</label>
<input id="{{ Input id }}" name="{{ Input name }}" type="text" class="form-control">
</div>
</form>
Input widths
Certain types of inputs like names, phone numbers, and postal codes require only a handful of characters. It is recommended to apply sizing classes to these kinds of <input>
s to give them an appropriate width relative to the input you are expecting. This provides a visual cue for the user on the length of information they should enter.
Note: These input width classes are best applied when the inputs are on their own row or line. When using a form grid layout, there may not be a need to use these input width classes.
For demonstration purposes, the following <form>
has been given a dashed border to visualize its width boundaries.
Code
<!-- Use `form-width-md` for a medium form width. Or use `form-width-lg` for a slightly larger form width. -->
<form class="{{ form-width-md | form-width-lg }}">
<div class="form-group">
<label for="{{ Input id }}">{{ Label }}</label>
<!-- Use the input-width class that best suits the input.
Or don't add the input-width classes to keep the width of the input 100%. -->
<input id="{{ Input id }}" name="{{ Input name }}" type="text" class="form-control {{ input-width-lg | input-width-md | input-width-sm }}">
</div>
</form>
Form groups
Use the .form-group
class to group labels, inputs, optional help text, and form validation messaging. The class contains styles for margin-bottom
spacing.
Code
<form class="form-width-md">
<div class="form-group">
<label for="{{ Input id }}">{{ Label }}</label>
<input id="{{ Input id }}" name="{{ Input name }}" type="text" class="form-control">
</div>
<div class="form-group">
<label for="{{ Input id }}">{{ Label }}</label>
<input id="{{ Input id }}" name="{{ Input name }}" type="text" class="form-control">
</div>
</form>
Form sections
Use the .form-section
class to group multiple inputs or .form-group
s. The class contains larger margin-bottom
spacing compared to the .form-group
.
Add headings to label the sections.
Code
<form class="form-width-md">
<fieldset class="form-section">
<legend class="h4">{{ Legend }}</legend>
<div class="form-group">
...
</div>
<div class="form-group">
...
</div>
</fieldset>
<fieldset class="form-section">
...
</fieldset>
</form>
Form grid
The responsive Boostrap grid system can be used to create form layouts with multiple inputs per row.
Please DOGroup related inputs together.
Code
<form class="form-width-md">
<h2>{{ Heading }}</h2>
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<label for="{{ Input id }}">{{ Label }}</label>
<input id="{{ Input id }}" name="{{ Input name }}" type="text" class="form-control">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for="{{ Input id }}">{{ Label }}</label>
<input id="{{ Input id }}" name="{{ Input name }}" type="text" class="form-control">
</div>
</div>
</div>
<h2>{{ Heading }}</h2>
<div class="row">
<div class="col-sm-4">
<div class="form-group">
<label for="{{ Input id }}">{{ Label }}</label>
<input id="{{ Input id }}" name="{{ Input name }}" type="text" class="form-control">
</div>
</div>
<div class="col-sm-8">
<div class="form-group">
<label for="{{ Input id }}">{{ Label }}</label>
<input id="{{ Input id }}" name="{{ Input name }}" type="text" class="form-control">
</div>
</div>
</div>
</form>
Do not group un-related inputs together.
Form row
You can also swap .row
with .form-row
, a variation of the standard grid row with tighter gutter spaces. Use this class for tighter and more compact form layouts.
Code
<form>
<div class="form-row">
<div class="col-sm-6">
<div class="form-group">
<label for="{{ Input id }}">{{ Label }}</label>
<input id="{{ Input id }}" name="{{ Input name }}" type="text" class="form-control">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for="{{ Input id }}">{{ Label }}</label>
<input id="{{ Input id }}" name="{{ Input name }}" type="text" class="form-control">
</div>
</div>
</div>
</form>