A Understanding the Pattern
Goal: Understand how desktop sidebars transform for mobile devices.
π The Pattern:
You've already built a beautiful desktop sidebar layout in the previous lesson using Flexbox. But what happens when someone views it on a phone?
A fixed sidebar works great on desktop where horizontal space is abundant. On mobile devices, however, that sidebar would consume 300px of precious screen spaceβleaving barely any room for content!
The solution? Transform it into an off-canvas menu that slides in when needed.
π€ Why transform the sidebar?
- Space efficiency: Mobile screens are narrowβa 300px sidebar would take up most of the width
- User expectations: Mobile users expect hamburger menus and top navigation
- Touch targets: Vertical menus work better with thumb-friendly tap zones
- Content priority: On mobile, content should be the focus, not navigation
π― What We'll Build:
- Desktop (>768px): Sidebar visible on the left (what you have now)
- Mobile (β€768px): Sidebar slides off-screen, hamburger menu appears at top
- Click hamburger: Sidebar slides in from the left
- Click Γ or link: Sidebar slides back out
π§ Reflection:
Think about mobile apps and websites you use daily. How do they handle navigation? What patterns do you see repeated?
Auto-saved β
B Add Checkbox Hack HTML
Goal: Add the HTML elements needed for the mobile menu (checkbox, mobile header, hamburger icon).
π Quick Note:
You might notice your images are overflowing in the CodePen preview - that's okay! We'll fix that in Part D using responsive image techniques. For now, focus on adding the mobile menu structure.
π‘ The "Checkbox Hack"
We're going to use a clever CSS technique called the "checkbox hack" to control the menu without JavaScript. A hidden checkbox will track whether the menu is open or closed, and CSS will react to its :checked state.
What we're adding:
- A hidden
<input type="checkbox">to track menu state - A mobile header with hamburger icon (hidden on desktop)
- A close button (Γ) inside the sidebar (hidden on desktop)
π Step 1: Add the checkbox
Add this right at the start of your <main class="container"> element, before the sidebar:
<!-- Hidden checkbox for menu toggle -->
<input type="checkbox" id="sidebar-toggle" class="sidebar-toggle">
π‘ What is this?
A checkbox is a form element that can be checked or unchecked. We'll use CSS to hide it visually, but track its state to control the menu.
π Step 2: Add close button to sidebar
Inside your <nav class="sidebar">, add a close button at the top (after the <h2>):
<label for="sidebar-toggle" class="close-btn">×</label>
π‘ What's a label?
The <label> element is connected to our checkbox via the for="sidebar-toggle" attribute. Clicking this label will toggle the checkbox! The × is an HTML entity that displays as Γ.
π Step 3: Add mobile header
Add this right after your </nav> (sidebar) closing tag, but before <div class="content">:
<!-- Mobile header (hidden on desktop) -->
<div class="mobile-header">
<label for="sidebar-toggle" class="hamburger">
<span></span>
<span></span>
<span></span>
</label>
<h1>Responsive Web Design</h1>
</div>
π‘ The hamburger icon
The three empty <span> elements will become the three lines of the hamburger icon. We'll style them with CSS! This label also connects to our checkbox.
π Step 4: Hide mobile header on desktop
Right now the mobile header is showing on desktop - let's hide it! We'll reveal it later when we add our mobile media query.
/* Hide mobile header on desktop (we'll show it in our media query later) */
.mobile-header {
display: none;
}
π‘ Why hide it now?
The mobile header is only needed on small screens. We're hiding it with display: none on desktop. Later in Part F, we'll use a media query to change this to display: flex for mobile devices.
β οΈ Order matters!
The checkbox must come before the sidebar and mobile-header in your HTML. CSS can only select elements that come after the checkbox using the sibling selector (~).
π§ Reflection:
Why use a checkbox instead of just a button? What advantage does the :checked state give us?
Auto-saved β
C Add Images (Not Responsive Yet!)
Goal: Add images to your content and discover why fixed widths break responsive layouts.
π· Step 1: Add images to your sections
Let's add images to make your content more visual. Add an <img> tag at the top of 2-3 of your sections (right after the <h2>).
Copy and paste these into your sections:
<!-- In #intro section (right after <h2>Introduction...</h2>) -->
<img src="https://placehold.co/800x400/667eea/white?text=Responsive+Design"
alt="Responsive Design Concept">
<!-- In #principles section (right after <h2>Core Principles</h2>) -->
<img src="https://placehold.co/800x400/764ba2/white?text=Core+Principles"
alt="Core Principles">
<!-- In #layouts section (right after <h2>Flexible Layouts</h2>) -->
<img src="https://placehold.co/800x400/3498db/white?text=Flexible+Layouts"
alt="Flexible Layouts">
π¨ Step 2: Set fixed width in CSS
Add this CSS to style your images with a fixed width:
π Step 3: Test the problem!
Now resize your browser window to mobile size (~400px width).
β οΈ What's wrong?
Your 800px-wide images break the layout! They cause horizontal scrolling and make the content area much wider than the screen. This is exactly why we need responsive images!
π§ Reflection:
What happens when you resize your browser to a narrow width? Why does the fixed 800px width cause problems?
Auto-saved β
D Make Images Responsive
Goal: Fix the image overflow problem by making images scale proportionally to their container.
β¨ The Solution: max-width
The fix is surprisingly simple! Replace width: 800px with max-width: 100%.
π‘ How it works:
max-width: 100%β Image can be up to its natural width, but never wider than its containerheight: autoβ Maintains the image's aspect ratio as it scales- On large screens: Image displays at full size (800px)
- On small screens: Image shrinks to fit the container
π¨ Update your CSS:
Change your image CSS from the fixed width to this:
π Test it!
Resize your browser window again. Your images should now scale smoothly! They never overflow their container, and they maintain their aspect ratio.
π Problem solved!
This simple techniqueβmax-width: 100% and height: autoβis the foundation of responsive images. It works for most use cases without any complex code.
π§ Reflection:
What's the difference between width: 100% and max-width: 100%? When would you use each?
Auto-saved β
E Style Mobile Header & Hamburger
Goal: Style the mobile header and hamburger icon (they'll be hidden on desktop, visible on mobile).
π± Step 1: Hide checkbox
First, hide the checkboxβit's purely functional. Add this to your CSS:
/* Hide checkbox (it's just for state tracking) */
.sidebar-toggle {
display: none;
}
π± Step 2: Hide close button (for now)
On desktop, we don't need the close button. Add this to your CSS:
/* Hide close button on desktop (show later on mobile) */
.close-btn {
display: none;
}
π± Step 3: Style mobile header
Style the mobile header (it's already hidden from Part B). Add this to your CSS:
/* Style mobile header (already hidden, will show in media query) */
.mobile-header {
background: #2c3e50;
color: white;
padding: 1rem 1.5rem;
align-items: center;
gap: 1rem;
position: sticky;
top: 0;
z-index: 999;
}
.mobile-header h1 {
margin: 0;
font-size: 1.3rem;
font-weight: 600;
}
π‘ Note:
We removed display: none since we already added that in Part B Step 4. This just adds the styling for colors, padding, and positioning.
π Step 4: Create hamburger icon
Style the three-line hamburger menu icon. Add this to your CSS:
/* Hamburger icon (three lines) */
.hamburger {
display: flex;
flex-direction: column;
gap: 5px;
cursor: pointer;
padding: 0.5rem;
}
.hamburger span {
width: 25px;
height: 3px;
background: white;
border-radius: 2px;
transition: all 0.3s ease;
}
π‘ How it works:
We use flexbox with flex-direction: column to stack the three <span> elements vertically. Each span becomes a white line with width: 25px and height: 3px.
π‘ What we've done:
We've added all the mobile elements, but they're hidden on desktop. In the next section, we'll use a media query to show them on mobile and hide the sidebar!
F Media Query for Mobile Layout
Goal: Use a media query to transform the layout on screens 768px and below.
π± The Media Query
Now we'll add a media query that changes everything at 768px and below. All the following steps go inside this:
@media (max-width: 768px) {
/* Steps go here */
}
π What's a media query?
Media queries let you apply CSS based on device characteristics like screen width. @media (max-width: 768px) means "apply these styles when the screen is 768px or narrower."
π¨ Step 1: Show mobile header
Inside your @media (max-width: 768px) query, add this code to reveal the mobile header:
/* Show mobile header on small screens */
.mobile-header {
display: flex;
}
π¨ Step 2: Stack layout vertically
Change the container from horizontal to vertical layout. Add this inside your media query:
/* Stack layout vertically on mobile */
.container {
flex-direction: column;
}
π¨ Step 3: Transform sidebar off-canvas
This is where the magic happens! We'll use CSS transforms to slide the sidebar off-screen. Add this inside your media query:
/* Hide sidebar off-screen on mobile */
.sidebar {
position: fixed;
top: 0;
left: 0;
height: 100vh;
width: 280px;
transform: translateX(-100%);
transition: transform 0.3s ease;
z-index: 1000;
}
π‘ How it works:
transform: translateX(-100%) moves the sidebar 100% of its width to the left (off-screen). The transition makes it slide smoothly. We'll reverse this when the checkbox is checked!
π¨ Step 4: Show close button
Now reveal the close button (Γ) inside the mobile sidebar. Add this inside your media query:
/* Show close button in mobile sidebar */
.close-btn {
display: block;
position: absolute;
right: 1.5rem;
top: 1.5rem;
font-size: 2rem;
cursor: pointer;
background: none;
border: none;
color: white;
line-height: 1;
}
β¨ Step 5: The magic - slide in when checked!
This is THE key moment! When the checkbox is checked (by clicking hamburger or close button), slide the sidebar back in. Add this inside your media query:
/* When checkbox is checked, slide sidebar in */
.sidebar-toggle:checked ~ .sidebar {
transform: translateX(0);
}
/* Add dark overlay behind sidebar */
.sidebar-toggle:checked ~ .sidebar::before {
content: '';
position: fixed;
top: 0;
left: 280px;
right: 0;
bottom: 0;
background: rgba(0,0,0,0.5);
z-index: -1;
}
π The Checkbox Hack!
The :checked selector detects when the checkbox is checked. The ~ (sibling selector) targets the sidebar that comes after the checkbox in your HTML. When checked, we reset the transform to translateX(0), sliding it back on screen!
π Test it!
Resize your browser to mobile width. Click the hamburger iconβthe sidebar should slide in! Click the Γ to close it.
π§ Reflection:
Why use transform: translateX(-100%) instead of left: -280px? (Hint: think about performance)
Auto-saved β
π Congratulations!
You've successfully built a fully functional responsive navigation! You've learned:
- β The checkbox hack for CSS-only interactivity
- β Responsive image techniques using
max-width: 100% - β CSS transforms and transitions for smooth animations
- β Media queries to adapt layouts for mobile devices
- β The off-canvas navigation pattern
π What you built:
- Desktop (>768px): Fixed sidebar navigation
- Mobile (β€768px): Hamburger menu with slide-in sidebar
- Pure CSS: No JavaScript required for core functionality!
- Smooth animations: Professional sliding effects and overlay
π Want to learn more?
Future topics you could explore:
- JavaScript enhancement: Auto-close menu when clicking links
- Accessibility: Add ARIA labels and keyboard focus styles
- Advanced animations: Animate hamburger icon into X shape
- Nested menus: Add dropdown submenus to your navigation