How to align nested form rows using Bootstrap 4?
Last Updated :
26 Nov, 2020
Bootstrap is a CSS framework used to design web pages. Bootstrap along with HTML and JavaScript results in interactive web designs. The most recent release is Bootstrap v4.5. Bootstrap has a variety of components and utilities. The Bootstrap components include the form component. Bootstrap has an inbuilt form control class that is used to create forms and corresponding form input fields. The Bootstrap form component allows the creation of forms using nested rows. Rows and Columns are yet another utility in Bootstrap that can be used to align the nested form rows.
In this article, we shall see two examples to align the nested form rows of which one makes use of the form-group class whereas the other example uses the concept of rows and columns only to display the desired structure.
Example 1: In this example, the main container is defined which consists of the nested form rows. Within the container, a form-group row class is defined with a column of the width of 7 units. This column again consists of a form-group row which comprises two columns. The first column is used to specify the labels for the input field and the second column is used to define the input fields.
Since the column width is defined the form labels and input field do not exceed the column size and therefore remain aligned. The button is aligned with the input fields of the form by placing it in the second column of the nested row and keeping the first column vacant.
Code Implementation:
HTML
<!DOCTYPE html>
<html>
<head>
<!-- Import bootstrap cdn -->
<link rel="stylesheet" href=
"https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
integrity=
"sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2"
crossorigin="anonymous">
<!-- Import jquery cdn -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"
integrity=
"sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj"
crossorigin="anonymous">
</script>
<script src=
"https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity=
"sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/tE3MoK7ZeZDyx"
crossorigin="anonymous">
</script>
</head>
<body>
<div class="container mt-2 ml-2">
<div class="form-group row">
<div class="col-sm-7">
<div class="form-group row">
<label for="description"
class="col-sm-5 col-form-label">
Name
</label>
<div class="col-sm-7">
<input type="text"
class="form-control">
</div>
</div>
<div class="form-group row">
<label for="description"
class="col-sm-5 col-form-label">
Institute
</label>
<div class="col-sm-7">
<input type="text"
class="form-control">
</div>
</div>
<div class="form-group row">
<label for="description"
class="col-sm-5 col-form-label">
Degree
</label>
<div class="col-sm-7">
<input type="text"
class="form-control">
</div>
</div>
<div class="form-group row">
<label for="description"
class="col-sm-5 col-form-label">
Year of Graduation
</label>
<div class="col-sm-7">
<input type="text"
class="form-control">
</div>
</div>
<div class="form-group row">
<label for="description"
class="col-sm-5 col-form-label">
Diploma/Part Time/Full Time
</label>
<div class="col-sm-7">
<input type="text"
class="form-control">
</div>
</div>
<div class="row">
<div class="col-sm-5"></div>
<div class="col-sm-7">
<button type="button"
class="btn btn-success btn-md">
Proceed
</button>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
Output:
Example 2: In this example, the main container is defined which consists of the nested rows. Within the container, a row class is defined which has a column with a width of 7 units. This column again consists of a row that comprises two columns. The first column is used to specify the labels for the input field and the second column is used to define the input fields. Since the column width is defined the form labels and input field do not exceed the column size and therefore remain aligned. At last, a button is positioned in proper alignment with the input fields of the form. It is similar to the first approach except we do not make use of the form-group class.
Code Implementation:
HTML
<!DOCTYPE html>
<html>
<head>
<!-- Import bootstrap cdn -->
<link rel="stylesheet" href=
"https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
integrity=
"sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2"
crossorigin="anonymous">
<!-- Import jquery cdn -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"
integrity=
"sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj"
crossorigin="anonymous">
</script>
<script src=
"https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity=
"sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/tE3MoK7ZeZDyx"
crossorigin="anonymous">
</script>
</head>
<body>
<div class="container mt-2 ml-2">
<div class="row">
<div class="col-sm-7">
<div class="row mt-2">
<label for="description"
class="col-sm-5">
Name
</label>
<div class="col-sm-7">
<input type="text"
class="form-control">
</div>
</div>
<div class="row mt-2">
<label for="description"
class="col-sm-5">
Institute
</label>
<div class="col-sm-7">
<input type="text"
class="form-control">
</div>
</div>
<div class="row mt-2">
<label for="description"
class="col-sm-5">
Degree
</label>
<div class="col-sm-7">
<input type="text"
class="form-control">
</div>
</div>
<div class="row mt-2">
<label for="description"
class="col-sm-5">
Year of Graduation
</label>
<div class="col-sm-7">
<input type="text"
class="form-control">
</div>
</div>
<div class="row mt-2">
<label for="description"
class="col-sm-5">
Diploma/Part Time/Full Time
</label>
<div class="col-sm-7">
<input type="text"
class="form-control">
</div>
</div>
<div class="row mt-2">
<div class="col-sm-5"></div>
<div class="col-sm-7">
<button type="button"
class="btn btn-success btn-md">
Proceed
</button>
<button type="button"
class="btn btn-danger btn-md">
Reset
</button>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
Output:
Similar Reads
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
HTML Tutorial HTML stands for HyperText Markup Language. It is the standard language used to create and structure content on the web. It tells the web browser how to display text, links, images, and other forms of multimedia on a webpage. HTML sets up the basic structure of a website, and then CSS and JavaScript
11 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
JavaScript Interview Questions and Answers JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as
15+ min read
React Tutorial React is a JavaScript Library known for front-end development (or user interface). It is popular due to its component-based architecture, Single Page Applications (SPAs), and Virtual DOM for building web applications that are fast, efficient, and scalable.Applications are built using reusable compon
8 min read
Decorators in Python In Python, decorators are a powerful and flexible way to modify or extend the behavior of functions or methods, without changing their actual code. A decorator is essentially a function that takes another function as an argument and returns a new function with enhanced functionality. Decorators are
10 min read
Sliding Window Technique Sliding Window Technique is a method used to solve problems that involve subarray or substring or window. The main idea is to use the results of previous window to do computations for the next window. This technique is commonly used in algorithms like finding subarrays with a specific sum, finding t
13 min read
AVL Tree Data Structure An AVL tree defined as a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees for any node cannot be more than one. Example of an AVL Tree:The balance factors for different nodes are : 12 :1, 8:1, 18:1, 5:1, 11:0, 17:0 and 4:0. Since all differences
4 min read
Domain Name System (DNS) DNS is a hierarchical and distributed naming system that translates domain names into IP addresses. When you type a domain name like www.geeksforgeeks.org into your browser, DNS ensures that the request reaches the correct server by resolving the domain to its corresponding IP address.Without DNS, w
8 min read