Lesson 7 of 10
Responsive Navigation Patterns
Navigation needs to work perfectly on every screen size. This mini-build covers the most common responsive navigation pattern: horizontal links on desktop, hamburger menu on mobile - built with CSS and minimal JavaScript.
Responsive Nav Strategy
- Desktop - Horizontal nav with links in a flex row. Logo + links visible side by side
- Mobile - Links hidden behind a hamburger icon. Toggle visibility with a class
- Hamburger icon - Built with CSS (3 spans) or a โฐ character. Toggles an 'active' class
- Slide-down transition - max-height: 0 โ max-height: 500px with overflow: hidden for smooth open/close
- Backdrop overlay - Semi-transparent overlay behind the mobile menu to indicate it's an overlay
- Focus management - Ensure keyboard users can navigate through menu items
- Breakpoint - Show hamburger below 768px, horizontal nav at 768px+
Mini-Build: Responsive Nav
Tip
Use the max-height transition trick for smooth mobile menu open/close: max-height: 0 โ max-height: 500px with transition: max-height 0.3s ease. This is smoother than toggling display: none (which can't be animated) and simpler than JavaScript-calculated heights.
Choose the right navigation pattern for screen size, thumb reachability, and information architecture
Common Mistake
Hiding the hamburger menu only with display: none without making it also inaccessible to screen readers. Add aria-expanded='true/false' on the button, and aria-hidden='true' on the hidden menu for proper accessibility. Screen reader users shouldn't encounter invisible navigation elements.
Practice Task
Improve the nav in the editor above: (1) Add a smooth open animation using max-height + transition instead of instant display toggle, (2) Animate the hamburger icon into an X when open using CSS transforms on the spans, (3) Press Tab in the browser to verify keyboard users can navigate all menu items.
Quick Quiz
Key Takeaways
- Navigation needs to work perfectly on every screen size.
- Desktop - Horizontal nav with links in a flex row. Logo + links visible side by side
- Mobile - Links hidden behind a hamburger icon. Toggle visibility with a class
- Hamburger icon - Built with CSS (3 spans) or a โฐ character. Toggles an 'active' class
Finished this lesson?