Performance: Navigation bar patterns
Navigation bar patterns affect page load speed and interaction responsiveness, especially on mobile devices.
<nav class="flex flex-col"> <ul> <li> <button aria-expanded="false" aria-controls="menu" class="px-4 py-2" id="menu-button">Menu</button> <ul id="menu" class="hidden bg-white shadow-lg" role="menu" aria-labelledby="menu-button"> <li><a href="#" class="block px-4 py-2">Item 1</a></li> <li><a href="#" class="block px-4 py-2">Item 2</a></li> <li><a href="#" class="block px-4 py-2">Item 3</a></li> </ul> </li> </ul> </nav> <script> const btn = document.getElementById('menu-button'); const menu = document.getElementById('menu'); btn.addEventListener('click', () => { const expanded = btn.getAttribute('aria-expanded') === 'true'; btn.setAttribute('aria-expanded', String(!expanded)); menu.classList.toggle('hidden'); }); </script>
<nav class="flex flex-col"> <ul> <li class="group relative"> <button class="px-4 py-2">Menu</button> <ul class="absolute hidden group-hover:block bg-white shadow-lg"> <li><a href="#" class="block px-4 py-2">Item 1</a></li> <li><a href="#" class="block px-4 py-2">Item 2</a></li> <li><a href="#" class="block px-4 py-2">Item 3</a></li> </ul> </li> </ul> </nav>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Hover-based dropdown with absolute positioning | High (nested lists) | Multiple on hover | High (shadows, repaint) | [X] Bad |
| Click-toggle dropdown with minimal DOM | Low (flat structure) | Single on toggle | Low (simple styles) | [OK] Good |
flex flex-row arranges items in a row horizontally.justify-between spreads items evenly with space between them.<nav class="flex justify-center space-x-6 bg-gray-100 p-4"> <a href="#" class="text-blue-600 hover:text-blue-800">Home</a> <a href="#" class="text-blue-600 hover:text-blue-800">About</a> <a href="#" class="text-blue-600 hover:text-blue-800">Contact</a> </nav>
flex justify-center space-x-6 centers links horizontally with 1.5rem space between each link.bg-gray-100 p-4 adds a light gray background and padding. Link text is blue and darkens on hover due to text-blue-600 hover:text-blue-800.<nav class="flex flex-row space-y-4"> <a href="#" class="px-3 py-2">Home</a> <a href="#" class="px-3 py-2">Services</a> <a href="#" class="px-3 py-2">Contact</a> </nav>
flex-row arranges items horizontally, but space-y-4 adds vertical spacing between items instead of horizontal spacing.space-x-4 should be used instead of space-y-4.flex flex-col lg:flex-row means default is vertical stacking, switching to horizontal on large screens.space-y-4 adds vertical spacing by default, lg:space-y-0 lg:space-x-6 removes vertical spacing and adds horizontal spacing on large screens.Pass the test and this pattern moves unknown