Form layout
Widths
By default, .form-control
s are block-level elements set to be 100% width of their parent.
Control the width of the form-controls by applying width classes to the <form>
or to the <input>
.
Form widths
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 illustration purposes, the following <form>
has been given a border to showcase its width boundaries.
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.
<form class="form-width-md">
<div class="form-group">
<label for="ex-group-username">Username</label>
<input id="ex-group-username" name="ex-group-username" type="text" class="form-control" aria-describedby="t-ex-group-username">
<p id="t-ex-group-username" class="form-text">Example helper text for the username input.</p>
</div>
<div class="form-group">
<label for="ex-group-password">Password</label>
<input id="ex-group-password" name="ex-group-password" 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.
<form class="form-width-md">
<section class="form-section">
<h3>Example form section heading</h3>
<div class="form-group">
…
</div>
<div class="form-group">
…
</div>
</section>
<section class="form-section">
<h3>Another example form section heading</h3>
<div class="form-group">
…
</div>
<div class="form-group">
…
</div>
</section>
</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">
<h4>Example 1</h4>
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<label for="ex-grid-first-name">First Name</label>
<input id="ex-grid-first-name" name="ex-grid-first-name" type="text" class="form-control">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for="ex-grid-last-name">Last Name</label>
<input id="ex-grid-last-name" name="ex-grid-last-name" type="text" class="form-control">
</div>
</div>
</div>
<h4>Example 2</h4>
<div class="row">
<div class="col-sm-4">
<div class="form-group">
<label for="ex-grid-unit">
<span class="field-name">Unit #</span>
</label>
<input id="ex-grid-unit" name="ex-grid-unit" type="text" class="form-control" maxlength="10">
</div>
</div>
<div class="col-sm-8">
<div class="form-group">
<label for="ex-grid-street-address">
<span class="field-name">Street address</span>
<span class="label-required">(required)</span>
</label>
<input id="ex-grid-street-address" name="ex-grid-street-address" type="text" class="form-control" aria-required="true" required>
</div>
</div>
</div>
</form>
Please DON’T
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.
<form>
<div class="form-row">
<div class="col-sm-6">
<div class="form-group">
<label for="first-name">First Name</label>
<input id="first-name" name="first-name" type="text" class="form-control">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for="last-name">Last Name</label>
<input id="last-name" name="last-name" type="text" class="form-control">
</div>
</div>
</div>
</form>