8.bootstrap Forms
8.bootstrap Forms
In bootstrap, forms are useful to apply some default styles for form controls such as input
elements, labels, textareas, select elements, etc., with a predefined set of classes.
By using bootstrap predefined .form-control class, we can style textual form controls such
as <input>, <select> and <textarea> elements. Same way, we need to use other predefined
classes such as .form-check-input, .form-control-fileEtc. to style checkboxes, file inputs, etc.,
based on our requirements.
The bootstrap has provided three types of form layouts: default (vertical), horizontal, and inline
forms. Now, we will learn how we can create different form layouts with different input controls.
Following is the example of creating the default (vertical) form layout with required form controls
in bootstrap.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<style>
.bcontent {
margin-top: 10px;
}
</style>
</head>
<body>
<h2>Bootstrap Forms</h2>
<hr />
<form>
<div class="form-group">
<label for="email">Email</label>
</div>
<div class="form-group">
<label for="password">Password</label>
</div>
<div class="form-group">
</div>
</div>
</form>
</div>
</body>
</html>
Output:
To create horizontal forms, you need to add .row class to form groups and add .col-*-* classes
to your labels and controls to specify the width. Also, be sure to add .col-form-label to
your <label> elements to align vertically centered to their associated form controls.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<style>
.bcontent {
margin-top: 10px;
</style>
</head>
<body>
<hr />
<form>
<div class="col-sm-10">
</div>
</div>
<div class="col-sm-10">
</div>
</div>
<div class="col-sm-10">
<div class="form-check">
</div>
<div class="form-check">
</div>
</div>
</div>
<div class="col-sm-10">
</div>
</div>
<div class="form-check">
</div>
</div>
</div>
</div>
</div>
</form>
</div>
</body>
</html>
Bootstrap Inline Forms
In bootstrap, if you want to show all the form elements such as labels, input controls, etc., on a
single horizontal row, then you need to use inline forms.
Generally, the inline forms will display all the labels and form controls on the same line only when
viewports that are at least 576px wide otherwise, the form controls will appear horizontally.
In bootstrap, we can create the inline forms by adding .form-inline class to <form> element.
Following is the example of creating the inline form with required form controls in bootstrap.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<style>
.bcontent {
margin-top: 10px;
</style>
</head>
<body>
<hr />
<form class="form-inline">
<div class="form-check">
</div>
</form>
</div>
</body>
</html>
If you observe the above result, the inline form has arranged the controls without having any
spaces, and it looks like a compressed form. In inline form, we need to use bootstrap spacing
utility classes to align the controls properly.
Following is the example of using the bootstrap spacing utility classes to align the inline form
controls properly.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<style>
.bcontent {
margin-top: 10px;
</style>
</head>
<body>
<hr />
<form class="form-inline">
</form>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<style>
.bcontent {
margin-top: 10px;
</style>
</head>
<body>
<div class="container-fluid bcontent">
<hr />
<form class="was-validated">
<div class="form-group">
<div class="valid-feedback">Valid.</div>
</div>
<div class="form-group">
<div class="valid-feedback">Valid.</div>
</div>
<div class="form-group">
<label for="email">Email</label>
<div class="valid-feedback">Valid.</div>
</div>
<div class="valid-feedback">Valid.</div>
</div>
</form>
</div>
</body>
</html>