📖 Scenario: You are building a website header that includes a brand name and a button to toggle the navigation menu on small screens.
🎯 Goal: Create a responsive Bootstrap navbar with a brand name on the left and a toggler button that shows or hides the navigation menu on smaller screens.
📋 What You'll Learn
Use Bootstrap 5 classes for the navbar
Add a brand name link with text 'MySite'
Include a toggler button with the correct attributes to control the menu
Ensure the navbar is responsive and accessible
💡 Why This Matters
🌍 Real World
Websites often have a header with a brand name and a menu that collapses on small screens for better usability.
💼 Career
Knowing how to build responsive navigation bars is essential for front-end web development jobs.
Progress0 / 4 steps
1
Create the basic navbar structure
Write the HTML code to create a <nav> element with Bootstrap classes navbar and navbar-expand-lg. Inside it, add a <div> with class container-fluid to hold the navbar content.
Bootsrap
Hint
Use the navbar and navbar-expand-lg classes on the <nav> tag and add a div with class container-fluid inside it.
2
Add the navbar brand link
Inside the div with class container-fluid, add an <a> element with class navbar-brand, href set to #, and text content MySite.
Bootsrap
Hint
The brand link uses the class navbar-brand and should link to #.
3
Add the navbar toggler button
Below the brand link, add a <button> element with class navbar-toggler, type button, data-bs-toggle="collapse", data-bs-target="#navbarSupportedContent", aria-controls="navbarSupportedContent", aria-expanded="false", and aria-label="Toggle navigation". Inside the button, add a <span> with class navbar-toggler-icon.
Bootsrap
Hint
The toggler button controls the collapsible menu with the ID navbarSupportedContent. Use the correct Bootstrap attributes for accessibility.
4
Add the collapsible navbar menu container
After the toggler button, add a <div> with classes collapse navbar-collapse and id navbarSupportedContent. This will hold the navigation links (which you can leave empty for now).
Bootsrap
Hint
This container collapses and expands when the toggler button is clicked. It must have the ID navbarSupportedContent.
Practice
(1/5)
1. What is the main purpose of the navbar-brand class in a Bootstrap navbar?
easy
A. To create a button that toggles the menu on small screens
B. To add navigation links inside the navbar
C. To display the site name or logo prominently in the navbar
D. To style the background color of the navbar
Solution
Step 1: Understand the role of navbar-brand
The navbar-brand class is used to show the website's name or logo in the navbar.
Step 2: Differentiate from other navbar elements
The toggler button is for menu control, links are for navigation, and background color is styling, not branding.
Final Answer:
To display the site name or logo prominently in the navbar -> Option C
Quick Check:
navbar-brand = site name/logo [OK]
Hint: Brand means site name or logo in navbar [OK]
Common Mistakes:
Confusing navbar-brand with navbar-toggler
Thinking navbar-brand adds navigation links
Assuming navbar-brand controls styling only
2. Which of the following is the correct HTML structure for a Bootstrap navbar toggler button?
B. Incorrect data attribute names; should use data-bs-toggle and data-bs-target
C. aria-expanded should be true by default
D. navbar-toggler-icon class is invalid
Solution
Step 1: Check Bootstrap 5 data attribute naming
Bootstrap 5 requires data-bs-toggle and data-bs-target instead of data-toggle and data-target.
Step 2: Verify other attributes
The button is properly closed, aria-expanded can be false initially, and navbar-toggler-icon is a valid class.
Final Answer:
Incorrect data attribute names; should use data-bs-toggle and data-bs-target -> Option B
Quick Check:
Bootstrap 5 uses data-bs-* attributes [OK]
Hint: Bootstrap 5 uses data-bs-toggle, not data-toggle [OK]
Common Mistakes:
Using old Bootstrap 4 data attributes
Changing aria-expanded default incorrectly
Misnaming navbar-toggler-icon class
5. You want to create a responsive Bootstrap navbar with a brand logo image and a toggler that controls the menu. Which of the following code snippets correctly implements both the brand and toggler with accessibility in mind?
hard
A.
B.
C.
D.
Solution
Step 1: Check brand logo and alt text
includes an img with descriptive alt="Site Logo", which is good for accessibility. has empty alt, which is less accessible.
Step 2: Verify toggler button attributes
uses correct Bootstrap 5 attributes data-bs-toggle="collapse" and data-bs-target="#navbarMenu" with proper aria attributes. uses old data-toggle and misses some aria attributes. incorrectly uses data-bs-toggle="dropdown" which is wrong for toggling collapse.
Final Answer:
Option A correctly implements brand logo with alt and toggler with proper attributes -> Option A
Quick Check:
Brand with alt + toggler with data-bs-toggle="collapse" [OK]
Hint: Use img alt and data-bs-toggle="collapse" for toggler [OK]
Common Mistakes:
Missing alt text on brand logo image
Using old data-toggle instead of data-bs-toggle
Using dropdown toggle instead of collapse toggle
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.