Forms are used to gather information about a person, a product, or a company and are very vital in conducting surveys. Bootstrap forms are the combination of text fields, textareas, selection boxes, radio buttons, and checkboxes. Applying CSS manually on forms is a boring and time taking process that’s why bootstrap makes it easy for us with the help of predefined bootstrap classes.

In this article you get a detail knowledge on

How to create a form

To create a form use tag, inside this form tag use tag with the class .form-label for the title of a text field then use tag for creating different form fields as your requirement. Form fields are specified by the type=“*” attribute with the class .form-control to apply the default form setting from bootstrap.

  • Text
  • Password
  • Date
  • Datetime
  • File
  • Email
  • Number
  • Radio
  • Checkbox
  • Submit
  • Reset
  • Range

Replace one of these with * to create different fields.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/Bootstrap-5-Forms-Classes-Explained-1.png" data-lazy- height="378" src="data:image/svg xml,” width=”735″>

Code

<form class=“row”>


            <div class=“col-md-6”>


              <label class=“form-label”>First Name</label>


              <input type=“text” class=“form-control”>


            </div>


            <div class=“col-md-6”>


              <label class=“form-label”>Last Name</label>


              <input type=“text” class=“form-control”>


            </div>


            <div class=“col-md-12”>


              <label class=“form-label”>Address</label>


              <input type=“text” class=“form-control”>


            </div>


            <div class=“col-md-6”>


              <label class=“form-label”>City</label>


              <input type=“email” class=“form-control”>


            </div>


            <div class=“col-md-4”>


              <label class=“form-label”>States</label>


              <select name=“” id=“” class=“form-select”>


                <option value=“”>Punjab</option>


                <option value=“”>Sindh</option>


                <option value=“”>Kpk</option>


                <option value=“”>Balochistan</option>


              </select>


            </div>


            <div class=“col-md-2”>


              <label class=“form-label”>Zip</label>


              <input type=“text” class=“form-control”>


            </div>


            <div class=“col-md-6”>


              <label class=“form-label”>Upload Domicile</label>


              <input type=“file” class=“form-control”>


            </div>


            <div class=“col-md-6”>


              <label class=“form-label”>Gender</label>


              <br>


              <div class=“form-check form-check-inline”>


                <input type=“radio” class=“form-check-input” name=“rdo” checked>


                <label class=“form-check-label”>Male</label>


              </div>


              <div class=“form-check form-check-inline”>


                <input type=“radio” class=“form-check-input” name=“rdo”>


                <label class=“form-check-label”>Female</label>


              </div>


              <div class=“form-check form-check-inline”>


                <input type=“radio” class=“form-check-input” name=“rdo”>


                <label class=“form-check-label”>Other</label>


              </div>


            </div>


            <div class=“col-md-12”>


              <br>


              <button class=“btn btn-primary form-control”>Submit</button>


            </div>


        </form>

This is how a simple form is created.

Types

There are three types of forms in bootstrap.

  • Vertical Forms
  • Horizontal Forms
  • Inline Forms

Vertical Forms

In these kinds of forms label and text fields are stacked vertically.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/Bootstrap-5-Forms-Classes-Explained-2.png" data-lazy- height="247" src="data:image/svg xml,” width=”719″>

Code

<form class=“row”>


            <div class=“col-md-12”>


              <label class=“form-label”>Email</label>


              <input type=“text” class=“form-control”>


            </div>


            <div class=“col-md-12”>


              <label class=“form-label”>Password</label>


              <input type=“password” class=“form-control”>


            </div>


            <div class=“col-md-12”>


                <br>


                <div class=“form-check”>


                    <input class=“form-check-input” type=“checkbox”>


                    <label class=“form-check-label”>


                      Remember Me


                    </label>


                </div>


            </div>


            <div class=“col-md-2”>


                <br>


                <input type=“button” class=“form-control btn btn-primary” value=“Submit”>


              </div>


        </form>

This is how vertical forms are created.

Horizontal Forms

In these kinds of forms label and text fields are stacked horizontally.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/Bootstrap-5-Forms-Classes-Explained-3.png" data-lazy- height="237" src="data:image/svg xml,” width=”535″>

Code

<form class=“row”>


            <div class=“row”>


                <div class=“col-sm-1”>


                    <label class=“form-label”>Email</label>


                  </div>


                  <div class=“col-sm-4”>


                    <input type=“text” class=“form-control”>


                  </div>


            </div>


            <div class=“row mt-3”>


                <div class=“col-sm-1”>


                    <label class=“form-label”>Password</label>


                  </div>


                  <div class=“col-sm-4”>


                    <input type=“password” class=“form-control”>


                  </div>


            </div>


            <div class=“col-md-12”>


                <br>


                <div class=“form-check”>


                    <input class=“form-check-input” type=“checkbox”>


                    <label class=“form-check-label”>


                      Remember Me


                    </label>


                </div>


            </div>


            <div class=“col-md-2”>


                <br>


                <input type=“button” class=“form-control btn btn-primary” value=“Submit”>


              </div>


        </form>

This is how horizontal forms are created.

Inline Forms

In these kinds of forms every form field is stacked horizontally in a row. These forms are useful where space is limited or where you want to make things compact.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/Bootstrap-5-Forms-Classes-Explained-4.png" data-lazy- height="64" src="data:image/svg xml,” width=”717″>

Code

<form>


  <div class=“row”>


    <div class=“col-sm-4”>


      <input type=“text” class=“form-control” placeholder=“Email”>


     </div>


     <div class=“col-sm-3”>


       <input type=“password” class=“form-control” placeholder=“Password”>


      </div>


      <div class=“col-sm-2”>


        <input type=“button” class=“form-control btn btn-primary” value=“Login”>


       </div>


    </div>


</form>

This is how inline forms are created.

Components

Following are the components that are used in a form. These components only work properly inside a tag.

Text-Fields

Text-field is where you type some information like name, password, email, number, address.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/Bootstrap-5-Forms-Classes-Explained-5.png" data-lazy- height="57" src="data:image/svg xml,” width=”373″>

Code

<form>


  <input type=“text” class=“form-control” placeholder=“Email”>


</form>

Text-fields are created by using tag and set its type attribute to “text” with the class .form-control. Use .form-control-lg class for large size text-field, .form-control class for default text-field and .form-control-sm for small text-field. One more thing I want to mention here is that for creating a read only text-field use .form-control-plaintext class with a keyword readonly in an tag.

Radio-Buttons

Radio buttons are the rounded buttons which are used where you have to select from two or three options.

<img alt="" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/Bootstrap-5-Forms-Classes-Explained-6.png" height="90" src="data:image/svg xml,” width=”277″>

Code

<form>


  <div class=“form-check”>


    <input type=“radio” class=“form-check-input” name=“rdo” checked>


    <label class=“form-check-label”>Male</label>


  </div>


  <div class=“form-check”>


    <input type=“radio” class=“form-check-input” name=“rdo”>


    <label class=“form-check-label”>Female</label>


  </div>


</form>

Radio-buttons are created by using tag, then set its type attribute to “radio” with the class .form-check-input and give the same value of name attribute to every radio button. If you want to activate any button by default then use checked keyword or if you want to disable any button then use disabled keyword in input tag.

CheckBox

Checkboxes are small boxes which are used where you have to select from multiple options.

<img alt="" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/Bootstrap-5-Forms-Classes-Explained-7.png" height="99" src="data:image/svg xml,” width=”196″>

Code

<form>


  <div class=“form-check”>


    <input type=“checkbox” class=“form-check-input” name=“rdo” checked>


    <label class=“form-check-label”>Male</label>


  </div>


  <div class=“form-check”>


    <input type=“checkbox” class=“form-check-input” name=“rdo”>


    <label class=“form-check-label”>Female</label>


  </div>


</form>

Checkboxes are created by using tag, then set its type attribute to “checkbox” with the class .form-check-input. If you want to activate any button by default then use checked keyword or if you want to disable any button then use disabled keyword in input tag.

Textarea

Textarea is where you type descriptive information like message, feedback.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/Bootstrap-5-Forms-Classes-Explained-8.png" data-lazy- height="194" src="data:image/svg xml,” width=”425″>

Code

Textarea is created by using the