Lesson 14: Navigation Bars - Building Menus with Flexbox
What You'll Learn
- How to create horizontal navigation bars
- How to create responsive navigation
- Hamburger menus for mobile
- Dropdown menus
- Sticky navigation
- Navigation styling and effects
Why This Matters
Navigation is how users move through your website. A well-designed, responsive navigation bar is essential for user experience. Flexbox makes creating professional navigation bars much easier!
Part 1: Basic Horizontal Navigation
Simple Horizontal Menu
<nav class="navbar">
<a href="#home">Home</a>
<a href="#about">About</a>
<a href="#services">Services</a>
<a href="#contact">Contact</a>
</nav>
.navbar {
display: flex;
background-color: #333;
padding: 0;
}
.navbar a {
color: white;
text-decoration: none;
padding: 20px 25px;
}
.navbar a:hover {
background-color: #555;
}
Part 2: Logo with Navigation
Layout with Logo
<header class="header">
<div class="logo">MyBrand</div>
<nav class="nav">
<a href="#home">Home</a>
<a href="#about">About</a>
<a href="#services">Services</a>
<a href="#contact">Contact</a>
</nav>
</header>
.header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 0 40px;
background-color: #2c3e50;
color: white;
}
.logo {
font-size: 24px;
font-weight: bold;
padding: 20px 0;
}
.nav {
display: flex;
gap: 0;
}
.nav a {
color: white;
text-decoration: none;
padding: 20px 20px;
transition: background-color 0.3s;
}
.nav a:hover {
background-color: #34495e;
}
.nav a:active,
.nav a.active {
background-color: #3498db;
}
Part 3: Responsive Navigation with Hamburger
HTML Structure
<header class="header">
<div class="logo">MyBrand</div>
<!-- Hamburger button (hidden on desktop) -->
<button class="hamburger">
<span></span>
<span></span>
<span></span>
</button>
<!-- Navigation menu -->
<nav class="nav">
<a href="#home">Home</a>
<a href="#about">About</a>
<a href="#services">Services</a>
<a href="#portfolio">Portfolio</a>
<a href="#contact">Contact</a>
</nav>
</header>
CSS - Mobile First
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.header {
position: relative;
display: flex;
justify-content: space-between;
align-items: center;
padding: 15px 20px;
background-color: #2c3e50;
color: white;
}
.logo {
font-size: 24px;
font-weight: bold;
z-index: 1001;
}
/ Hamburger Button - Mobile /
.hamburger {
display: flex;
flex-direction: column;
gap: 5px;
background: none;
border: none;
cursor: pointer;
z-index: 1001;
}
.hamburger span {
display: block;
width: 30px;
height: 3px;
background-color: white;
transition: all 0.3s;
}
/ Hamburger animation when open /
.hamburger.active span:nth-child(1) {
transform: rotate(45deg) translate(8px, 8px);
}
.hamburger.active span:nth-child(2) {
opacity: 0;
}
.hamburger.active span:nth-child(3) {
transform: rotate(-45deg) translate(8px, -8px);
}
/ Navigation - Mobile (hidden by default) /
.nav {
position: fixed;
top: 0;
right: -100%;
width: 70%;
height: 100vh;
background-color: #2c3e50;
display: flex;
flex-direction: column;
padding: 80px 20px 20px;
transition: right 0.3s;
z-index: 1000;
}
.nav.active {
right: 0;
}
.nav a {
color: white;
text-decoration: none;
padding: 15px 20px;
font-size: 18px;
border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}
.nav a:hover {
background-color: #34495e;
}
/ TABLET AND DESKTOP (768px+) /
@media (min-width: 768px) {
.hamburger {
display: none; / Hide hamburger /
}
.nav {
position: static;
width: auto;
height: auto;
flex-direction: row;
padding: 0;
background: none;
}
.nav a {
border: none;
font-size: 16px;
padding: 20px 20px;
}
}
JavaScript (for hamburger toggle)
Create a file: script.js
// Get elements
const hamburger = document.querySelector('.hamburger');
const nav = document.querySelector('.nav');
// Toggle menu on hamburger click
hamburger.addEventListener('click', () => {
hamburger.classList.toggle('active');
nav.classList.toggle('active');
});
// Close menu when clicking on a link
const navLinks = document.querySelectorAll('.nav a');
navLinks.forEach(link => {
link.addEventListener('click', () => {
hamburger.classList.remove('active');
nav.classList.remove('active');
});
});
Add to HTML before </body>:
<script src="script.js"></script>
Part 4: Sticky Navigation
Navigation that stays at top when scrolling:
.header {
position: sticky;
top: 0;
z-index: 1000;
display: flex;
justify-content: space-between;
align-items: center;
padding: 20px 40px;
background-color: #2c3e50;
color: white;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}
Scroll Effect (changes on scroll)
.header {
position: sticky;
top: 0;
transition: all 0.3s;
}
/ Add this class with JavaScript when scrolling /
.header.scrolled {
padding: 10px 40px;
background-color: rgba(44, 62, 80, 0.95);
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
}
JavaScript:
window.addEventListener('scroll', () => {
const header = document.querySelector('.header');
if (window.scrollY > 50) {
header.classList.add('scrolled');
} else {
header.classList.remove('scrolled');
}
});
Part 5: Dropdown Menus
HTML Structure
<nav class="nav">
<a href="#home">Home</a>
<a href="#about">About</a>
<!-- Dropdown menu -->
<div class="dropdown">
<a href="#services" class="dropdown-toggle">Services</a>
<div class="dropdown-menu">
<a href="#web-design">Web Design</a>
<a href="#web-dev">Web Development</a>
<a href="#seo">SEO</a>
<a href="#marketing">Marketing</a>
</div>
</div>
<a href="#contact">Contact</a>
</nav>
CSS for Dropdown
.nav {
display: flex;
align-items: center;
gap: 0;
}
.nav > a,
.dropdown-toggle {
color: white;
text-decoration: none;
padding: 20px 20px;
display: block;
}
/ Dropdown container /
.dropdown {
position: relative;
}
/ Dropdown menu (hidden by default) /
.dropdown-menu {
position: absolute;
top: 100%;
left: 0;
min-width: 200px;
background-color: #34495e;
display: none;
flex-direction: column;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}
/ Show dropdown on hover /
.dropdown:hover .dropdown-menu {
display: flex;
}
.dropdown-menu a {
color: white;
text-decoration: none;
padding: 12px 20px;
border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}
.dropdown-menu a:hover {
background-color: #2c3e50;
}
.dropdown-menu a:last-child {
border-bottom: none;
}
/ Dropdown arrow indicator /
.dropdown-toggle::after {
content: ' ▼';
font-size: 0.7em;
}
Part 6: Call-to-Action Button
Adding a special button to navigation:
<header class="header">
<div class="logo">MyBrand</div>
<nav class="nav">
<a href="#home">Home</a>
<a href="#about">About</a>
<a href="#services">Services</a>
<a href="#contact">Contact</a>
</nav>
<a href="#signup" class="cta-button">Sign Up</a>
</header>
.header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 15px 40px;
background-color: #2c3e50;
}
.nav {
display: flex;
flex: 1;
justify-content: center;
gap: 10px;
}
.cta-button {
padding: 10px 25px;
background-color: #e74c3c;
color: white;
text-decoration: none;
border-radius: 25px;
font-weight: bold;
transition: all 0.3s;
}
.cta-button:hover {
background-color: #c0392b;
transform: translateY(-2px);
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}
Part 7: Complete Example - Professional Navigation
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Professional Navigation</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<header class="header">
<div class="logo">
<span class="logo-icon">🚀</span>
<span class="logo-text">StartupCo</span>
</div>
<button class="hamburger" aria-label="Toggle menu">
<span></span>
<span></span>
<span></span>
</button>
<nav class="nav">
<a href="#home" class="active">Home</a>
<a href="#features">Features</a>
<div class="dropdown">
<a href="#products" class="dropdown-toggle">Products</a>
<div class="dropdown-menu">
<a href="#product-a">Product A</a>
<a href="#product-b">Product B</a>
<a href="#product-c">Product C</a>
</div>
</div>
<a href="#pricing">Pricing</a>
<a href="#about">About</a>
<a href="#contact">Contact</a>
</nav>
<a href="#demo" class="cta-button">Get Demo</a>
</header>
<!-- Demo content -->
<main style="height: 2000px; padding: 40px;">
<h1>Scroll to see sticky navigation</h1>
<p>Navigation stays at top when scrolling</p>
</main>
<script src="script.js"></script>
</body>
</html>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Tahoma, sans-serif;
}
/ Header /
.header {
position: sticky;
top: 0;
display: flex;
justify-content: space-between;
align-items: center;
padding: 15px 30px;
background-color: #ffffff;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
z-index: 1000;
transition: all 0.3s;
}
.header.scrolled {
padding: 10px 30px;
box-shadow: 0 2px 20px rgba(0, 0, 0, 0.15);
}
/ Logo /
.logo {
display: flex;
align-items: center;
gap: 10px;
font-size: 24px;
font-weight: bold;
color: #2c3e50;
z-index: 1001;
}
.logo-icon {
font-size: 32px;
}
/ Hamburger /
.hamburger {
display: none;
flex-direction: column;
gap: 5px;
background: none;
border: none;
cursor: pointer;
z-index: 1001;
}
.hamburger span {
width: 28px;
height: 3px;
background-color: #2c3e50;
transition: all 0.3s;
border-radius: 3px;
}
.hamburger.active span:nth-child(1) {
transform: rotate(45deg) translate(7px, 7px);
}
.hamburger.active span:nth-child(2) {
opacity: 0;
}
.hamburger.active span:nth-child(3) {
transform: rotate(-45deg) translate(7px, -7px);
}
/ Navigation /
.nav {
display: flex;
align-items: center;
gap: 5px;
}
.nav > a,
.dropdown-toggle {
color: #2c3e50;
text-decoration: none;
padding: 10px 18px;
font-weight: 500;
transition: all 0.3s;
border-radius: 6px;
}
.nav > a:hover,
.dropdown-toggle:hover {
background-color: #f0f0f0;
color: #3498db;
}
.nav > a.active {
background-color: #3498db;
color: white;
}
/ Dropdown /
.dropdown {
position: relative;
}
.dropdown-menu {
position: absolute;
top: calc(100% + 5px);
left: 0;
min-width: 180px;
background-color: white;
border-radius: 8px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
display: none;
flex-direction: column;
overflow: hidden;
}
.dropdown:hover .dropdown-menu {
display: flex;
}
.dropdown-menu a {
color: #2c3e50;
text-decoration: none;
padding: 12px 20px;
transition: all 0.2s;
}
.dropdown-menu a:hover {
background-color: #3498db;
color: white;
}
.dropdown-toggle::after {
content: ' ▼';
font-size: 0.7em;
}
/ CTA Button /
.cta-button {
padding: 10px 25px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
text-decoration: none;
border-radius: 25px;
font-weight: 600;
transition: all 0.3s;
white-space: nowrap;
}
.cta-button:hover {
transform: translateY(-2px);
box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4);
}
/ MOBILE (below 768px) /
@media (max-width: 767px) {
.hamburger {
display: flex;
}
.nav {
position: fixed;
top: 0;
right: -100%;
width: 80%;
max-width: 300px;
height: 100vh;
background-color: white;
flex-direction: column;
align-items: stretch;
padding: 80px 0 20px;
box-shadow: -2px 0 10px rgba(0, 0, 0, 0.1);
transition: right 0.3s;
overflow-y: auto;
}
.nav.active {
right: 0;
}
.nav > a,
.dropdown-toggle {
padding: 15px 30px;
border-radius: 0;
border-bottom: 1px solid #f0f0f0;
}
.dropdown-menu {
position: static;
box-shadow: none;
background-color: #f8f9fa;
display: none;
}
.dropdown.active .dropdown-menu {
display: flex;
}
.cta-button {
margin: 20px 30px 0;
text-align: center;
}
}
/ TABLET (768px - 1023px) /
@media (min-width: 768px) and (max-width: 1023px) {
.nav {
gap: 2px;
}
.nav > a,
.dropdown-toggle {
padding: 10px 12px;
font-size: 14px;
}
}
// script.js
const hamburger = document.querySelector('.hamburger');
const nav = document.querySelector('.nav');
const dropdowns = document.querySelectorAll('.dropdown');
// Toggle mobile menu
hamburger.addEventListener('click', () => {
hamburger.classList.toggle('active');
nav.classList.toggle('active');
});
// Close mobile menu when clicking links
document.querySelectorAll('.nav a').forEach(link => {
link.addEventListener('click', (e) => {
// Don't close if it's a dropdown toggle
if (!e.target.classList.contains('dropdown-toggle')) {
hamburger.classList.remove('active');
nav.classList.remove('active');
}
});
});
// Mobile dropdown toggle
dropdowns.forEach(dropdown => {
const toggle = dropdown.querySelector('.dropdown-toggle');
toggle.addEventListener('click', (e) => {
if (window.innerWidth < 768) {
e.preventDefault();
dropdown.classList.toggle('active');
}
});
});
// Scroll effect
window.addEventListener('scroll', () => {
const header = document.querySelector('.header');
if (window.scrollY > 50) {
header.classList.add('scrolled');
} else {
header.classList.remove('scrolled');
}
});
Practice Exercises
Exercise 1: Simple Navigation
Create a navigation bar with:
- Logo on left
- 5 menu items
- Hover effects
- Active state for current page
Exercise 2: Responsive Navigation
Create a fully responsive navigation:
- Desktop: horizontal menu
- Mobile: hamburger menu with slide-in
- Smooth transitions
Exercise 3: Advanced Navigation
Create navigation with:
- Sticky positioning
- Dropdown menus
- CTA button
- Scroll effects (shrinks on scroll)
What You Learned
- ✅ Creating horizontal navigation bars
- ✅ Logo and menu layout with Flexbox
- ✅ Responsive hamburger menus
- ✅ Dropdown menus
- ✅ Sticky navigation
- ✅ Scroll effects
- ✅ Navigation styling and animations
What's Next?
In the final lesson, you'll put everything together in a Complete Website Project - building a full multi-section website from scratch!