Performance: Navbar brand and toggler
This affects the initial page load speed and interaction responsiveness when toggling the navigation menu on small screens.
<nav class="navbar navbar-expand-lg"> <a class="navbar-brand" href="#">Brand</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"> <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> <li class="nav-item"><a class="nav-link" href="#">Contact</a></li> </ul> </div> </nav>
<nav class="navbar"> <div class="navbar-brand">Brand</div> <button class="navbar-toggler" onclick="toggleMenu()">Toggle</button> <div id="menu" style="display:none;"> <a href="#">Home</a> <a href="#">About</a> <a href="#">Contact</a> </div> </nav> <script> function toggleMenu() { const menu = document.getElementById('menu'); if (menu.style.display === 'none') { menu.style.display = 'block'; } else { menu.style.display = 'none'; } } </script>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Direct style toggling (display:none/block) | Minimal DOM nodes | 1 reflow per toggle | 1 paint per toggle | [X] Bad |
| Bootstrap collapse with class toggling | Minimal DOM nodes | 0 reflows, only composite | Low paint cost | [OK] Good |
navbar-brand class in a Bootstrap navbar?navbar-brandnavbar-brand class is used to show the website's name or logo in the navbar.button with class navbar-toggler, type button, and uses data-bs-toggle="collapse" and data-bs-target to control the menu.aria-controls, aria-expanded, and aria-label for screen readers.<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>navbar-expand-lg means the navbar expands (shows menu) on large screens and collapses on smaller screens.div with collapse navbar-collapse and id navMenu. On small screens, the menu is hidden and toggled by the button.<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>
data-bs-toggle and data-bs-target instead of data-toggle and data-target.aria-expanded can be false initially, and navbar-toggler-icon is a valid class.img with descriptive alt="Site Logo", which is good for accessibility. has empty alt, which is less accessible.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.Pass the test and this pattern moves unknown