Skip to main content

Archive

Show more

Bootstrap Forms

Bootstrap Forms

Bootstrap offers a wide range of form styles and components to simplify the process of creating elegant and responsive forms in web applications. Whether you need basic input fields, checkboxes, radio buttons, or complex form layouts, Bootstrap provides CSS classes and components to streamline form development.


1. Basic Form Structure

Bootstrap follows a simple and intuitive structure for creating forms:

  • Form Container: Wrap your form elements within a <form> element.
  • Form Group: Group form controls together using the .form-group class.
  • Form Control: Use various input types and form controls such as text inputs, checkboxes, radio buttons, etc.
  • Label: Associate labels with form controls using the <label> element.

Example:

<form>
  <div class="form-group">
    <label for="exampleInputEmail1">Email address</label>
    <input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter email">
    <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
  </div>
  <div class="form-group">
    <label for="exampleInputPassword1">Password</label>
    <input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
  </div>
  <div class="form-check">
    <input type="checkbox" class="form-check-input" id="exampleCheck1">
    <label class="form-check-label" for="exampleCheck1">Check me out</label>
  </div>
  <button type="submit" class="btn btn-primary">Submit</button>
</form>

2. Form Validation

Bootstrap provides built-in validation styles and feedback icons for form validation:

  • Valid Feedback: Use the .is-valid class for valid input fields.
  • Invalid Feedback: Use the .is-invalid class for invalid input fields.

Example:

<div class="form-group">
  <label for="validationServer01">First name</label>
  <input type="text" class="form-control is-valid" id="validationServer01" placeholder="First name" value="Mark" required>
  <div class="valid-feedback">
    Looks good!
  </div>
</div>

3. Supported Input Types

Bootstrap supports various input types, including text, password, email, number, date, and more. You can use these input types to capture different types of user input:

  • Text Input: <input type="text">
  • Password Input: <input type="password">
  • Email Input: <input type="email">
  • Number Input: <input type="number">
  • Date Input: <input type="date">

4. Form Layouts

Bootstrap offers different form layouts, including inline forms and horizontal forms, to suit your design needs:

  • Inline Form: Use the .form-inline class to create inline forms where form controls are horizontally aligned.
  • Horizontal Form: Use the .form-horizontal class to create horizontal forms with labels and controls side-by-side.

Conclusion

Bootstrap simplifies the process of creating stylish and responsive forms by providing a comprehensive set of CSS classes and components. By leveraging Bootstrap's form styles, validation features, and layout options, developers can quickly build user-friendly forms for their web applications.

Comments