Lesson 6 of 10
Responsive Navbar with Flexbox
Build a complete responsive navigation bar from scratch using Flexbox. The navbar stacks vertically on mobile and displays horizontally on desktop - the most common navigation pattern on the web.
Navbar Structure
- Container - display: flex with justify-content: space-between to split logo and links
- Logo area - flex: 0 0 auto to keep the logo at its natural width
- Nav links - display: flex with gap for horizontal spacing on desktop
- Mobile layout - flex-direction: column on the links for vertical stacking
- Hamburger toggle - Show/hide nav links with a class toggle (usually JavaScript)
- Desktop breakpoint - @media (min-width: 768px) switches to horizontal layout
- Active state - Highlight the current page link with border-bottom or background
Mini-Build: Responsive Navbar
Tip
Add a transition: color 0.2s ease to nav links and a colored bottom border or background on hover. This subtle animation makes the navbar feel polished and professional - users notice the attention to detail even if they don't consciously register it.
Flexbox easily switches navigation menus from horizontal rows to vertical mobile stacks
Common Mistake
Making the entire navbar a flex container with both logo and individual links as direct children. Instead, keep logo and the <ul> nav-links as the two flex items using justify-content: space-between, then make the <ul> a separate flex container with gap for the individual links.
Practice Task
Enhance the navbar in the editor above: (1) Add a transition: color 0.2s to nav links, (2) Add a 2px bottom border on the .active link using border-bottom: 2px solid #E44D26, (3) Style a CTA button (e.g., 'Get Started') with background: #E44D26 and push it right with margin-left: auto.
Quick Quiz
Key Takeaways
- Build a complete responsive navigation bar from scratch using Flexbox.
- Container - display: flex with justify-content: space-between to split logo and links
- Logo area - flex: 0 0 auto to keep the logo at its natural width
- Nav links - display: flex with gap for horizontal spacing on desktop
Finished this lesson?