Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Take A QuizChallenge yourself and boost your learning! Start the quiz now to earn credits.
Take A QuizUnlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
Take A Quiz
🧠 Why This Chapter
Matters
Forms are essential for capturing user input — from contact
forms to sign-up pages and payment checkouts. Bootstrap provides stylized,
responsive, and accessible form controls right out of the box.
In this chapter, you'll learn how to:
✅ 1. Basic Form Structure
All form elements start with the <form> tag and use
Bootstrap’s form classes:
<form>
<div class="mb-3">
<label for="exampleEmail"
class="form-label">Email address</label>
<input type="email" class="form-control"
id="exampleEmail" placeholder="name@example.com">
</div>
<button type="submit" class="btn
btn-primary">Submit</button>
</form>
Class |
Description |
form-label |
Styles the <label> tag |
form-control |
Applies styles to <input> and <textarea> |
mb-3 |
Margin-bottom spacing for inputs |
✅ Always wrap inputs inside
div.mb-3 or use grid for layout.
✅ 2. Text Inputs, Textareas, and
Placeholders
<input
type="text" class="form-control" placeholder="Your
name">
<textarea
class="form-control" rows="3" placeholder="Your
message"></textarea>
✅ The placeholder attribute shows
a gray hint inside the input.
✅ 3. Form Layouts
🔹 Inline Form
<form
class="d-flex">
<input class="form-control me-2"
type="search" placeholder="Search">
<button class="btn
btn-outline-success" type="submit">Search</button>
</form>
🔹 Horizontal Form with
Grid
<form>
<div class="row mb-3">
<label for="email"
class="col-sm-2 col-form-label">Email</label>
<div class="col-sm-10">
<input type="email"
class="form-control" id="email">
</div>
</div>
</form>
✅ Use .row, .col-sm-*, and
.col-form-label for structured layouts.
✅ 4. Checkboxes, Radios, and
Switches
🔹 Checkbox
<div
class="form-check">
<input class="form-check-input"
type="checkbox" id="subscribe">
<label class="form-check-label"
for="subscribe">Subscribe to newsletter</label>
</div>
🔹 Radio
<div
class="form-check">
<input class="form-check-input"
type="radio" name="plan" id="basic">
<label class="form-check-label"
for="basic">Basic Plan</label>
</div>
🔹 Switch
<div
class="form-check form-switch">
<input class="form-check-input"
type="checkbox" id="toggleFeature">
<label class="form-check-label"
for="toggleFeature">Enable Feature</label>
</div>
✅ 5. Select Menus
<select
class="form-select">
<option selected>Select your
country</option>
<option
value="1">USA</option>
<option
value="2">UK</option>
</select>
Class |
Description |
form-select |
Styles <select> elements |
✅ Combine with size, multiple, or
disabled attributes.
✅ 6. Input Groups (With Icons or
Buttons)
<div
class="input-group mb-3">
<span
class="input-group-text">@</span>
<input type="text"
class="form-control" placeholder="Username">
</div>
You can place buttons, labels, or icons before or after the input.
✅ 7. Form Validation
(Client-Side)
🔹 Validating with
Bootstrap
<form
class="needs-validation" novalidate>
<div class="mb-3">
<input type="text"
class="form-control" required>
<div
class="invalid-feedback">Field is required</div>
</div>
</form>
🔹 JS to Trigger
Validation
(()
=> {
'use strict';
const forms =
document.querySelectorAll('.needs-validation');
Array.from(forms).forEach(form => {
form.addEventListener('submit', e => {
if (!form.checkValidity()) {
e.preventDefault();
e.stopPropagation();
}
form.classList.add('was-validated');
});
});
})();
✅ Adds red borders and shows
validation messages.
✅ 8. Disabled & Readonly
Inputs
<input
type="text" class="form-control" disabled
value="Disabled input">
<input
type="text" class="form-control" readonly
value="Read-only input">
✅ 9. Floating Labels
Floating labels rise as you type into the input.
<div
class="form-floating mb-3">
<input type="email"
class="form-control" id="floatingEmail"
placeholder="name@example.com">
<label
for="floatingEmail">Email address</label>
</div>
✅ Improves UI clarity and space
usage.
✅ 10. Recap Table: Form Classes
Component |
Class |
Notes |
Input |
form-control |
Applies to text, email, etc. |
Label |
form-label |
Connects to input by for |
Checkbox/Radio |
form-check + form-check-input |
Wraps for correct layout |
Select |
form-select |
Stylized dropdown |
Input Group |
input-group, input-group-text |
Icons/buttons with input |
Validation |
needs-validation, was-validated |
JS-based checks |
Bootstrap is a free front-end framework used to create responsive, mobile-first websites using HTML, CSS, and JavaScript components.
✅ Yes — Bootstrap is actively maintained and widely used in both small and enterprise-level projects.
BootstraDo I need to know JavaScript to use Bootstrap?p 5 dropped jQuery, introduced new utilities, improved grid systems, and enhanced customizability with CSS variables.
❌ No — you can use most components with just HTML and CSS. But knowing JavaScript enhances your control over dynamic components.
You can include it via a CDN, download it locally, or install it using npm or yarn.
✅ Yes — use Sass variables or override styles with your own custom CSS to match your branding.
They serve different purposes. Bootstrap offers pre-built components and design systems, while Tailwind is utility-first and highly customizable.
✅ Yes — use Bootstrap directly or with wrappers like React-Bootstrap or BootstrapVue.
Bootstrap uses a 12-column grid system with breakpoints for different screen sizes, allowing for responsive layouts.
Please log in to access this content. You will be redirected to the login page shortly.
LoginReady to take your education and career to the next level? Register today and join our growing community of learners and professionals.
Comments(0)