Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Forms | Basic Concepts
Bootstrap: Building Stylish Websites

FormsForms

Bootstrap makes creating appealing and adaptable forms easy by offering a collection of form control classes and components. These form control classes can be used to style input fields, checkboxes, radio buttons, select, dropdowns, and textareas consistently throughout the website.

Common Form Control Classes

form-control class

This class is used to style various form elements, including input fields, textareas, and select dropdowns. When applied, it ensures that form elements are responsive and visually consistent across different devices and screen sizes.

Key features of the form-control class

  • Responsive Design: Form elements styled with form-control automatically adjust their width to fill the available space within their container;
  • Uniform Styling: By applying form-control, form elements receive consistent styling, including borders, padding, and font properties;
  • Focus and Hover States: form-control provides visual feedback to users by changing the appearance of form elements when they are hovered over or focused on.
html

index.html

css

index.css

js

index.js

form-check classes

  • form-check: This class styles checkbox and radio button inputs to display them inline and apply consistent styling;
  • form-check-input: Applied to checkbox and radio button input elements within a form-check container. This class ensures consistent styling of the input elements;
  • form-check-label: Used to style the label associated with checkbox and radio button inputs. It ensures consistent styling of the labels and helps maintain alignment with the corresponding input elements.
html

index.html

css

index.css

js

index.js

Grid System for Form Layout

Bootstrap's grid system provides a powerful tool for organizing form elements into multi-column layouts. By leveraging grid classes such as col-md-6, col-lg-4, and others, developers can arrange form elements into responsive, flexible layouts that adapt seamlessly to various screen sizes and devices.

Key features:

  • Responsive Columns: Grid classes allow developers to define the width of form elements within different breakpoints. For example, col-md-6 specifies that the column should occupy half the width of its container on medium-sized screens and larger. Similarly, col-lg-4 specifies a column width of one-third on large screens;
  • Flexibility: We can mix and match grid classes to create complex form layouts;
  • Optimized for Mobile: Bootstrap's grid system is inherently mobile-first, ensuring that form layouts look great on smaller screens by default.
html

index.html

css

index.css

js

index.js

Result of the responsive form grid's behavior

Form Validation Styles

Form validation styles provide visual feedback to users about the validity of their input. These styles include indicators for success, error, and warning states, making it easy for users to understand whether their input meets the required criteria.

html

index.html

css

index.css

js

index.js

In the provided example:

  • The needs-validation class is added to enable Bootstrap's validation styles;
  • The novalidate attribute is used to disable browser-native form validation;
  • The input field for the email address has the required attribute, making it mandatory;
  • The valid-feedback and invalid-feedback classes are used to provide feedback messages for valid and invalid inputs, respectively;
  • Additionally, JavaScript is used to prevent form submission if there are invalid fields and to apply the was-validated class to the form.

Everything was clear?

Section 3. Chapter 7
course content

Course Content

Bootstrap: Building Stylish Websites

FormsForms

Bootstrap makes creating appealing and adaptable forms easy by offering a collection of form control classes and components. These form control classes can be used to style input fields, checkboxes, radio buttons, select, dropdowns, and textareas consistently throughout the website.

Common Form Control Classes

form-control class

This class is used to style various form elements, including input fields, textareas, and select dropdowns. When applied, it ensures that form elements are responsive and visually consistent across different devices and screen sizes.

Key features of the form-control class

  • Responsive Design: Form elements styled with form-control automatically adjust their width to fill the available space within their container;
  • Uniform Styling: By applying form-control, form elements receive consistent styling, including borders, padding, and font properties;
  • Focus and Hover States: form-control provides visual feedback to users by changing the appearance of form elements when they are hovered over or focused on.
html

index.html

css

index.css

js

index.js

form-check classes

  • form-check: This class styles checkbox and radio button inputs to display them inline and apply consistent styling;
  • form-check-input: Applied to checkbox and radio button input elements within a form-check container. This class ensures consistent styling of the input elements;
  • form-check-label: Used to style the label associated with checkbox and radio button inputs. It ensures consistent styling of the labels and helps maintain alignment with the corresponding input elements.
html

index.html

css

index.css

js

index.js

Grid System for Form Layout

Bootstrap's grid system provides a powerful tool for organizing form elements into multi-column layouts. By leveraging grid classes such as col-md-6, col-lg-4, and others, developers can arrange form elements into responsive, flexible layouts that adapt seamlessly to various screen sizes and devices.

Key features:

  • Responsive Columns: Grid classes allow developers to define the width of form elements within different breakpoints. For example, col-md-6 specifies that the column should occupy half the width of its container on medium-sized screens and larger. Similarly, col-lg-4 specifies a column width of one-third on large screens;
  • Flexibility: We can mix and match grid classes to create complex form layouts;
  • Optimized for Mobile: Bootstrap's grid system is inherently mobile-first, ensuring that form layouts look great on smaller screens by default.
html

index.html

css

index.css

js

index.js

Result of the responsive form grid's behavior

Form Validation Styles

Form validation styles provide visual feedback to users about the validity of their input. These styles include indicators for success, error, and warning states, making it easy for users to understand whether their input meets the required criteria.

html

index.html

css

index.css

js

index.js

In the provided example:

  • The needs-validation class is added to enable Bootstrap's validation styles;
  • The novalidate attribute is used to disable browser-native form validation;
  • The input field for the email address has the required attribute, making it mandatory;
  • The valid-feedback and invalid-feedback classes are used to provide feedback messages for valid and invalid inputs, respectively;
  • Additionally, JavaScript is used to prevent form submission if there are invalid fields and to apply the was-validated class to the form.

Everything was clear?

Section 3. Chapter 7
some-alt