Optional Form Layouts
With a few helper classes, you can dynamically update the layout of your form. Bootstrap comes with a few preset styles to choose from.
Search form
Add .form-search to the <form> tag, and then add .search-query to the <input> for an input box with rounded corners and an inline submit button.
<!DOCTYPE html> <html> <head> <title>Bootstrap Tutorial</title> <link href="css/bootstrap.min.css" rel="stylesheet"> </head> <body> <body> <form class="form-search"> <input type="text" class="input-medium search-query"> <button type="submit" class="btn">Search</button> </form> </body> </html>
Inline form
To create a form where all of the elements are inline and labels are alongside, add the class .form-inline to the <form> tag. To have the label and the input on the same line, use this inline form code:
<!DOCTYPE html> <html> <head> <title>Bootstrap Tutorial</title> <link href="css/bootstrap.min.css" rel="stylesheet"> </head> <body> <body> <form class="form-inline"> <input type="text" class="input-small" placeholder="Email"> <input type="password" class="input-small" placeholder="Password"> <label class="checkbox"> <input type="checkbox"> Remember me </label> <button type="submit" class="btn">Sign in</button> </form> </body> </html>
Horizontal form
Bootstrap also comes with a prebaked horizontal form; this one stands apart from the others not only in the amount of markup, but also in the presentation of the form. Traditionally you’d use a table to get a form layout like the one shown in below example but Bootstrap manages to do it without using tables. Even better, if you’re using the responsive CSS, the horizontal form will automatically adapt to smaller layouts by stacking the controls vertically.
-
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 .control-group.
- Add a class of .control-label to the labels.
- Wrap any associated controls in a <div> with class .controls for proper alignment.
Example
<!DOCTYPE html> <html> <head> <title>Bootstrap Tutorial</title> <link href="css/bootstrap.min.css" rel="stylesheet"> </head> <body> <body> <form class="form-horizontal"> <div class="control-group"> <label class="control-label" for="inputEmail">Email</label> <div class="controls"> <input type="text" id="inputEmail" placeholder="Email"> </div> </div> <div class="control-group"> <label class="control-label" for="inputPassword">Password</label> <div class="controls"> <input type="password" id="inputPassword" placeholder="Password"> </div> </div> <div class="control-group"> <div class="controls"> <label class="checkbox"> <input type="checkbox"> Remember me </label> <button type="submit" class="btn">Sign in</button> </div> </div> </form> </body> </html>