This tutorial aims to guide you through the process of creating responsive form layouts using Bootstrap's powerful grid system.
By the end of this tutorial, you will be able to:
Bootstrap's grid system uses rows and columns to layout and align content. It's built with flexbox and is fully responsive. The grid system splits the screen into 12 equal columns, and you can decide how many columns a particular element should span.
Bootstrap provides various classes to create different types of form layouts such as vertical, horizontal, and inline forms.
<form>
<div class="mb-3">
<label for="exampleInputEmail1" class="form-label">Email address</label>
<input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp">
<div id="emailHelp" class="form-text">We'll never share your email with anyone else.</div>
</div>
<div class="mb-3">
<label for="exampleInputPassword1" class="form-label">Password</label>
<input type="password" class="form-control" id="exampleInputPassword1">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
In the above example, mb-3
is a margin utility class that adds some space at the bottom. form-label
and form-control
are form specific classes that style the form elements.
<form>
<div class="row">
<div class="col-md-6">
<div class="mb-3">
<label for="firstName" class="form-label">First Name</label>
<input type="text" class="form-control" id="firstName">
</div>
</div>
<div class="col-md-6">
<div class="mb-3">
<label for="lastName" class="form-label">Last Name</label>
<input type="text" class="form-control" id="lastName">
</div>
</div>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
In this example, we've used the grid system to create a responsive form layout. col-md-6
means the column will span 6 out of 12 columns on medium to extra large screens, and will span 12 columns (full width) on small screens.
For further practice, try creating different types of form layouts and make them responsive.