BS4 Forms
Bootstrap 4's Default Settings
With Bootstrap, form controls immediately get some global styling:
The width of every text-based <input>, <textarea>, and <select> element with class .form-control is 100%.
Bootstrap 4 Form Layouts
- Stacked (full-width) form
- Inline form
Bootstrap 4 Stacked Form
A stacked form with two input fields, a checkbox, and a submit button is created using the example that follows.
To guarantee appropriate margins, surround each form control with a wrapper element that ends in .form-group:
Example
Bootstrap Inline Form
- To the <form> element, add class .form-inline.
Example
Inline Form with Utilities
With the spacing tools provided by Bootstrap, the above inline form will appear much nicer and feel less “compressed”. The ensuing illustration augments every input on all devices (small and up) with a right margin (.mr-sm-2). Additionally, the input field is styled using a margin bottom class (.mb-2) when it breaks (converts from horizontal to vertical owing to insufficient space or width):
Example
Form Row/Grid
Instead of utilizing spacing tools, you may instead manage the width and alignment of form inputs by using columns (.col). Don’t forget to include them in a.row container, though.
We’ll utilize two columns that will show up side by side in the example below. In the Bootstrap Grids Chapter, there will be a lot more information on rows and columns.
==== Example MUKAVU ====
Example
Use .form-row instead of .row if you wish to reduce grid margins (override default column gutters):
==== Example MUKAVU ====
Example
Form Validation
==== Example MUKAVU ====
You may give users insightful feedback by using various validation classes. If you would like to offer validation feedback either before or after the form is submitted, add .was-validated or .needs-validation to the <form> element. To show what’s missing from the form, the input fields will have a border that is either red (invalid) or green (valid). A .valid-feedback or .invalid-feedback message can also be added to inform the user of any missing information or actions that must be completed before submitting the form.
Example
In this instance, the .was-validated suffix is used to identify any missing fields prior to form submission:
Example
In this case, the validation effect is added after the form has been submitted (if there is anything lacking), thanks to the use of .needs-validation. Note that in order for this example to function properly, you will also need to include some jQuery code: