Creating responsive forms is a common task in web development, and Bootstrap, with its powerful grid system, simplifies this process significantly. One popular layout for forms is the horizontal form, which can be divided into two columns for better organization and readability. In this article, we'll guide you through creating a Bootstrap horizontal form with two columns.

Before we dive into the code, let's briefly discuss why you might want to use a horizontal form. Horizontal forms, also known as block forms, allow for better alignment of labels and inputs, making the form easier to scan and understand. They're particularly useful for longer or more complex forms.

Setting Up the Bootstrap Grid
The first step in creating a horizontal form is to set up the Bootstrap grid. Bootstrap uses a 12-column grid system, which we'll use to create two equal columns for our form.

We'll wrap our form in a container and then use the grid system to create two columns. Each column will take up 6 columns (or 'col-md-6' in Bootstrap's responsive grid system).
Creating the Form Container

First, we'll create a container for our form. In Bootstrap, a container is a responsive wrapper with some padding and a max-width. We'll use the default container class, 'container', which has a max-width of 1140px at the medium screen size.
Inside this container, we'll create a row using the 'row' class. This will allow us to use the grid system to create our columns.
Creating the Two Columns

Next, we'll create two columns using the 'col-md-6' class. This will make each column take up half the width of the container on medium and larger screens. On smaller screens, the columns will stack on top of each other due to Bootstrap's responsive design.
Inside each column, we'll start adding our form elements.
Adding Form Elements

Now that we have our grid set up, we can start adding form elements. In Bootstrap, form elements are styled using the 'form-control' class. This adds some basic styles and ensures that the inputs are the full width of their containing element.
We'll also use Bootstrap's form validation styles to make our form more user-friendly. This includes adding the 'is-valid' and 'is-invalid' classes to inputs based on their validity.




















Labels and Inputs
For each form element, we'll use a label with the 'for' attribute set to the input's ID. This ensures that the label is associated with the input and that clicking the label focuses the input.
We'll also use the 'form-group' class to add some padding and margin around our inputs. This helps to separate the inputs and makes the form easier to read.
Form Validation
To add form validation, we'll use the 'is-valid' and 'is-invalid' classes on our inputs. We'll also add an 'invalid-feedback' element to provide feedback to the user when an input is invalid.
For example, we might have an input like this:
<div class="form-group"> <label for="email">Email address</label> <input type="email" class="form-control is-invalid" id="email" placeholder="Enter email"> <div class="invalid-feedback">Please provide a valid email address.</div> </div>
Arranging Form Elements
With our grid and form elements set up, we can start arranging our form elements. In a horizontal form, labels and inputs are typically placed side by side. We can achieve this by using Bootstrap's inline-form class.
The 'form-inline' class removes the default vertical alignment and padding from form elements, allowing them to be placed side by side.
Aligning Labels and Inputs
To align our labels and inputs, we'll use the 'form-row' class to create a new row for each group of form elements. Inside this row, we'll use the 'col-form-label' and 'col' classes to align the label and input.
For example:
<div class="form-row"> <label for="firstName" class="col-form-label col-md-3">First name</label> <input type="text" class="form-control col-md-9" id="firstName" placeholder="First name"> </div>
Arranging Form Elements Vertically
While horizontal forms are great for long forms, sometimes we want to arrange our form elements vertically. We can do this by removing the 'form-inline' class and using the default vertical alignment of form elements.
We can also use Bootstrap's flexbox utilities to align form elements vertically. For example, we can use the 'align-items-center' class on a flex container to vertically center its children.
And that's it! With these steps, you should be able to create a Bootstrap horizontal form with two columns. This layout can help to make your forms more organized and easier to use. As always, don't forget to test your form thoroughly to ensure that it works as expected on all devices.