A navbar brand shows your website's name or logo at the top. The toggler button helps users open or close the menu on small screens.
Navbar brand and toggler in Bootsrap
<nav class="navbar"> <a class="navbar-brand" href="#">BrandName</a> <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarMenu" aria-controls="navbarMenu" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarMenu"> <!-- menu items here --> </div> </nav>
The navbar-brand is usually a link with your logo or name.
The navbar-toggler button controls showing or hiding the menu on small screens.
<a class="navbar-brand" href="#">MySite</a>
menu on small screens.<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#menu" aria-controls="menu" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button>
<nav class="navbar navbar-expand-lg navbar-light bg-light"> <a class="navbar-brand" href="#">Logo</a> <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navContent" aria-controls="navContent" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navContent"> <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>
This example shows a navbar with a brand name "MyWebsite" and a toggler button. On small screens, the menu items collapse and can be opened by clicking the toggler button.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Navbar Brand and Toggler Example</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet" /> </head> <body> <nav class="navbar navbar-expand-lg navbar-light bg-light"> <div class="container-fluid"> <a class="navbar-brand" href="#">MyWebsite</a> <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarNav"> <ul class="navbar-nav"> <li class="nav-item"> <a class="nav-link active" aria-current="page" href="#">Home</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Features</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Pricing</a> </li> </ul> </div> </div> </nav> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script> </body> </html>
Always include aria-label on the toggler button for screen readers.
Use navbar-expand-lg or similar classes to control when the menu collapses.
Make sure to include Bootstrap's JavaScript bundle for the toggler to work.
The navbar-brand shows your site name or logo.
The navbar-toggler button helps open and close the menu on small screens.
Use Bootstrap classes and attributes to make the navbar responsive and accessible.
Practice
navbar-brand class in a Bootstrap navbar?Solution
Step 1: Understand the role of
Thenavbar-brandnavbar-brandclass 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 CQuick Check:
navbar-brand = site name/logo [OK]
- Confusing navbar-brand with navbar-toggler
- Thinking navbar-brand adds navigation links
- Assuming navbar-brand controls styling only
Solution
Step 1: Check Bootstrap 5 toggler button syntax
The toggler is abuttonwith classnavbar-toggler, typebutton, and usesdata-bs-toggle="collapse"anddata-bs-targetto control the menu.Step 2: Verify accessibility attributes
It includesaria-controls,aria-expanded, andaria-labelfor screen readers.Final Answer:
Option A matches the correct Bootstrap 5 toggler button structure -> Option AQuick Check:
Correct toggler uses button with data-bs-toggle and aria-label [OK]
- Using <div> instead of <button> for toggler
- Wrong data attributes like data-toggle instead of data-bs-toggle
- Missing aria-label for accessibility
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">MySite</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navMenu" aria-controls="navMenu" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navMenu">
<ul class="navbar-nav me-auto">
<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 navbar-expand-lg behavior
The classnavbar-expand-lgmeans the navbar expands (shows menu) on large screens and collapses on smaller screens.Step 2: Check toggler and collapse relationship
The toggler button controls thedivwithcollapse navbar-collapseand idnavMenu. On small screens, the menu is hidden and toggled by the button.Final Answer:
The menu items will be hidden and can be toggled by clicking the hamburger button -> Option DQuick Check:
navbar-expand-lg collapses menu on small screens [OK]
- Thinking menu is always visible
- Assuming navbar-brand disappears on small screens
- Confusing toggler with modal trigger
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#menu" aria-controls="menu" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button>
Solution
Step 1: Check Bootstrap 5 data attribute naming
Bootstrap 5 requiresdata-bs-toggleanddata-bs-targetinstead ofdata-toggleanddata-target.Step 2: Verify other attributes
The button is properly closed,aria-expandedcan be false initially, andnavbar-toggler-iconis a valid class.Final Answer:
Incorrect data attribute names; should use data-bs-toggle and data-bs-target -> Option BQuick Check:
Bootstrap 5 uses data-bs-* attributes [OK]
- Using old Bootstrap 4 data attributes
- Changing aria-expanded default incorrectly
- Misnaming navbar-toggler-icon class
Solution
Step 1: Check brand logo and alt text
includes animgwith descriptivealt="Site Logo", which is good for accessibility. has empty alt, which is less accessible.Step 2: Verify toggler button attributes
uses correct Bootstrap 5 attributesdata-bs-toggle="collapse"anddata-bs-target="#navbarMenu"with proper aria attributes. uses olddata-toggleand misses some aria attributes. incorrectly usesdata-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 AQuick Check:
Brand with alt + toggler with data-bs-toggle="collapse" [OK]
- Missing alt text on brand logo image
- Using old data-toggle instead of data-bs-toggle
- Using dropdown toggle instead of collapse toggle
Ready to prove it?
Pass the test and this pattern moves unknown
