📖 Scenario: You are creating a simple contact form for a website using Bootstrap. The form will collect a user's name and email address.
🎯 Goal: Build a basic Bootstrap form with input fields for Name and Email, each with proper labels and Bootstrap styling.
📋 What You'll Learn
Use Bootstrap 5 classes for styling the form controls
Include <label> elements linked to their respective inputs
Use form-control class on input fields
Make sure the form is accessible with proper for and id attributes
Use semantic HTML5 elements
💡 Why This Matters
🌍 Real World
Forms are everywhere on websites for collecting user information like contact details, sign-ups, and feedback.
💼 Career
Knowing how to build accessible, styled forms with Bootstrap is a key skill for front-end web developers.
Progress0 / 4 steps
1
Create the basic HTML form structure
Create a <form> element with two input fields: one with id="nameInput" and one with id="emailInput". Do not add labels or classes yet.
Bootsrap
Hint
Start with a <form> tag and add two <input> elements with the exact id attributes.
2
Add labels for the input fields
Add a <label> for the name input with for="nameInput" and text "Name". Add another <label> for the email input with for="emailInput" and text "Email".
Bootsrap
Hint
Use <label> tags with the for attribute matching the input id.
3
Add Bootstrap form-control classes
Add the Bootstrap class form-control to both input fields with id="nameInput" and id="emailInput".
Bootsrap
Hint
Add class="form-control" inside each <input> tag.
4
Wrap inputs in Bootstrap mb-3 divs and add accessibility
Wrap each label and input pair inside a <div> with class mb-3 for spacing. Add aria-label attributes to inputs matching their labels: "Name" for nameInput and "Email" for emailInput.
Bootsrap
Hint
Use <div class="mb-3"> to group each label and input. Add aria-label attributes to inputs for accessibility.
Practice
(1/5)
1. What is the main purpose of the form-control class in Bootstrap?
easy
A. To format tables
B. To create navigation bars
C. To add animations to buttons
D. To style input elements like text boxes consistently
Solution
Step 1: Understand the role of form-control
The form-control class is used to style form inputs such as text boxes, textareas, and selects.
Step 2: Identify what it does visually
It makes form inputs look consistent and user-friendly across browsers.
Final Answer:
To style input elements like text boxes consistently -> Option D
A. A styled email input box with placeholder text and accessible label
B. A plain input box without any styling or placeholder
C. A button labeled 'Enter email'
D. An error because aria-label is invalid here
Solution
Step 1: Analyze the input element
The input is of type email, has the Bootstrap class form-control, a placeholder, and an ARIA label.
Step 2: Understand the visual and accessibility effects
The form-control class styles the input nicely. The placeholder shows faint text inside the box. The aria-label helps screen readers identify the input as 'Email address'.
Final Answer:
A styled email input box with placeholder text and accessible label -> Option A
5. You want to create a dropdown menu for selecting a country using Bootstrap. Which code snippet correctly applies Bootstrap form control classes and accessibility best practices?
hard
A. <select class="form-select" aria-label="Country selector"><option>USA</option><option>Canada</option></select>
B. <select class="form-control" aria-labelledby="country"><option>USA</option><option>Canada</option></select>
C. <select class="form-select" aria-label="Country selector"><option selected>Select country</option><option>USA</option><option>Canada</option></select>
D. <select class="form-control" aria-label="country"><option>USA</option><option>Canada</option></select>
Solution
Step 1: Identify correct Bootstrap class for dropdowns
Bootstrap uses form-select for styling select dropdowns, not form-control.
Step 2: Check accessibility and usability
Using aria-label="Country selector" provides an accessible name. Including a default option like Select country with selected guides users.
Step 3: Compare options
<select class="form-select" aria-label="Country selector"><option selected>Select country</option><option>USA</option><option>Canada</option></select> uses form-select, has an accessible label, and a default selected option, making it best practice.
Final Answer:
<select class="form-select" aria-label="Country selector"><option selected>Select country</option><option>USA</option><option>Canada</option></select> -> Option C