Mastering Bootstrap: The Ultimate Guide to Building Responsive Websites Faster

9.98K 0 0 0 0

Chapter 5: Bootstrap Forms & Inputs

🧠 Why This Chapter Matters

Forms are essential for capturing user input — from contact forms to sign-up pages and payment checkouts. Bootstrap provides stylized, responsive, and accessible form controls right out of the box.

In this chapter, you'll learn how to:

  • Build structured forms with Bootstrap classes
  • Style inputs, checkboxes, radios, and selects
  • Add validation and feedback
  • Use layout utilities for horizontal and grid-based forms

1. Basic Form Structure

All form elements start with the <form> tag and use Bootstrap’s form classes:

<form>

  <div class="mb-3">

    <label for="exampleEmail" class="form-label">Email address</label>

    <input type="email" class="form-control" id="exampleEmail" placeholder="name@example.com">

  </div>

  <button type="submit" class="btn btn-primary">Submit</button>

</form>

Class

Description

form-label

Styles the <label> tag

form-control

Applies styles to <input> and <textarea>

mb-3

Margin-bottom spacing for inputs

Always wrap inputs inside div.mb-3 or use grid for layout.


2. Text Inputs, Textareas, and Placeholders

<input type="text" class="form-control" placeholder="Your name">

<textarea class="form-control" rows="3" placeholder="Your message"></textarea>

The placeholder attribute shows a gray hint inside the input.


3. Form Layouts

🔹 Inline Form

<form class="d-flex">

  <input class="form-control me-2" type="search" placeholder="Search">

  <button class="btn btn-outline-success" type="submit">Search</button>

</form>

🔹 Horizontal Form with Grid

<form>

  <div class="row mb-3">

    <label for="email" class="col-sm-2 col-form-label">Email</label>

    <div class="col-sm-10">

      <input type="email" class="form-control" id="email">

    </div>

  </div>

</form>

Use .row, .col-sm-*, and .col-form-label for structured layouts.


4. Checkboxes, Radios, and Switches

🔹 Checkbox

<div class="form-check">

  <input class="form-check-input" type="checkbox" id="subscribe">

  <label class="form-check-label" for="subscribe">Subscribe to newsletter</label>

</div>

🔹 Radio

<div class="form-check">

  <input class="form-check-input" type="radio" name="plan" id="basic">

  <label class="form-check-label" for="basic">Basic Plan</label>

</div>

🔹 Switch

<div class="form-check form-switch">

  <input class="form-check-input" type="checkbox" id="toggleFeature">

  <label class="form-check-label" for="toggleFeature">Enable Feature</label>

</div>


5. Select Menus

<select class="form-select">

  <option selected>Select your country</option>

  <option value="1">USA</option>

  <option value="2">UK</option>

</select>

Class

Description

form-select

Styles <select> elements

Combine with size, multiple, or disabled attributes.


6. Input Groups (With Icons or Buttons)

<div class="input-group mb-3">

  <span class="input-group-text">@</span>

  <input type="text" class="form-control" placeholder="Username">

</div>

You can place buttons, labels, or icons before or after the input.


7. Form Validation (Client-Side)

🔹 Validating with Bootstrap

<form class="needs-validation" novalidate>

  <div class="mb-3">

    <input type="text" class="form-control" required>

    <div class="invalid-feedback">Field is required</div>

  </div>

</form>

🔹 JS to Trigger Validation

(() => {

  'use strict';

  const forms = document.querySelectorAll('.needs-validation');

  Array.from(forms).forEach(form => {

    form.addEventListener('submit', e => {

      if (!form.checkValidity()) {

        e.preventDefault();

        e.stopPropagation();

      }

      form.classList.add('was-validated');

    });

  });

})();

Adds red borders and shows validation messages.


8. Disabled & Readonly Inputs

<input type="text" class="form-control" disabled value="Disabled input">

<input type="text" class="form-control" readonly value="Read-only input">


9. Floating Labels

Floating labels rise as you type into the input.

<div class="form-floating mb-3">

  <input type="email" class="form-control" id="floatingEmail" placeholder="name@example.com">

  <label for="floatingEmail">Email address</label>

</div>

Improves UI clarity and space usage.


10. Recap Table: Form Classes


Component

Class

Notes

Input

form-control

Applies to text, email, etc.

Label

form-label

Connects to input by for

Checkbox/Radio

form-check + form-check-input

Wraps for correct layout

Select

form-select

Stylized dropdown

Input Group

input-group, input-group-text

Icons/buttons with input

Validation

needs-validation, was-validated

JS-based checks

Back

FAQs


1. What is Bootstrap and what is it used for?

Bootstrap is a free front-end framework used to create responsive, mobile-first websites using HTML, CSS, and JavaScript components.

2. Is Bootstrap still relevant in 2025?

Yes Bootstrap is actively maintained and widely used in both small and enterprise-level projects.

3. What is the difference between Bootstrap 4 and 5?

BootstraDo I need to know JavaScript to use Bootstrap?p 5 dropped jQuery, introduced new utilities, improved grid systems, and enhanced customizability with CSS variables.

4. Do I need to know JavaScript to use Bootstrap?

No you can use most components with just HTML and CSS. But knowing JavaScript enhances your control over dynamic components.

5. How do I include Bootstrap in my project?

You can include it via a CDN, download it locally, or install it using npm or yarn.

6. Can I customize Bootstrap styles?

Yes use Sass variables or override styles with your own custom CSS to match your branding.

7. Is Bootstrap better than Tailwind CSS?

They serve different purposes. Bootstrap offers pre-built components and design systems, while Tailwind is utility-first and highly customizable.

8. Does Bootstrap work with React or Vue?

Yes use Bootstrap directly or with wrappers like React-Bootstrap or BootstrapVue.

9. How does the Bootstrap grid system work?

Bootstrap uses a 12-column grid system with breakpoints for different screen sizes, allowing for responsive layouts.