Lesson 8: Responsive Design: Media Queries, Fluid Units and Mobile First
A website today is read on phones, tablets, laptops, televisions and watches, and a page designed for only one screen size will look broken on all the others. Responsive design is the practice of building one page that adapts to any screen, and it rests on three ideas that work together: a flexible layout that stretches and reflows, fluid units that scale with the screen instead of fixed pixel sizes, and media queries, which are CSS rules that only apply when the screen meets a condition, such as being wider than a certain size. The approach is called mobile-first when you design the small screen first and add rules for larger screens, and this lesson shows you how to build that way from the start.
The viewport and the meta tag
The first step is a single line in the head of every page: <meta name="viewport" content="width=device-width, initial-scale=1">. Without it, a phone pretends to be a wide desktop screen, shrinks the whole page to fit, and the visitor has to pinch and zoom to read anything, which is the classic sign of a site that was never made responsive. With the tag, the browser uses the device's real width, your CSS sees the true screen size, and your media queries work as intended. It is a small line with an outsized effect, and forgetting it is the most common responsive-design mistake, so check for it first whenever a page looks wrong on a phone. Every page in this course includes it, and every page you build should too.
Fluid units instead of fixed pixels
Fixed pixel sizes break on different screens, so responsive design prefers units that scale. Percentages let an element take a share of its container, so width: 50% always means half, whatever the screen. The rem unit is based on the root font size, so padding: 1rem stays proportional if a visitor enlarges their text, which fixed pixels do not. The viewport units vw and vh are shares of the screen itself, so font-size: 4vw grows with the window, and the newer clamp() function sets a size with a minimum, a preferred value and a maximum, such as clamp(1.5rem, 4vw, 2.5rem), which keeps a heading readable on a phone without letting it grow enormous on a wide monitor. Images need the same treatment: img { max-width: 100%; height: auto; } stops an image overflowing its container on a narrow screen, and it belongs in every stylesheet you write.
/* Mobile-first: base styles are for small screens */
.card-row {
display: grid;
grid-template-columns: 1fr;
gap: 1rem;
}
/* Wider screens get more columns */
@media (min-width: 600px) {
.card-row { grid-template-columns: repeat(2, 1fr); }
}
@media (min-width: 900px) {
.card-row { grid-template-columns: repeat(3, 1fr); }
}
img { max-width: 100%; height: auto; }
Media queries and breakpoints
A media query is a CSS rule wrapped in a condition, such as @media (min-width: 600px) { ... }, which means “apply these styles only when the screen is at least 600 pixels wide”. In a mobile-first stylesheet, the base rules describe the small-screen layout, usually a single column, and each media query adds rules for a wider screen, such as two columns at 600 pixels and three at 900. The widths where the layout changes are called breakpoints, and the best way to choose them is to look at your page, shrink the browser window, and add a breakpoint where the design starts to look wrong, rather than copying a list of device sizes, because new devices arrive every year and your content should decide, not the hardware. Keep breakpoints few, name them by purpose in a comment, and test at widths between them, not only at the exact values.
Testing on real screens
You do not need a cupboard full of devices to test responsive design. Your browser's developer tools include a device toolbar that simulates phones and tablets, lets you drag the width to any size, and shows you how the page reflows, and that covers most testing. But do also check on a real phone at least once before you publish, because touch targets, font sizes and loading speed feel different on a real device: buttons should be at least 44 pixels tall so they are easy to tap, text should be readable without zooming, and the page should still work if images are slow to load. Finally, remember that responsive design is about people, not just pixels: a visitor on a phone may be on a slow connection or in bright sunlight, so keep pages light, keep the important content first, and make sure the page works with a keyboard as well as a mouse.
Watch & Learn
One video to watch for this unit, from a trusted educator.
- Learn Flexbox in 15 Minutes — Web Dev Simplified
A fast, clear tour of Flexbox: containers, direction, alignment and wrapping — the layout tool at the heart of responsive pages.
Key Terms
- Responsive design
- Building one page that adapts to any screen size, using flexible layouts, fluid units and media queries.
- Viewport
- The visible area of a web page on a device; the viewport meta tag tells phones to use their real width.
- Media query
- A CSS rule that applies only when a condition is met, such as @media (min-width: 600px).
- Breakpoint
- A screen width at which the layout changes, chosen where the design starts to look wrong.
- Mobile-first
- Designing the small-screen layout first, then adding rules for larger screens.
- rem
- A unit based on the root font size, which scales when a visitor enlarges their text.
- clamp()
- A function setting a size with a minimum, a preferred value and a maximum, such as clamp(1.5rem, 4vw, 2.5rem).
Discussion Questions
- Why is it better to choose breakpoints by watching your design break than by copying a list of device sizes?
- A heading set to font-size: 32px looks fine on a laptop but huge on a phone. Which fluid approach would fix it, and why?
- Why should buttons be at least 44 pixels tall on touch screens?
Practice Questions
- What does the viewport meta tag do, and what happens without it?
- What is a media query?
- What does mobile-first mean?
- Why is img { max-width: 100%; height: auto; } important?
- What does clamp(1.5rem, 4vw, 2.5rem) do?
Show answers
- It tells a phone to use its real screen width. Without it, the phone pretends to be a wide desktop screen and shrinks the page, forcing visitors to pinch and zoom.
- A CSS rule that applies only when a condition is met. For example, @media (min-width: 600px) applies its styles only on screens at least 600 pixels wide.
- Design the small-screen layout first, then add rules for larger screens. Base styles cover phones, and media queries add columns and spacing as the screen grows.
- It stops images overflowing narrow screens. The image can shrink to fit its container but never stretch larger than its real size.
- It sets a size that scales with the screen but stays between limits. The preferred size is 4vw, never smaller than 1.5rem and never larger than 2.5rem.