This is a guide to writing Bootstrap 3 form syntax. It is designed to:
The official docs are a great reference. This guide is an introduction to the fundamentals, with a focus on clarity, and lots of side-by-side examples.
We've built a drag-and-drop form builder that generates valid Bootstrap form HTML. Free to use, no sign up required.
To use Bootstrap, you must structure your form a certain way and apply CSS classes to your form elements.
There are 3 important rules:
<div>
with .form-group
<label>
should be given .control-label
<input>
should be given .form-control
Below is a stripped down example to illustrate the structure:
<form>
<div class="form-group">
<label class="control-label"></label>
<input class="form-control">
</div>
</form>
Here are some simple examples that follow the standard layout.
<form>
<div class="form-group"> <!-- Email field !-->
<label for="email_id" class="control-label">Email</label>
<input type="email" class="form-control" id="email_id" name="email_name" placeholder="[email protected]">
</div>
<div class="form-group"> <!-- Submit button !-->
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
<form>
<div class="form-group"> <!-- Message input !-->
<label class="control-label " for="message_id">Message</label>
<textarea class="form-control" id="message_id" name="message" rows="5"></textarea>
</div>
<div class="form-group"> <!-- Submit button !-->
<button class="btn btn-primary " name="submit" type="submit">Submit</button>
</div>
</form>
<form>
<div class="form-group"> <!-- Message input !-->
<label class="control-label " for="region_id">Message</label>
<select class="form-control" id="region_id" name="region">
<option value="North">North</option>
<option value="West">West</option>
<option value="East">East</option>
<option value="South">South</option>
</select>
</div>
<div class="form-group"> <!-- Submit button !-->
<button class="btn btn-primary " name="submit" type="submit">Submit</button>
</div>
</form>
<label>
should contain a for
value that matches the corresponding <input>
's id
. This way, when the <label>
is clicked, the cursor moves to the <input>
.<input>
must contain an appropriate HTML5 type (e.g. text
, email
, url
, tel
, number
etc.)inputs
are given a width of 100%. You can use the Bootstrap grid system to adjust the form's width.Checkboxes and radioshave a unique structure. This is because the <label>
can play two different roles:
Therefore, to create one or more radio or checkbox follow these steps (working from child to parent):
<input>
for each item in your list<input>
with its own <label>
(no class is needed)<label>
in Step 2 with a <div>
and give it the class .radio
(or .checkbox
) <label>
and give it the class .control-label
<div>
and give it the class .form-group
<form>
<div class="form-group"> <!-- Radio group !-->
<label class="control-label">What is your favorite food?</label>
<div class="radio">
<label>
<input type="radio" name="fav_foods" value="pizza">
Pizza
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="fav_foods" value="hamburger">
Hamburgers
</label>
</div>
</div>
<div class="form-group"> <!-- Submit button !-->
<button class="btn btn-primary " name="submit" type="submit">Submit</button>
</div>
</form>
<form>
<div class="form-group"> <!-- Checkbox Group !-->
<label class="control-label">What is your favorite food?</label>
<div class="checkbox">
<label>
<input type="checkbox" name="fav_foods" value="pizza">
Pizza
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="fav_foods" value="hamburger">
Hamburgers
</label>
</div>
</div>
<div class="form-group"> <!-- Submit button !-->
<button class="btn btn-primary " name="submit" type="submit">Submit</button>
</div>
</form>
<form>
<div class="form-group"> <!-- Terms Checkbox !-->
<div class="checkbox">
<label>
<input type="checkbox" name="fav_foods" value="pizza">
I agree to the terms of use.
</label>
</div>
</div>
<div class="form-group"> <!-- Submit button !-->
<button class="btn btn-primary " name="submit" type="submit">Submit</button>
</div>
</form>
<label>
's in this example do not require a for
attribute. This is because:
<label>
does not correspond to a single <input>
<label>
already wraps it's <input>
(communicating ownership)By default, label's are stacked on the line above inputs. There are two other options:
.form-horizontal
to the <form>
.form-horizontal
to the <form>
.col-sm-2
) to each <label>
specifying its width<div>
and give each a grid class (e.g. .col-sm-10
)
<form class="form-inline">
<div class="form-group">
<label for="email_id" class="control-label">Email</label>
<input type="email" class="form-control" id="email_id" name="email_name" placeholder="[email protected]">
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
<form class="form-horizontal">
<div class="form-group">
<label for="email_id" class="control-label col-sm-2">Email</label>
<div class="col-sm-10"> <!-- This is a new div -->
<input type="email" class="form-control" id="email_id" name="email_name" placeholder="[email protected]">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2"> <!--New div, offset because there is no label -->
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
<form>
<div class="form-group"> <!-- Email field !-->
<label for="email_id" class="control-label">Email</label>
<input type="email" class="form-control" id="email_id" name="email_name" placeholder="[email protected]">
</div>
<div class="form-group"> <!-- Submit button !-->
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
<label>
(the <button>
in this case), need an offset-grid class (e.g. .col-sm-offset-2
) so that spacing is consistent.If a field is invalid, you can clearly communicate this to your user by making the entire field (border and text) red. To do this:
.has-error
to the .form-group
div<p>
with the class .help-text
to explain the problem
<form>
<div class="form-group has-error"> <!-- Email field !-->
<label for="email_id" class="control-label">Email</label>
<input type="email" class="form-control" id="email_id" name="email_name" value="support2formden.com" aria-describedby="email_help">
<p id="email_help" class="help-block">You must include a valid email address.</p>
</div>
<div class="form-group"> <!-- Submit button !-->
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
<form>
<div class="form-group"> <!-- Email field !-->
<label for="email_id" class="control-label">Email</label>
<input type="email" class="form-control" id="email_id" name="email_name" placeholder="[email protected]">
</div>
<div class="form-group"> <!-- Submit button !-->
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
aria-describedby
to the input with a value that corresponds to the .help-block
element's id
.has-warning
(orange) and .has-success
(green). Learn more.Here are some common examples to get you started. In these example, because the code is so long, the example is shown first.
<form>
<div class="form-group"> <!-- Name field -->
<label class="control-label " for="name">Name</label>
<input class="form-control" id="name" name="name" type="text"/>
</div>
<div class="form-group"> <!-- Email field -->
<label class="control-label requiredField" for="email">Email<span class="asteriskField">*</span></label>
<input class="form-control" id="email" name="email" type="text"/>
</div>
<div class="form-group"> <!-- Subject field -->
<label class="control-label " for="subject">Subject</label>
<input class="form-control" id="subject" name="subject" type="text"/>
</div>
<div class="form-group"> <!-- Message field -->
<label class="control-label " for="message">Message</label>
<textarea class="form-control" cols="40" id="message" name="message" rows="10"></textarea>
</div>
<div class="form-group">
<button class="btn btn-primary " name="submit" type="submit">Submit</button>
</div>
</form>
<form class="form-inline">
<div class="form-group"> <!-- Username field -->
<input class="form-control" id="username_id" name="user_name" type="text" placeholder="Username"/>
</div>
<div class="form-group"> <!-- Password field -->
<input class="form-control" id="password_id" name="user_password" type="password" placeholder="Password"/>
</div>
<div class="form-group"> <!-- Login button -->
<button class="btn btn-primary " name="submit" type="submit">Login</button>
</div>
<div class="form-group"> <!-- Remember Field -->
<div class="checkbox">
<label class="checkbox">
<input name="remember" type="checkbox" value="yes"/>
Remember
</label>
</div>
</div>
</form>
<form class="form-horizontal">
<div class="form-group"> <!-- Full Name -->
<label for="full_name_id" class="control-label col-sm-2">Full Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="full_name_id" name="full_name" placeholder="John Deer">
</div>
</div>
<div class="form-group"> <!-- Email -->
<label for="email_id" class="control-label col-sm-2">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email_id" name="email_name" placeholder="[email protected]">
<p class="help-text">We value your privacy and will never sell your email address.</p>
</div>
</div>
<div class="form-group"> <!-- Frequency Field -->
<label class="control-label col-sm-2">Email Me</label>
<div class="col-sm-10">
<div class="radio">
<label class="radio">
<input name="email_frequency" type="radio" value="day"/>
Daily
</label>
</div>
<div class="radio">
<label class="radio">
<input name="email_frequency" type="radio" value="week"/>
Weekly
</label>
</div>
<div class="radio">
<label class="radio">
<input name="email_frequency" type="radio" value="month"/>
Monthly
</label>
</div>
</div>
</div>
<div class="form-group"> <!-- Submit Button -->
<div class="col-sm-10 col-sm-offset-2">
<button type="submit" class="btn btn-primary">Get Updates!</button>
</div>
</div>
</form>
<form>
<div class="form-group"> <!-- Full Name -->
<label for="full_name_id" class="control-label">Full Name</label>
<input type="text" class="form-control" id="full_name_id" name="full_name" placeholder="John Deer">
</div>
<div class="form-group"> <!-- Street 1 -->
<label for="street1_id" class="control-label">Street Address 1</label>
<input type="text" class="form-control" id="street1_id" name="street1" placeholder="Street address, P.O. box, company name, c/o">
</div>
<div class="form-group"> <!-- Street 2 -->
<label for="street2_id" class="control-label">Street Address 2</label>
<input type="text" class="form-control" id="street2_id" name="street2" placeholder="Apartment, suite, unit, building, floor, etc.">
</div>
<div class="form-group"> <!-- City-->
<label for="city_id" class="control-label">City</label>
<input type="text" class="form-control" id="city_id" name="city" placeholder="Smallville">
</div>
<div class="form-group"> <!-- State Button -->
<label for="state_id" class="control-label">State</label>
<select class="form-control" id="state_id">
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
<option value="AR">Arkansas</option>
<option value="CA">California</option>
<option value="CO">Colorado</option>
<option value="CT">Connecticut</option>
<option value="DE">Delaware</option>
<option value="DC">District Of Columbia</option>
<option value="FL">Florida</option>
<option value="GA">Georgia</option>
<option value="HI">Hawaii</option>
<option value="ID">Idaho</option>
<option value="IL">Illinois</option>
<option value="IN">Indiana</option>
<option value="IA">Iowa</option>
<option value="KS">Kansas</option>
<option value="KY">Kentucky</option>
<option value="LA">Louisiana</option>
<option value="ME">Maine</option>
<option value="MD">Maryland</option>
<option value="MA">Massachusetts</option>
<option value="MI">Michigan</option>
<option value="MN">Minnesota</option>
<option value="MS">Mississippi</option>
<option value="MO">Missouri</option>
<option value="MT">Montana</option>
<option value="NE">Nebraska</option>
<option value="NV">Nevada</option>
<option value="NH">New Hampshire</option>
<option value="NJ">New Jersey</option>
<option value="NM">New Mexico</option>
<option value="NY">New York</option>
<option value="NC">North Carolina</option>
<option value="ND">North Dakota</option>
<option value="OH">Ohio</option>
<option value="OK">Oklahoma</option>
<option value="OR">Oregon</option>
<option value="PA">Pennsylvania</option>
<option value="RI">Rhode Island</option>
<option value="SC">South Carolina</option>
<option value="SD">South Dakota</option>
<option value="TN">Tennessee</option>
<option value="TX">Texas</option>
<option value="UT">Utah</option>
<option value="VT">Vermont</option>
<option value="VA">Virginia</option>
<option value="WA">Washington</option>
<option value="WV">West Virginia</option>
<option value="WI">Wisconsin</option>
<option value="WY">Wyoming</option>
</select>
</div>
<div class="form-group"> <!-- Zip Code-->
<label for="zip_id" class="control-label">Zip Code</label>
<input type="text" class="form-control" id="zip_id" name="zip" placeholder="#####">
</div>
<div class="form-group"> <!-- Submit Button -->
<button type="submit" class="btn btn-primary">Buy!</button>
</div>
</form>
© 2014-2020 Pareto Software, LLC | Blog | Bootstrap Form Builder