📖 Scenario: You are building a simple website header with a navigation bar. This navbar will help users move between different pages easily.
🎯 Goal: Create a basic responsive navigation bar using Bootstrap 5. The navbar should have a brand name and three links: Home, About, and Contact.
📋 What You'll Learn
Use Bootstrap 5 classes to create the navbar
Include a brand name on the left side
Add three navigation links: Home, About, Contact
Make the navbar responsive with a toggle button on small screens
Use semantic HTML and accessible attributes
💡 Why This Matters
🌍 Real World
Navigation bars are essential for websites to help users find pages easily. Bootstrap makes it simple to create responsive and accessible navbars.
💼 Career
Knowing how to build and customize navbars is a common skill required for front-end web development jobs.
Progress0 / 4 steps
1
Create the basic HTML structure for the navbar
Write the HTML skeleton for a Bootstrap navbar. Create a <nav> element with classes navbar and navbar-expand-lg. Inside it, add a <div> with class container-fluid. Inside this container, add a brand link with class navbar-brand and text MySite.
Bootsrap
Hint
Use the <a> tag with class navbar-brand inside the container.
2
Add the navbar toggler button for small screens
Inside the container-fluid div, after the brand link, add a button with class navbar-toggler. Set type="button", data-bs-toggle="collapse", data-bs-target="#navbarNav", aria-controls="navbarNav", aria-expanded="false", and aria-label="Toggle navigation". Inside the button, add a <span> with class navbar-toggler-icon.
Bootsrap
Hint
The toggler button uses data-bs-toggle="collapse" and targets #navbarNav.
3
Add the collapsible navigation links
After the toggler button, add a <div> with classes collapse navbar-collapse and id navbarNav. Inside it, add an unordered list <ul> with class navbar-nav. Inside the list, add three list items <li> with class nav-item. Each list item should contain a link <a> with class nav-link and href #. The links text should be Home, About, and Contact respectively.
Bootsrap
Hint
Wrap the links inside a div with collapse navbar-collapse and id navbarNav.
4
Add accessibility and responsive meta tag
Add the <meta> tag for viewport inside the <head> section: <meta name="viewport" content="width=device-width, initial-scale=1">. Also, add aria-current="page" attribute to the Home link to indicate the current page.
Bootsrap
Hint
The viewport meta tag helps the navbar be responsive on mobile devices. The aria-current="page" attribute marks the active link for screen readers.
Practice
(1/5)
1. What is the main purpose of a navbar in a website?
easy
A. To help users navigate through different pages easily
B. To display large images on the homepage
C. To add background music to the site
D. To store user data securely
Solution
Step 1: Understand the role of a navbar
A navbar is a menu bar that helps users find and visit different parts of a website.
Step 2: Match the purpose with options
Only To help users navigate through different pages easily describes navigation, which is the navbar's main job.
Final Answer:
To help users navigate through different pages easily -> Option A
Quick Check:
Navbar = Navigation aid [OK]
Hint: Navbar is for navigation menus, not media or data [OK]
Common Mistakes:
Confusing navbar with image or media display
Thinking navbar stores data
Assuming navbar plays audio
2. Which of the following is the correct Bootstrap class to create a responsive navbar container?
easy
A. <div class='navbar-container'>
B. <section class='navbar-expand'>
C. <header class='nav-responsive'>
D. <nav class='navbar navbar-expand-lg'>
Solution
Step 1: Identify Bootstrap navbar syntax
Bootstrap uses navbar and navbar-expand-lg classes on a <nav> element for responsive navbars.
Step 2: Compare options with Bootstrap docs
Only <nav class='navbar navbar-expand-lg'> uses correct element and classes as per Bootstrap standards.
A. The button uses old data attributes; should use data-bs-toggle and data-bs-target
B. Missing navbar-brand class on the brand link
C. The nav-item class is incorrect; should be nav-link
D. The navbar is missing a background color class
Solution
Step 1: Check Bootstrap 5 data attribute syntax
Bootstrap 5 requires data-bs-toggle and data-bs-target instead of data-toggle and data-target.
Step 2: Verify other classes
The brand link has correct class, nav-item and nav-link are used properly, and background class bg-dark is present.
Final Answer:
The button uses old data attributes; should use data-bs-toggle and data-bs-target -> Option A
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
Confusing nav-item and nav-link classes
Forgetting background color class
5. You want a navbar that always stays visible at the top when scrolling and collapses on small screens. Which combination of Bootstrap classes should you use?
hard
A. navbar fixed-bottom navbar-expand-md navbar-light bg-light
B. navbar fixed-top navbar-expand-sm navbar-light bg-light
C. navbar sticky-top navbar-expand-lg navbar-dark bg-dark
D. navbar sticky-top navbar-expand-xl navbar-light bg-transparent
Solution
Step 1: Understand fixed vs sticky navbar
fixed-top fixes navbar always at top, sticky-top sticks it after scrolling. Both keep navbar visible, but sticky-top is preferred for smooth behavior.
Step 2: Check collapse breakpoint and color classes
navbar-expand-lg collapses below large screens, navbar-dark bg-dark gives dark style with good contrast.