-->

Bootstrap - Forms

Forms are an essential part of web development, enabling user interaction and data collection. Bootstrap 5 provides a powerful and flexible set of tools to create modern, responsive, and accessible forms. Whether you're building a simple contact form, a complex user registration form, or handling user inputs with validation, Bootstrap 5 has the utilities and components you need.

In this guide, we’ll dive into Bootstrap 5 Forms, explaining how to use its various form components, layout options, and form validation techniques.

1. Basic Form Structure

A basic form in Bootstrap 5 includes the use of form groups, labels, and input elements. Bootstrap 5 introduces some new features, like custom form controls, grid support, and responsive layouts, which make form creation smoother and more adaptable.

Basic Form Example:

<form>

  <div class="mb-3">

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

    <input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp">

    <div id="emailHelp" class="form-text">We'll never share your email with anyone else.</div>

  </div>  

  <div class="mb-3">

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

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

  </div>

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

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

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

  </div>

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

</form>

Explanation:

<form>: Defines the form container.

<div class="mb-3">: The mb-3 class adds a margin-bottom to each form element, ensuring proper spacing.

<label>: Labels provide a description for each input field.

<input>: Defines the form control (e.g., email, password).

<button>: Defines the form submission button.

2. Form Layouts and Grid System

Bootstrap 5 allows you to easily create responsive form layouts using its grid system. By combining the grid classes with form elements, you can create complex layouts that adjust according to screen size.

Example: Responsive Form Layout

<form>

  <div class="row mb-3">

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

      <label for="firstName" class="form-label">First Name</label>

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

    </div>

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

      <label for="lastName" class="form-label">Last Name</label>

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

    </div>

  </div>

  <div class="mb-3">

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

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

  </div>

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

</form>

Explanation:

<div class="row mb-3">: Defines a row with bottom margin.

<div class="col-md-6">: Defines the columns that adjust to half width on medium screens (≥768px).

By using the Bootstrap grid classes like col-sm, col-md, and col-lg, you can control how your form adapts across different screen sizes.

3. Form Validation

Bootstrap 5 comes with built-in form validation classes that allow you to easily create custom or inline validation for user inputs. These classes can help ensure that the user provides valid input before submitting the form.

Example: Inline Form Validation

<form>

  <div class="mb-3">

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

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

    <div class="invalid-feedback">Please provide a valid email address.</div>

  </div>

  <div class="mb-3">

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

    <input type="password" class="form-control is-valid" id="password" required>

    <div class="valid-feedback">Looks good!</div>

  </div>

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

</form>

Explanation:

is-invalid and is-valid: These classes apply custom validation styles to the input fields.

invalid-feedback and valid-feedback: These elements display feedback based on the validity of the field.

You can combine these classes with JavaScript to dynamically update the validation feedback as the user interacts with the form.

4. Custom Form Controls

Bootstrap 5 also allows you to create custom form controls, like checkboxes, radio buttons, and switches, to match the design of your website. These controls are enhanced with custom styles and functionality.

Example: Custom Checkboxes and Radios

<div class="mb-3">

  <label for="customCheck1" class="form-label">Checkbox</label>

  <div class="form-check">

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

    <label class="form-check-label" for="customCheck1">Check this custom checkbox</label>

  </div>

</div>

<div class="mb-3">

  <label for="customRadio1" class="form-label">Radio</label>

  <div class="form-check">

    <input class="form-check-input" type="radio" name="radioOptions" id="customRadio1" checked>

    <label class="form-check-label" for="customRadio1">Option 1</label>

  </div>

  <div class="form-check">

    <input class="form-check-input" type="radio" name="radioOptions" id="customRadio2">

    <label class="form-check-label" for="customRadio2">Option 2</label>

  </div>

</div>

Explanation:

form-check: This class is used for styling checkboxes and radio buttons in Bootstrap 5.

form-check-input: Applied to the input to style checkboxes and radios.

form-check-label: Applied to the label to style the associated text.

5. File Input

You can also use Bootstrap 5's custom file input to create more attractive and functional file upload forms.

Example: Custom File Input

<div class="mb-3">

  <label for="formFile" class="form-label">Upload file</label>

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

</div>

Explanation:

form-control: This class is used to style the file input, making it look like a regular form control.

6. Input Groups

Bootstrap 5 introduces input groups that allow you to prepend or append text, icons, or buttons to form inputs. This is useful for creating search bars, pricing inputs, and more.

Example: Input Group with Icon

<div class="mb-3">

  <label for="inputGroup" class="form-label">Username</label>

  <div class="input-group">

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

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

  </div>

</div>

Explanation:

input-group: This class is used to create the input group.

input-group-text: This is used for adding text or icons before or after the input field.

Conclusion

Bootstrap 5 offers a wide range of features to make form creation fast, responsive, and accessible. With its grid system, built-in form validation, custom controls, and input groups, you can create modern, user-friendly forms for any type of application. By understanding and utilizing these form components and utilities, you can enhance both the user experience and the performance of your website.