Bootstrap Forms
In Bootstrap, there are three types of form layouts
- Vertical form (this is default)
- Horizontal form
- Inline form
Standard rules for all three form layouts:
- Wrap labels and form controls in <div class="form-group"> (needed for optimum spacing)
- Add class .form-control to all textual <input>, <textarea>, and <select> elements
Bootstrap Vertical Form (default)
The basic form structure comes with Bootstrap; individual form controls automatically receive some global styling. To create a basic form do the following :
- Add a role form to the parent <form> element.
- Wrap labels and controls in a <div> with class .form-group. This is needed for optimum spacing.
- Add a class of .form-control to all textual <input>, <textarea>, and <select> elements
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">WebSiteLogo</a>
</div>
<ul class="nav navbar-nav">
<li class="active"><a href="#">Home</a></li>
<li><a href="#">Menu 1</a></li>
<li><a href="#">Menu 2</a></li>
<li><a href="#">Menu 3</a></li>
</ul>
</div>
</nav>
Bootstrap Inline Form
In Bootstrap Inline forms, all elements are inline, left-aligned, and the labels are alongside.
<form class="form-inline">
<div class="form-group">
<label for="email">Email address:</label>
<input type="email" class="form-control" id="email">
</div>
<div class="form-group">
<label for="pwd">Password:</label>
<input type="password" class="form-control" id="pwd">
</div>
<div class="checkbox">
<label><input type="checkbox"> Remember me</label>
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
Bootstrap Horizontal Form
To create a form that uses the horizontal layout, do the following :
- Add a class of .form-horizontal to the parent <form> element.
- Wrap labels and controls in a <div> with class .form-group.
- Add a class of .control-label to the labels.
<form class="form-inline">
<div class="form-group">
<label for="email">Email address:</label>
<input type="email" class="form-control" id="email">
</div>
<div class="form-group">
<label for="pwd">Password:</label>
<input type="password" class="form-control" id="pwd">
</div>
<div class="checkbox">
<label><input type="checkbox"> Remember me</label>
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>