Responsive Web Design Explained
How one page adapts to phones, tablets and wide screens using flexible layouts, media queries and responsive images.
Key takeaways
- A responsive site serves the same content at the same address and adapts its layout with CSS.
- The viewport meta tag is required for phones to lay the page out at their real width.
- Flexbox, grid and flexible units let many layouts adapt with few or no breakpoints.
- Add breakpoints where your content starts to look wrong, not to match particular device models.
- Test on real hardware, at high zoom and in both orientations.
Responsive web design means one set of pages, with one set of addresses, that rearranges itself to suit the space it's given. It relies on a few ingredients: a viewport declaration, layouts measured in flexible units, images that scale and CSS rules that change the arrangement when the available width or the user's preferences change.
This article explains each ingredient with short examples, shows how to choose breakpoints and lists the problems that most often appear on real devices.
Start with the viewport
Early mobile browsers assumed web pages were built for wide desktop screens, so they laid them out on a wide virtual canvas and shrank the result. Your page has to opt out:
<meta name="viewport" content="width=device-width, initial-scale=1"> This tells the browser to make the layout width match the device width in CSS pixels. A CSS pixel is a unit of layout that may span several physical pixels on a dense screen. Don't add settings that block zooming, such as user-scalable=no, because people need to enlarge text.
The three classic ingredients
The approach became widely known under the name "responsive web design" around 2010, and its core ideas still hold.
Plain HTML is already responsive: text wraps to whatever width it gets. Most layout trouble comes from fixed widths added on top.
Build a fluid layout
Use percentages, fr units, rem and max-width instead of fixed pixel widths. A readable article column can be as simple as:
.article {
max-width: 70ch;
margin-inline: auto;
padding-inline: 1rem;
} CSS Grid can create columns that wrap on their own:
.cards {
display: grid;
gap: 1rem;
grid-template-columns: repeat(auto-fill, minmax(min(16rem, 100%), 1fr));
} Each card is at least 16rem wide where there's room, and the browser fits as many columns as it can. The min() guard prevents overflow on very narrow screens. Flexbox with flex-wrap: wrap does a similar job for rows of items such as navigation links or tags.
For type and spacing that scale smoothly, clamp() sets a minimum, a preferred value and a maximum:
h1 { font-size: clamp(1.75rem, 1.2rem + 2.5vw, 3rem); } Including a rem part in the middle value keeps the text responsive to user zoom and font settings.
Add media queries where content needs them
A media query applies rules only when a condition holds:
.layout { display: block; }
@media (min-width: 48rem) {
.layout {
display: grid;
grid-template-columns: 16rem 1fr;
gap: 2rem;
}
} Below the breakpoint, everything stacks. Above it, a sidebar appears beside the content. Choose that number by widening the window until the stacked layout looks stretched or the two-column one has enough room. Device sizes change constantly, so content is the stable reference.
Media queries can also test orientation, pointer precision, hover ability and preferences:
| Query | Useful for |
|---|---|
(min-width: 60rem) | Adding columns |
(pointer: coarse) | Larger touch targets |
(hover: hover) | Hover-only enhancements |
(prefers-color-scheme: dark) | Dark theme |
(prefers-reduced-motion: reduce) | Calming animation |
print | Cleaner printed pages |
Screen width doesn't tell you the input method. A laptop may have a touchscreen and a tablet may have a mouse, so don't hide functions based on guesses.
Container queries: respond to the space around a component
A card might sit in a narrow sidebar on one page and a wide main area on another. Container queries let it respond to its container instead of the whole window:
.slot { container-type: inline-size; }
@container (min-width: 30rem) {
.card { display: flex; }
} Current versions of the major browsers support size container queries. If your audience uses old devices, check support and make sure the unqueried style works on its own.
Make images and media fit
At minimum, stop images from overflowing:
img, video { max-width: 100%; height: auto; } Include width and height attributes on images so the browser can reserve space and avoid layout jumps. Then avoid sending a huge file to a small screen. srcset offers several sizes, and sizes tells the browser how wide the image will be shown:
<img src="/img/lake-800.jpg"
srcset="/img/lake-400.jpg 400w, /img/lake-800.jpg 800w, /img/lake-1600.jpg 1600w"
sizes="(min-width: 48rem) 40rem, 100vw"
width="800" height="533" alt="A lake at sunrise"> The browser picks a file using the width needed and the screen density. Use the picture element when you want a different crop on small screens or want to offer newer formats with a fallback. Image formats for speed compares them, and a bulk image resizer can prepare the set. For embedded video, the aspect-ratio property keeps the box in shape.
Handle the awkward content
Some things don't shrink gracefully:
- Tables: wrap them in a horizontally scrollable, focusable region, or restructure into stacked entries for simple data.
- Long strings and URLs: use
overflow-wrap: anywherewhere needed. - Navigation: a toggle button with
aria-expandedcan reveal links on small screens. - Code blocks: let them scroll inside their own box.
- Fixed headers and banners: they eat a lot of a short landscape screen.
Full-height layouts can misbehave because mobile browser toolbars appear and disappear. Newer viewport units such as dvh account for this in supporting browsers.
Choose a layout pattern
Most responsive pages combine a handful of arrangements. Naming them helps you decide what should happen as space shrinks.
| Pattern | Wide screen | Narrow screen |
|---|---|---|
| Wrapping grid | Several equal cards per row | One card per row |
| Sidebar | Navigation or filters beside content | Sidebar below content or behind a button |
| Split | Image and text side by side | Image stacked above or below text |
| Priority navigation | All links visible | Top links visible, the rest under "More" |
| Centered column | Wide margins around readable text | Small margins, full width |
Decide what stacks first. In a split layout that alternates image left and image right, naive stacking can leave two images touching and their text out of sync. Keeping each image and its text inside one container avoids that.
Resist hiding content at small sizes. If something isn't worth showing on a phone, ask whether it's worth showing at all. Where a section really is secondary, a disclosure element such as details and summary keeps it available without taking space.
Spacing should scale too. Padding that looks generous on a monitor can consume a third of a phone screen. Define a few spacing variables and change them at your breakpoints, or use clamp() so they follow the width.
Retrofit an existing site
You may not be starting from zero. To adapt a fixed-width site without rewriting everything at once:
- Add the viewport meta tag, then expect things to look worse until the next steps are done.
- Replace fixed widths on the main containers with
max-widthand percentage or flexible tracks. - Make images and embeds scale.
- Turn floated or table-based columns into flexbox or grid, stacked by default.
- Rework navigation so it fits and can be operated by touch and keyboard.
- Fix data tables, forms and third-party widgets one at a time.
- Remove old overrides once the new rules work.
Begin with the templates that get the most visits. Keep a list of pages that use unusual layouts, such as landing pages built by hand, since these are where fixed widths hide. A staging site lets you try changes without exposing half-finished pages to visitors.
Older separate mobile sites on their own subdomain are worth retiring in favor of one responsive version. When you do, redirect each old address to its matching page with permanent redirects, as covered in how to redirect pages without losing visitors.
Test the result
Browser developer tools simulate screen sizes, which is quick but imperfect. Also use at least one real phone, try landscape, raise the system text size and zoom the desktop browser to 400 percent. At that zoom, ordinary content should reflow into a single column without needing to scroll in two directions.
Check focus order whenever CSS rearranges blocks, as described in keyboard navigation and focus.
Responsive is not the same as fast
A page can fit a phone perfectly and still load slowly. Hiding an element with CSS doesn't necessarily stop it downloading. Watch script weight, image sizes and fonts, and measure using the ideas in Core Web Vitals explained.
If you're planning a new design, start with the narrow layout, as mobile-first design in practice explains. If you use a theme or website builder, it is probably responsive already, but your tables, embeds and oversized images can still break it, so test your own content.
Common questions
Do I need a separate app or mobile site?
For most content and shop sites, a responsive site is enough and is far less work to maintain. An app makes sense when people use you often and need device features the web handles poorly for your case.
How many breakpoints should I have?
As few as the content needs. Many layouts manage with two or three, plus a handful of small adjustments inside individual components.