Lesson 11 / Responsive
Responsive Web Design
Build websites that adapt gracefully to different screen sizes and devices for a consistent user experience.
Teacher Notes / In-Class Use
Demo Live
- Walk through the interactive demo before students start changing their own project files.
- Connect the demo back to the first goal: Understand why responsive design matters across devices
Try In Class
- Create a responsive container using relative widths.
- Have students make one visible change, save, refresh, and explain what changed.
Submit Or Check
- Ask students to show the work in the browser, not only in the editor.
- Have students commit their progress with a clear message when the checkpoint is stable.
Watch For
- Students copying code without checking file paths, spelling, or capitalization.
- Visual changes that work locally but break when the project is published.
Learning Goals
- Understand why responsive design matters across devices
- Use fluid grids, flexible images, media queries, and breakpoints
- Apply responsive typography, responsive images, and accessible navigation patterns
Interactive Demo
How to use this demo.
Use the demo as a small lab. Change one thing, observe the result, then connect it back to your own project.
What To Try
- Drag the viewport width control from narrow to wide.
- Watch when the layout shifts from one column to multiple columns.
What Changes
- The preview responds to viewport width instead of a fixed device name.
- The code sample shows how media queries and flexible sizing support the change.
What To Notice
- Responsive design is about content adapting to available space.
- Breakpoints should respond to layout needs, not only common device sizes.
Apply It
- Resize your own project in the browser and note the first width where the layout breaks.
Interactive Demo
Responsive Layout Visualizer
Drag the viewport width slider to see how the same HTML can adapt across mobile, tablet, and desktop layouts.
.cards {
display: grid;
gap: 1rem;
grid-template-columns: 1fr;
}
@media (min-width: 700px) {
.cards {
grid-template-columns: repeat(2, 1fr);
}
} What to notice
- Mobile-first CSS starts with the smallest layout.
- Breakpoints should happen when the content needs more room.
- The layout changes while the HTML stays the same.
Try this
- Move slowly from 320px to 1200px.
- Notice when the layout changes from one to two to three columns.
- Copy the generated CSS into a practice page.
This demo uses extra JavaScript for teaching. The code sample shows the pattern to practice. View full demo source (opens in a new tab).
Introduction
Responsive web design is the practice of building websites that adapt gracefully to different screen sizes and devices.
This ensures a consistent and optimal user experience across desktops, laptops, tablets, and smartphones.
This lesson explores the core concepts of responsive design and demonstrates techniques for creating flexible and adaptable layouts.
The Importance of Responsive Design
In today's multi-device world, users access websites from a variety of screens. Responsive design is crucial for the following reasons:
- Improved User Experience: Provides a consistent and usable experience across all devices.
- Increased Reach: Reaches a wider audience by accommodating different devices.
- SEO Benefits: Google favors mobile-friendly websites in search rankings.
- Easier Maintenance: Manages a single website instead of separate versions for different devices.
Core Techniques
Responsive design relies on a few foundational techniques that work together to make layouts flexible and adaptable.
- Fluid Grids: Use relative units like percentages for widths instead of fixed units like pixels. This allows elements to scale proportionally to the screen size.
- Flexible Images: Prevent images from overflowing their containers by setting max-width: 100%; and height: auto;.
- Media Queries: Apply different CSS styles based on device characteristics like screen size, orientation, and resolution.
Media Queries: The Foundation of Responsive Design
Media queries allow you to define CSS rules that only apply when certain conditions are met, such as screen width or orientation.
@media (condition) {
/* CSS rules to apply when the condition is true */
} Common Conditions
- min-width: Applies styles when the screen width is at least the specified value.
- max-width: Applies styles when the screen width is at most the specified value.
- orientation: Applies styles based on screen orientation, portrait or landscape.
Breakpoints
Breakpoints are specific screen widths that define the points at which your layout or content should change.
- Mobile: Around 320px - 480px
- Tablet: Around 768px - 1024px
- Desktop: Around 1280px and above
Media Query Example
/* Default styles for larger screens */
.container {
width: 960px;
margin: 0 auto;
}
/* Styles for smaller screens */
@media (max-width: 768px) {
.container {
width: 90%;
}
} Combining Conditions
You can combine multiple conditions using logical operators like and, or, and not.
@media (min-width: 768px) and (orientation: landscape) {
/* Styles for landscape tablets */
}
@media (max-width: 768px) or (orientation: portrait) {
/* Styles for smaller screens or portrait orientation */
} Other Media Features
- aspect-ratio: Target devices with specific aspect ratios.
- resolution: Apply styles for high-DPI screens.
- color: Target devices with different color capabilities.
- hover: Distinguish between touch and mouse interactions.
Mobile-First vs. Desktop-First
- Mobile-First: Start with styles for the smallest screen and progressively add styles for larger screens using min-width media queries. This approach is generally favored for performance and maintainability.
- Desktop-First: Start with styles for the largest screen and progressively add styles for smaller screens using max-width media queries.
Viewport Meta Tag
The viewport meta tag is essential for controlling how a web page is scaled and displayed on mobile devices.
- width=device-width: Sets the viewport width to the device's screen width.
- initial-scale=1.0: Sets the initial zoom level to 1.0, no zoom.
<meta name="viewport" content="width=device-width, initial-scale=1.0"> Responsive Images
Responsive images ensure that images display properly and efficiently on different screen sizes and resolutions.
img {
max-width: 100%;
height: auto;
} Using srcset and sizes
This setup lets the browser choose the best image source based on the screen size and resolution.
<img src="image.jpg"
srcset="image-small.jpg 300w, image-medium.jpg 600w, image-large.jpg 1200w"
sizes="(max-width: 600px) 100vw, (max-width: 1000px) 50vw, 33vw"> Using the picture Element
This allows different image sources to be served based on screen size, optimizing quality and performance.
<picture>
<source media="(max-width: 600px)" srcset="image-small.jpg">
<source media="(max-width: 1000px)" srcset="image-medium.jpg">
<img src="image-large.jpg" alt="Description">
</picture> Modern Image Formats
Consider using formats like WebP for better compression and quality. The picture element can help serve WebP to browsers that support it.
Responsive Typography
Ensure that text remains readable and attractive on all screen sizes.
body {
font-size: 16px;
}
h1 {
font-size: 2rem;
}
p {
font-size: 1.2em;
} Viewport Units
h1 {
font-size: 4vw;
} Using Media Queries for Typography
@media (max-width: 768px) {
h1 {
font-size: 3rem;
}
p {
line-height: 1.5;
}
} Other Tips
- Use heavier font weights for small screens.
- Adjust line length with max-width.
Responsive Navigation Examples
Navigation often needs special attention to ensure usability on smaller screens. Below are two popular mobile navigation patterns.
- Hamburger Menu: A collapsible menu that reveals items when the button is clicked. Useful for conserving space.
- Off-Canvas Menu: A side-sliding menu that appears from the edge of the screen. Good for more complex navigations.
Accessibility
Make sure navigation menus and other components are usable by everyone.
- Use ARIA attributes like aria-expanded and aria-controls.
- Ensure keyboard operability.
Testing and Debugging
- Browser DevTools: Use to simulate screen sizes and debug styles.
- Real Devices: Always verify on physical devices when possible.
Additional Resources
Responsive Web Design Basics | Media Queries | Responsive Images
Checkpoint
Before moving on, make sure these feel true.
- I can explain the main concept in my own words.
- I can apply this lesson to my current project.
- I can verify the result in the browser.
- I can commit the change with a clear message.
Project Connection
This lesson supports current class projects.
Practice
- Create a responsive container using relative widths.
- Add a media query that changes layout below 768px.
- Make images flexible with max-width and height auto.
- Test the page in browser DevTools and on a real device if possible.