Responsive navbar collapse in Bootsrap - Mini Project: Build & Apply
<nav> element with the Bootstrap 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. Also add an unordered list <ul> with class navbar-nav containing three list items <li> each with class nav-item. Each list item should have a link <a> with class nav-link and text Home, About, and Contact respectively.Start by creating a <nav> with classes navbar navbar-expand-lg. Then add a container <div> with class container-fluid. Inside, add the brand link and the navigation list with three links.
navbar-toggler, type button, and these attributes: 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.Add a button with class navbar-toggler and the required Bootstrap data attributes for toggling the collapse. Inside the button, add a <span> with class navbar-toggler-icon.
<ul> with class navbar-nav inside a <div> with classes collapse navbar-collapse and id navbarSupportedContent. This container will collapse and expand when the toggler button is clicked.Wrap the <ul> inside a <div> with classes collapse navbar-collapse and id navbarSupportedContent. This makes the menu collapsible.
<head> section and the Bootstrap 5 JavaScript bundle script before the closing </body> tag. Use the official Bootstrap CDN URLs: CSS link href https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css with rel="stylesheet", and JS script src https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js. Also add the required <!DOCTYPE html>, <html lang="en">, <meta charset="UTF-8">, and viewport meta tag <meta name="viewport" content="width=device-width, initial-scale=1"> in the <head>.Wrap your navbar in a full HTML document. Add the required meta tags in the <head>. Include the Bootstrap CSS link in the <head> and the Bootstrap JS bundle script before the closing </body> tag.
Practice
navbar-toggler button in a Bootstrap responsive navbar?Solution
Step 1: Understand the role of
Thenavbar-togglernavbar-togglerbutton is designed to toggle (show/hide) the navigation links when the screen is small.Step 2: Connect to responsive behavior
On small screens, the menu collapses and the toggler button controls its visibility.Final Answer:
To show or hide the navigation menu on small screens -> Option BQuick Check:
Navbar toggler = show/hide menu [OK]
- Thinking it changes navbar color
- Confusing it with logo placement
- Assuming it fixes navbar position
Solution
Step 1: Identify the correct class order
Bootstrap requires the classescollapseandnavbar-collapsetogether to make the menu collapsible.Step 2: Confirm the required classes
The combinationcollapse navbar-collapseprovides both the collapse functionality and navbar styling.Final Answer:
<div class='collapse navbar-collapse'> -> Option CQuick Check:
Collapse + navbar-collapse = collapsible menu [OK]
- Missing the collapse class
- Using 'navbar-toggler' instead
- Mixing 'navbar-expand' with collapse
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navMenu">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navMenu">
<ul class="navbar-nav">
<li class="nav-item"><a class="nav-link" href="#">Home</a></li>
<li class="nav-item"><a class="nav-link" href="#">About</a></li>
</ul>
</div>
</nav>Solution
Step 1: Understand the toggler and collapse connection
The button toggles the element with IDnavMenuwhich has classescollapse navbar-collapse.Step 2: Behavior on small screens
On small screens, the menu is hidden by default and appears when the toggler is clicked, then hides again on next click.Final Answer:
The menu items appear or disappear when the toggler is clicked -> Option DQuick Check:
Toggler click = toggle menu visibility [OK]
- Thinking button needs an ID instead of data-bs-target
- Assuming color changes on toggle
- Believing menu is always visible
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#menu">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="menu">
<ul class="navbar-nav">
<li class="nav-item"><a class="nav-link" href="#">Link</a></li>
</ul>
</div>
</nav>Solution
Step 1: Check Bootstrap 5 attribute naming
Bootstrap 5 usesdata-bs-toggleanddata-bs-targetinstead ofdata-toggleanddata-target.Step 2: Identify impact on collapse functionality
Using old attribute names prevents the toggler from controlling the collapse behavior.Final Answer:
Use data-bs-toggle and data-bs-target attributes -> Option AQuick Check:
Bootstrap 5 uses data-bs-* attributes [OK]
- Using old Bootstrap 4 attribute names
- Adding wrong classes to button or div
- Missing id attribute on collapse div
<nav> element to achieve this?Solution
Step 1: Understand Bootstrap expand classes
The classnavbar-expand-lgmeans the navbar is expanded from large screens (992px) and larger, and collapses below large (medium and smaller).Step 2: Match requirement to class
Collapses on medium (768px) and smaller, expands on large and above:navbar-expand-lg.Final Answer:
navbar-expand-lg -> Option AQuick Check:
Expand from lg up means collapse md and below [OK]
- Choosing navbar-expand-md which expands from medium (too early)
- Using navbar-expand-sm which expands too early
- Confusing xl with lg breakpoints
Ready to prove it?
Pass the test and this pattern moves unknown
