Lesson 22 of 60 – Bootstrap Forms
37%

Bootstrap Forms

Bootstrap provides ready-made classes for creating clean, responsive, and user-friendly HTML forms. Forms are commonly used for login pages, registration pages, admission forms, contact forms, feedback forms, search forms, and payment forms.

Note: Bootstrap does not replace HTML forms. It provides CSS classes that make standard HTML form elements easier to style and organize.

1. What Is a Bootstrap Form?

A Bootstrap form is a normal HTML form styled using Bootstrap classes.

<form>

    <label>Name</label>

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

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

</form>

Bootstrap form classes help create consistent spacing, sizing, and responsive layouts.

2. Basic Form Structure

A basic Bootstrap form usually contains a label, a form control, and an action button.

<form>

    <div class="mb-3">

        <label class="form-label">
        Name
        </label>

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

    </div>

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

</form>

3. The .form-label Class

The .form-label class styles labels consistently with Bootstrap forms.

<label class="form-label">
Email Address
</label>

Using a proper label also improves form accessibility and usability.

4. The .form-control Class

The .form-control class is used with many text-based form controls such as text, email, password, number, and date inputs.

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

5. Form Spacing with .mb-3

Bootstrap spacing utilities such as .mb-3 can be used to create space between form fields.

<div class="mb-3">

    <label class="form-label">
    Name
    </label>

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

</div>

6. Text Input

A text input is used to collect short text information.

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

7. Email Input

Use type="email" when the form expects an email address.

<input type="email"
class="form-control"
placeholder="Enter email">

The email input type also gives browsers useful information about the expected data.

8. Password Input

Use type="password" for password fields.

<input type="password"
class="form-control"
placeholder="Enter password">

The browser hides the characters typed into a password field.

9. Number Input

The number input is useful when users need to enter numeric values.

<input type="number"
class="form-control"
placeholder="Enter age">

10. Date Input

The date input allows users to select a date.

<input type="date"
class="form-control">

11. Textarea

The <textarea> element is used for longer text such as messages, comments, addresses, and descriptions.

<textarea
class="form-control"
rows="4"
placeholder="Enter message">
</textarea>

12. Select Dropdown

The .form-select class is used to style a select dropdown.

<select class="form-select">

    <option>
    Select Course
    </option>

    <option>
    HTML
    </option>

    <option>
    CSS
    </option>

</select>

13. Checkbox

Checkboxes allow users to select one or more options.

<div class="form-check">

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

    <label
    class="form-check-label"
    for="html">

    HTML

    </label>

</div>

14. Radio Buttons

Radio buttons allow users to select one option from a group.

<div class="form-check">

    <input
    class="form-check-input"
    type="radio"
    name="gender"
    id="male">

    <label
    class="form-check-label"
    for="male">

    Male

    </label>

</div>

15. Form Check

The .form-check class provides a wrapper for checkboxes and radio buttons.

<div class="form-check">

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

    <label class="form-check-label"
    for="terms">

    I agree to the terms.

    </label>

</div>

16. File Input

The file input allows users to select a file from their device.

<label class="form-label">
Upload Photo
</label>

<input type="file"
class="form-control">

For server-side file uploading, the form also needs appropriate HTML attributes and server-side handling.

17. Form Validation

HTML validation attributes such as required can be combined with Bootstrap form styling.

<input type="email"
class="form-control"
required>
Tip: Bootstrap also provides validation-related classes and styles for showing valid and invalid states.

18. Disabled Form Controls

The disabled attribute prevents the user from interacting with a form control.

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

19. Readonly Controls

The readonly attribute prevents editing while allowing the value to remain available in the form.

<input type="text"
class="form-control"
value="Student ID: 101"
readonly>

20. Input Size

Bootstrap provides .form-control-lg and .form-control-sm for larger and smaller text inputs.

<input class="form-control form-control-lg"
placeholder="Large input">

<input class="form-control"
placeholder="Normal input">

<input class="form-control form-control-sm"
placeholder="Small input">

21. Form Help Text

The .form-text class can be used to provide additional information about a form field.

<label class="form-label">
Password
</label>

<input type="password"
class="form-control">

<div class="form-text">
Your password must be at least 8 characters.
</div>
Your password should be kept private.

22. Input Groups

Input groups allow you to place text, symbols, or buttons beside a form control.

<div class="input-group">

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

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

</div>
@

23. Horizontal Forms

Bootstrap grid classes can be used to place labels and controls in different columns.

<div class="row mb-3">

    <label class="col-sm-3 col-form-label">
    Name
    </label>

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

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

    </div>

</div>

24. Inline Forms

Bootstrap grid and display utilities can be used to create forms where multiple controls appear in the same row.

<div class="row g-3 align-items-end">

    <div class="col-md-4">

        <label class="form-label">
        Name
        </label>

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

    </div>

    <div class="col-md-4">

        <label class="form-label">
        Course
        </label>

        <select class="form-select">
        ...
        </select>

    </div>

</div>

25. Form Buttons

Bootstrap button classes can be used for form actions such as Submit, Reset, Save, Cancel, and Login.

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

<button type="reset"
class="btn btn-secondary">
Reset
</button>

26. Login Form Example

A login form can combine labels, email or text inputs, password fields, checkboxes, and buttons.

<form>

    <div class="mb-3">

        <label class="form-label">
        Email
        </label>

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

    </div>

    <div class="mb-3">

        <label class="form-label">
        Password
        </label>

        <input type="password"
        class="form-control">

    </div>

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

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

        <label class="form-check-label">
        Remember me
        </label>

    </div>

    <button
    class="btn btn-primary">
    Login
    </button>

</form>

27. Registration Form Example

A registration form may contain multiple fields such as name, email, phone, password, course, and terms and conditions.

28. Form Validation States

Bootstrap provides classes such as .is-valid and .is-invalid for displaying validation states.

<input type="text"
class="form-control is-valid"
value="Rahul">

<input type="text"
class="form-control is-invalid"
value="">
Looks good!
Please provide a value.

29. Practical Admission Form

The following example demonstrates a simple student admission form using Bootstrap grid and form classes.

30. Complete Bootstrap Form Example

This complete example combines labels, text inputs, email, password, select, textarea, checkbox, responsive grid, and Bootstrap buttons.

<form>

    <div class="row">

        <div class="col-md-6 mb-3">

            <label class="form-label">
            Full Name
            </label>

            <input type="text"
            class="form-control"
            placeholder="Enter full name"
            required>

        </div>


        <div class="col-md-6 mb-3">

            <label class="form-label">
            Email
            </label>

            <input type="email"
            class="form-control"
            placeholder="Enter email"
            required>

        </div>


        <div class="col-md-6 mb-3">

            <label class="form-label">
            Password
            </label>

            <input type="password"
            class="form-control"
            placeholder="Enter password"
            required>

        </div>


        <div class="col-md-6 mb-3">

            <label class="form-label">
            Course
            </label>

            <select class="form-select">

                <option selected>
                Select Course
                </option>

                <option>
                HTML
                </option>

                <option>
                CSS
                </option>

                <option>
                Python
                </option>

            </select>

        </div>


        <div class="col-12 mb-3">

            <label class="form-label">
            Message
            </label>

            <textarea
            class="form-control"
            rows="4"
            placeholder="Enter message">
            </textarea>

        </div>


        <div class="col-12 mb-3">

            <div class="form-check">

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

                <label
                class="form-check-label"
                for="terms">

                I agree to the terms.

                </label>

            </div>

        </div>


        <div class="col-12">

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

            Submit

            </button>

            <button
            type="reset"
            class="btn btn-secondary">

            Reset

            </button>

        </div>

    </div>

</form>

📌 Key Points

  • Bootstrap forms are based on standard HTML forms.
  • Use .form-label for labels.
  • Use .form-control for many text-based inputs.
  • Use .form-select for select dropdowns.
  • Use .form-check for checkboxes and radio buttons.
  • Use .form-text for additional help text.
  • Use .input-group to combine inputs with text or buttons.
  • Use Bootstrap grid classes to create responsive forms.
  • Use .form-control-lg and .form-control-sm for input sizing.
  • Use .is-valid and .is-invalid for validation states.
  • Use HTML attributes such as required, disabled, and readonly when appropriate.
  • Always connect labels with their controls using the for and id attributes.
  • Keep forms simple and easy to understand.
  • Always consider responsive design for smaller screens.

🧠 Quick Quiz

Question: Which Bootstrap class is commonly used to style text input fields?