Responsive web design was defined by Ethan Marcotte in his 2010 A List Apart article as three ingredients: fluid grids, flexible images and media queries. This guide walks through those three, the viewport setup, and the tools every major browser supports as of 2026 — range-syntax media queries, container queries, srcset and sizes, and clamp() — with example code for each. The order of writing styles from the smallest screen up, and common mobile UI patterns, are covered separately in the mobile-first CSS guide.
1. The three ingredients of responsive design
Responsive Web Design (RWD) means one set of HTML and CSS that rearranges itself to fit the screen width. Marcotte's three ingredients still hold up, and most CSS features that came later are simply easier ways to implement them.
- Fluid grids: column widths are set as proportions (%,
fr) instead of fixed px, so they grow and shrink with the screen. Today CSS Grid and Flexbox do this job. - Flexible images: images never grow wider than their parent, and the browser downloads a file sized for the screen (
srcset,sizes,<picture>). - Media queries: styles change based on conditions such as screen width, input method or user preferences.
Add container queries, which respond to the width of the area a component sits in, and clamp(), which scales font sizes smoothly without any media queries, and you have the 2026 responsive toolkit.
2. The viewport meta tag
Without a viewport meta tag, mobile browsers render the page at roughly 980px wide and then shrink it to fit the screen. In that state even a 360px phone reports a 980px viewport, so your media queries don't fire as intended. Every responsive page needs this line in its <head> first:
<meta name="viewport" content="width=device-width, initial-scale=1">
3. Media query syntax
A media query applies the CSS inside it only when its condition matches. Media queries don't add specificity to selectors, so when specificity is equal, the rule written later wins.
3.1 Basic form
A media type and conditions are joined with and; if you omit the media type it defaults to all. The example below enlarges text only when the screen is at least 768px wide.
@media [media type] and (condition) {
/* CSS rules */
}
/* Example: applies only at 768px and up */
@media (width >= 768px) {
.element {
font-size: 1.125rem;
}
}
3.2 Media types
- all: every device (the default when omitted)
- screen: computer, tablet and phone screens
- print: printing and print preview
- speech: in the spec, but not supported by major browsers or screen readers. Screen readers read the screen styles as-is, so in practice all, screen and print are all you need.
3.3 Range syntax
The old max-width: 768px and min-width: 768px both include 768px, so the two rules overlap at exactly 768px. Switching to 767px to avoid that leaves a gap: under zoom or on high-density screens, a fractional width like 767.5px belongs to neither rule. Range syntax uses comparison operators to express boundaries precisely, and every major browser has supported it since 2023.
/* Old style: both rules match at 768px */
@media (max-width: 768px) { }
@media (min-width: 768px) { }
/* Range syntax: clear boundaries */
@media (width < 768px) { } /* below 768px */
@media (width >= 768px) { } /* 768px and up */
@media (768px <= width < 1024px) { } /* 768px up to, but not including, 1024px */
@media (height < 600px) { } /* short screens (e.g. phones in landscape) */
3.4 Input method: hover and pointer
Screen width alone can't tell you whether someone is using a mouse or a finger. A wide tablet is still a touch device, and on touch devices :hover styles often stick after a single tap. The hover and pointer features let you split styles by input method.
/* Only where there's a precise pointer that can hover, like a mouse */
@media (hover: hover) and (pointer: fine) {
.btn:hover {
background: #1565c0;
}
}
/* Imprecise pointers like a finger: enlarge the touch target */
@media (pointer: coarse) {
.btn {
min-height: 44px;
}
}
3.5 User preferences and display features
You can also respond to settings the user chose in their operating system. prefers-reduced-motion in particular is used to tone down animation for people who get dizzy from motion, so any site with transition effects should include it.
/* Dark mode */
@media (prefers-color-scheme: dark) {
:root {
--bg: #121212;
--text: #e8e8e8;
}
}
/* Reduced motion */
@media (prefers-reduced-motion: reduce) {
*, *::before, *::after {
animation-duration: 0.01ms !important;
transition-duration: 0.01ms !important;
}
}
/* Orientation */
@media (orientation: landscape) { }
/* High-density displays */
@media (resolution >= 2dppx) { }
4. Choosing breakpoints
Base breakpoints on where your content starts to look awkward, not on a list of specific devices. Slowly widen the browser window, find the width where lines of text get too long or the gaps between cards get too big, and put a breakpoint near there. If you need a starting point, the easiest setup to maintain is base styles with no media query for the narrowest screen, adding rules only as the width grows:
/* Base: narrowest screen (no media query) */
.page { padding: 1rem; }
@media (width >= 768px) { /* tablet and up */ }
@media (width >= 1024px) { /* desktop and up */ }
@media (width >= 1440px) { /* large desktop */ }
An em in a media query is calculated from the user's default browser font size, so with (width >= 48em), a user who enlarges their default font also gets the layout for the next narrower screen. At the default setting (16px), 48em equals 768px. The reasoning behind building up from small screens, and how to do it, is explained in detail in the mobile-first CSS guide.
5. Layouts without media queries
Layouts that say "several per row if there's room, otherwise one at a time" — like a list of cards — can be built without media queries. You don't have to pick breakpoints one by one, and the layout adapts on its own even when dropped into a narrow area like a sidebar.
5.1 Grid auto-fit and minmax()
This creates as many columns as can each get at least 300px and distributes the leftover space evenly. Wrapping the minimum in min(300px, 100%) prevents horizontal scrolling when the container is narrower than 300px (a 320px screen with 1rem padding on each side leaves 288px).
.card-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(300px, 100%), 1fr));
gap: 1rem;
}
5.2 Flexbox flex-basis
In Flexbox, flex-basis plays the same role. When there isn't room for two items side by side (300px × 2 + a 16px gap = about 616px), items automatically wrap to one per row, so no media query is needed. Try it yourself in the Flexbox visualizer.
.flex-list {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
.flex-item {
flex: 1 1 300px; /* starts at 300px, grows and shrinks with the space */
min-width: 0; /* stops long words or code from pushing the item wider */
}
5.3 When you need a fixed column count
If the design spells out the column count — say, two columns on tablet and three on desktop — state it explicitly with media queries. The default is one column, so you only need to add rules for wider screens. You can preview the grid structure in the CSS Grid generator.
.card-grid-fixed {
display: grid;
gap: 1rem;
}
@media (width >= 768px) {
.card-grid-fixed { grid-template-columns: repeat(2, 1fr); }
}
@media (width >= 1024px) {
.card-grid-fixed { grid-template-columns: repeat(3, 1fr); }
}
6. Container queries
Media queries are based on the width of the whole screen. So if the same card is used in both a wide main column and a narrow sidebar, they can't handle the case where the screen is wide but the card's slot is narrow. Container queries change styles based on a parent element's width instead, and every major browser has supported them since 2023.
In the example below, .card-list is declared as a container, so the .card elements inside it lay out the image and text side by side only when the list area is at least 480px wide. Note that a container query can only restyle the container's descendants, not the container itself.
.card-list {
container-type: inline-size;
}
.card {
display: grid;
gap: 0.75rem;
}
@container (width >= 480px) {
.card {
grid-template-columns: 160px 1fr;
align-items: start;
}
}
7. Responsive images
7.1 Base CSS and size attributes
This is the basic rule that keeps images from overflowing their parent. Also add width and height attributes to the HTML <img> to tell the browser the original aspect ratio — it can then reserve space before the image arrives, preventing layout shift while loading (CLS).
img {
max-width: 100%;
height: auto;
}
7.2 srcset and sizes
When you serve the same image at different sizes, list each file's actual width (w) in srcset, and in sizes describe how wide the image will be displayed at each screen width. The browser combines this with the device's pixel density and downloads only the single best-fitting file. The sizes below matches the three/two/one-column grid from 5.3, and uses the min-width notation inside the HTML attribute for the widest compatibility.
<img
src="photo-800.jpg"
srcset="photo-480.jpg 480w, photo-800.jpg 800w, photo-1200.jpg 1200w, photo-1600.jpg 1600w"
sizes="(min-width: 1024px) 33vw, (min-width: 768px) 50vw, 100vw"
width="1600" height="900"
alt="A bridge over a river at sunset"
loading="lazy" decoding="async">
7.3 The picture element
Use <picture> when you want to show a differently composed image depending on the screen (art direction), or give a different file only to browsers that support a newer format like AVIF. If it's the same image at different sizes, the srcset from 7.2 is the better fit. In alt, describe what the image actually shows rather than writing something like "responsive image".
<picture>
<source media="(min-width: 768px)" srcset="hero-wide.avif" type="image/avif">
<source media="(min-width: 768px)" srcset="hero-wide.jpg">
<source srcset="hero-square.avif" type="image/avif">
<img src="hero-square.jpg" width="800" height="800" alt="A latte and a laptop by a café window">
</picture>
7.4 Background images
Swap decorative background images with media queries in CSS. Only the image from the matching rule is downloaded, so putting the smallest file in the base style and switching to larger files on wider screens wastes the least data. If the image conveys content, use an <img> with alt instead of a background.
.banner {
background-image: url("banner-small.jpg");
background-size: cover;
background-position: center;
}
@media (width >= 768px) {
.banner { background-image: url("banner-medium.jpg"); }
}
@media (width >= 1024px) {
.banner { background-image: url("banner-large.jpg"); }
}
8. Fluid typography
8.1 Setting font sizes with clamp()
clamp(minimum, preferred, maximum) scales the font size smoothly with the screen width while never going below the minimum or above the maximum. Include rem in the preferred value so text still grows when the user zooms or changes their browser font size; with vw alone, zooming leaves the size unchanged. The values below are calculated to hit the minimum on a 360px screen and the maximum on a 1440px screen.
/* Don't pin the root in px, e.g. html { font-size: 16px; }.
It overrides the user's browser font-size setting. */
h1 { font-size: clamp(1.5rem, 1.167rem + 1.481vw, 2.5rem); } /* 24px@360 → 40px@1440 */
h2 { font-size: clamp(1.25rem, 1.083rem + 0.741vw, 1.75rem); } /* 20px@360 → 28px@1440 */
p { font-size: clamp(1rem, 0.917rem + 0.37vw, 1.25rem); } /* 16px@360 → 20px@1440 */
Enter the minimum and maximum screen widths and font sizes and the clamp() generator does this math for you; check px-to-rem conversions with the PX to REM converter. The accessibility problems of sizing text with vw alone are covered in depth in VW units and web accessibility.
8.2 Line length and line height
When body text stretches all the way across a wide screen, readers struggle to find the start of the next line. Give the text area a maximum width, and set line-height as a unitless number so the ratio holds when the font size changes. To carry line-height and letter-spacing values over from a design file, use the line height calculator and the letter spacing converter.
.content {
max-inline-size: 40em; /* keeps lines from getting too long */
margin-inline: auto;
padding-inline: 1rem;
line-height: 1.7; /* unitless: proportional to font size */
}
9. How to test
9.1 Developer tools
- Chrome DevTools device mode: try different screen widths, and save the widths you check often (360, 390, 412px, etc.) as custom devices.
- Firefox Responsive Design Mode: resizes the viewport and also simulates touch events.
- Safari Responsive Design Mode and Web Inspector: change the screen size from the Develop menu in macOS Safari, and remotely debug a real iPhone connected over USB with Web Inspector (the Xcode Simulator works too).
- Real devices: the address bar collapsing and expanding, portrait/landscape rotation and touch accuracy can only be checked on real hardware.
9.2 Zoom and font-size tests
A responsive layout must hold up not just at different screen widths but also when the user has enlarged the text. Check these two things:
- Browser zoom at 200% (Ctrl or Cmd +
+): does the text actually get close to twice as large, and does the layout reflow into a single column without horizontal scrolling? - Browser default font size set to "Very large" (Chrome Settings › Appearance › Font size): does text sized in rem grow, and is text inside buttons left uncut?
A heading sized only in vw, like the one below, doesn't change size in either test. Zooming to 200% on desktop halves the CSS viewport width, which halves the 5vw value too — that cancels out the 2× zoom, so the size on screen stays the same. A clamp() that mixes in rem grows by the rem portion.
/* Doesn't respond to zoom or font settings */
.title-bad { font-size: 5vw; }
/* The rem portion follows zoom and font settings (24px@360 → 40px@1440) */
.title-good { font-size: clamp(1.5rem, 1.167rem + 1.481vw, 2.5rem); }
Cases where you're better off not using vw are collected in 6 cases where you should NOT convert px to vw.
10. Using the PX to VW converter
Choosing a unit starts with one question: should this value scale with the screen width? Font sizes and padding inside components get rem, layout values that should stretch with the screen get vw, and values that need to transition smoothly between the two get clamp(). For vw values, enter your design's base width in the PX to VW converter, type in the px value, and the result appears instantly.
11. Summary
Responsive sites are built on three ingredients — fluid grids, flexible images and media queries — plus newer tools like container queries and clamp(). To see the order in which to use these tools to build a real mobile screen, continue with the mobile-first CSS guide.