Bootstrap Forms
Bootstrap provides ready-to-use classes for creating clean, responsive and user-friendly forms.
What is a Form?
A form is an HTML section used to collect information from a user.
Forms can be used to collect:
- Name
- Email Address
- Phone Number
- Password
- Course Selection
- Feedback
Basic Bootstrap Form
Bootstrap provides the .form-label and .form-control classes for styling labels and input fields.
Example
<form>
<div class="mb-3">
<label class="form-label">
Name
</label>
<input type="text"
class="form-control">
</div>
<div class="mb-3">
<label class="form-label">
Email
</label>
<input type="email"
class="form-control">
</div>
<button type="submit"
class="btn btn-primary">
Submit
</button>
</form>
Output
Text Input
The .form-control class is used to style text-based input controls.
Example
<label class="form-label">
Full Name
</label>
<input type="text"
class="form-control"
placeholder="Enter your full name">
Output
Email Input
Use type="email" when collecting an email address.
Example
<label class="form-label">
Email Address
</label>
<input type="email"
class="form-control"
placeholder="name@example.com">
Output
Password Input
Password fields hide the characters entered by the user.
Example
<label class="form-label">
Password
</label>
<input type="password"
class="form-control"
placeholder="Enter password">
Output
Textarea
A textarea is used when the user needs to enter multiple lines of text.
Bootstrap uses the same .form-control class for styling a textarea.
Example
<label class="form-label">
Message
</label>
<textarea class="form-control"
rows="4"></textarea>
Output
Select / Dropdown
The .form-select class is used to style dropdown controls.
Example
<label class="form-label">
Select Course
</label>
<select class="form-select">
<option>
Select Course
</option>
<option>
.NET Full Stack
</option>
<option>
Java Full Stack
</option>
<option>
Python Full Stack
</option>
</select>
Output
Checkbox
Checkboxes allow users to select one or more options.
Example
<div class="form-check">
<input class="form-check-input"
type="checkbox">
<label class="form-check-label">
I agree to the terms and conditions.
</label>
</div>
Output
Radio Buttons
Radio buttons are used when the user should select only one option from a group.
Example
<div class="form-check">
<input class="form-check-input"
type="radio"
name="course">
<label class="form-check-label">
Online
</label>
</div>
<div class="form-check">
<input class="form-check-input"
type="radio"
name="course">
<label class="form-check-label">
Offline
</label>
</div>
Output
File Input
File inputs allow users to select files from their device.
Example
<label class="form-label">
Upload Resume
</label>
<input type="file"
class="form-control">
Output
Form Control Sizing
Bootstrap provides classes to control the size of form controls.
- .form-control-lg - Large input
- .form-control - Normal input
- .form-control-sm - Small input
Example
<input class="form-control form-control-lg"
type="text"
placeholder="Large input">
<input class="form-control"
type="text"
placeholder="Normal input">
<input class="form-control form-control-sm"
type="text"
placeholder="Small input">
Output
Disabled Form Control
The disabled attribute prevents the user from entering or changing a value.
Example
<input type="text"
class="form-control"
value="CIIT Training Institute"
disabled>
Output
Readonly Form Control
The readonly attribute allows the user to see the value but prevents editing it.
Example
<input type="text"
class="form-control"
value="CIIT"
readonly>
Output
Complete Registration Form
Let's combine different Bootstrap form controls to create a simple student registration form.
Example
<form>
<div class="mb-3">
<label class="form-label">
Full Name
</label>
<input type="text"
class="form-control">
</div>
<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">
Select Course
</label>
<select class="form-select">
<option>
Select Course
</option>
<option>
.NET Full Stack
</option>
<option>
Java Full Stack
</option>
</select>
</div>
<button type="submit"
class="btn btn-primary">
Register
</button>
</form>
Output
CIIT Training Institute Example
A training institute can use Bootstrap Forms to collect student enquiry details.
Important Bootstrap Form Classes
| Class / Attribute | Purpose |
|---|---|
.form-label |
Styles form labels |
.form-control |
Styles text-based form controls |
.form-control-lg |
Creates a large form control |
.form-control-sm |
Creates a small form control |
.form-select |
Styles select/dropdown controls |
.form-check |
Creates checkbox/radio wrapper |
.form-check-input |
Styles checkbox/radio input |
.form-check-label |
Styles checkbox/radio label |
disabled |
Disables a form control |
readonly |
Makes a control read-only |
Common Mistakes
- Forgetting to use .form-control on text inputs.
- Using .form-control instead of .form-select for dropdowns.
- Forgetting to associate labels with their inputs.
- Using radio buttons without the same name attribute when only one option should be selected.
- Creating forms without meaningful labels and placeholders.
- Using only CSS for validation instead of also validating the submitted data on the server.
Summary
- Bootstrap provides ready-made classes for creating responsive forms.
- .form-label is used for form labels.
- .form-control styles text-based inputs and textareas.
- .form-select styles dropdown controls.
- .form-check is used with checkboxes and radio buttons.
- .form-control-lg and .form-control-sm control input size.
- Bootstrap Forms are useful for login, registration, enquiry, contact and application pages.