Responsive CSS in 2026: Combining @media, @container, clamp(), and srcset Without Extra BreakpointsLongread · digest for November 30
Modular Bauhaus-style vector composition showing UI blocks adapting to their parent container width
Longread7 sections≈ 25 min

Responsive CSS in 2026: Combining @media, @container, clamp(), and srcset Without Extra Breakpoints

Picture a standard news feed on a 1200-pixel monitor. The top card stretches across the full screen: image on the left, large headline and summary on the right. One row below sit three more identical cards—using the exact same .card class—placed in narrow columns, so each stacks vertically and scales its headline down on its own. No .card--wide modifiers, no JavaScript, and no dozen breakpoints for specific phone models.

This works in pure CSS across every modern browser once you stop dumping all responsiveness into one bucket and split responsibilities cleanly. Page-wide grids, print styles, and system preferences like dark mode go to @media. Component layout inside its own slot goes to @container. Smooth spacing and font sizes belong to clamp(), while srcset and <picture> choose the right image for the screen and pixel density.

The viewport Meta Tag and Mobile-First: Why Phones Show a Shrunk Desktop

Almost everyone who has built a first responsive page has hit the same surprise: in a narrow laptop window, everything reflows cleanly, but on a phone it opens as a tiny desktop site with microscopic text. Nine times out of ten, the reason is a missing viewport meta tag in <head>.

<!-- Tell the browser: viewport width matches the screen width -->
<meta name="viewport" content="width=device-width, initial-scale=1" />

Without that line, a mobile browser assumes it's looking at an old non-mobile site. It lays out the page in a wide virtual window and scales the whole picture down to fit the phone screen. Narrow-width media queries never fire because the browser thinks it has plenty of room.

You can verify this in thirty seconds. Open Chrome DevTools, enable Toggle device toolbar, set the width to 390 and device type to Mobile (if the type dropdown is hidden, add it via More options -> Add device type). Reload the page and run window.innerWidth in the console. Without the meta tag, Chromium reports 980; with it, you get 390.

Here it helps to separate physical display pixels from CSS pixels. A modern phone often has a devicePixelRatio (DPR) of 2 or 3, meaning multiple hardware dots map to a single CSS pixel. Setting width=device-width ties the viewport to the screen width in CSS pixels, while initial-scale=1 sets a 1:1 starting zoom. The vw unit is measured from this same width: 1vw equals one percent of the viewport.

Once the viewport foundation is in place, rule order matters. A mobile-first approach starts with a single-column base layout for narrow screens. As width grows, media queries add second and third columns. The cascade stays shorter and cleaner: you don't build a complex desktop grid only to reset margins and grid-column back down for phones.

Modern @media: Range Syntax Instead of min-width and System Preferences

The old min-width and max-width syntax forces you to read twice just to see which way a boundary points. In modern media query syntax, width conditions look like ordinary math inequalities and read cleanly at a glance.

Bauhaus-style poster graphic with a coordinate ruler of screen-width media query ranges

/* Old syntax: both boundaries included */
@media (min-width: 30em) and (max-width: 50em) {
  .grid { gap: 1rem; }
}

/* Modern range: exact same condition */
@media (30em <= width <= 50em) {
  .grid { gap: 1rem; }
}

/* Strict inequalities: rule does not apply at exactly 30em or 50em */
@media (30em < width < 50em) {
  .grid { gap: 1rem; }
}

/* Logical OR using the or operator */
@media (width < 30em) or (orientation: portrait) {
  .grid { gap: 1rem; }
}

The difference between <= and < seems trivial until a layout breaks at one specific resolution. At a 16px browser default font size, 30em equals 480px and 50em equals 800px. The table below shows how both variants behave at key widths.

Window Width30em <= width <= 50emmin-width: 30em & max-width: 50em30em < width < 50em
400pxnonono
480pxyesyesno
640pxyesyesyes
800pxyesyesno
900pxnonono

Alongside or (or a comma), and and not work as expected, and boolean features need no value: @media (color) matches any color display. Only screen, print, and all remain relevant among media types, while legacy types like handheld or tv are long gone. The only keyword in @media only screen does nothing in modern browsers either, so you can drop it.

Another common question is where to place breakpoints. Tying them to popular phone dimensions is pointless: models change every year, and desktop users often snap browsers to half-screen. Put breakpoints where the content itself starts feeling cramped or overly stretched. Writing them in rem or em is best, though there is a subtle catch.

In regular CSS properties, rem is relative to the html element's font size. Inside @media, rem and em always use the browser's base font setting, even if you override font-size on html. If a user keeps the default 16px, 40rem triggers at 640px and 64rem at 1024px. If someone bumps their browser's base font size to 20px, the 40rem threshold automatically shifts to 800px, keeping columns from collapsing into cramped large text.

/* Base mobile grid: single column */
.grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
}

/* Two columns once the window has room (640px at 16px font size) */
@media (width >= 40rem) {
  .grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

This mobile-first setup keeps grids predictable at any zoom or font scale. A typical card feed only needs two thresholds: one column below 40rem, two columns up to 64rem, and three columns from 64rem upward.

@media is also how you check user system preferences, which container queries can't see because they apply to the whole device.

/* Match native browser controls to the OS dark theme */
@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
  }
}

/* Turn off animations if reduced motion is enabled in the OS */
@media (prefers-reduced-motion: reduce) {
  .anim {
    animation: none;
  }
}

color-scheme: dark recolors built-in browser UI (scrollbars, checkboxes, inputs), while you set your own background and text colors in the same block. For reduced motion, the short boolean @media (prefers-reduced-motion) works too and is equivalent to (prefers-reduced-motion: reduce). Just place it below the base .anim rule, or wrap transitions inside @media (prefers-reduced-motion: no-preference).

Container Queries @container, cqi Units, and style()

Media queries handle page skeletons well, but they break down on reusable components. Take a product card or profile widget: today it sits in a 720px main column, tomorrow a designer drops it into a 320px sidebar, and the browser window is still 1200px wide. @media can't help because the window doesn't care which slot holds the block.

That's why CSS gained @container queries. Instead of window width, they inspect the size of the nearest ancestor declared as a container.

/* Declare the wrapper as a container: name card, type inline-size */
.card-container {
  container: card / inline-size;
}

/* Change child styles when the wrapper is wider than 400px */
@container card (width > 400px) {
  .card h2 {
    font-size: 1.5em;
  }
}

container: card / inline-size is shorthand for container-name: card and container-type: inline-size. Picking the right container-type solves half of component layout issues, and there are three options:

  1. inline-size tracks size along the inline axis—width in horizontal writing modes. Height still comes from the content, making this the go-to default for cards, widgets, and grid items.
  2. size tracks both width and height, with a strict tradeoff: content no longer sizes the container. Unless you set an explicit height or stretch it from a parent layout, it collapses to zero.
  3. normal is the default for all elements. It doesn't track dimensions, but it supports style() queries and name-only queries.

When you need to check both axes or match just a wrapper's name without measuring width, the syntax stays concise.

/* Query width and height: ancestor needs container-type: size */
@container (width > 400px) and (height > 400px) {
  h2 {
    padding: 1rem;
  }
}

/* Name-only query: container-name: sidebar is enough */
@container sidebar {
  h2 {
    border-bottom: 2px solid;
  }
}

Alongside @container came container length units. 1cqw is one percent of container width, 1cqh is one percent of its height, 1cqi is one percent of its inline axis (width in horizontal text), and 1cqb is one percent of its block axis, plus cqmin / cqmax. If an element has no matching container ancestor on that axis, the browser doesn't error out—it quietly falls back to small viewport units (svw and svh, the phone screen size with browser toolbars expanded). Testing on a 1000px window shows the difference clearly: width: 50cqw inside an 800px container evaluates to 400px, while without a container it becomes 500px, half the window.

Another branch of container queries is style(). It checks the value of a CSS custom property (--*) on an ancestor, letting you toggle component themes or states without extra classes.

/* Set the theme via a CSS variable on the wrapper */
.theme {
  --theme: dark;
}

/* Works even with default container-type: normal */
@container style(--theme: dark) {
  .card {
    color: #fff;
    background: #222;
  }
}

The boundary here is clear: in stable engines as of late 2026, style() works with --name custom properties. Querying standard CSS properties like style(display: flex) isn't supported yet, and numeric ranges inside style() remain limited. For CSS variables, though, you don't even need container-type: inline-size—it works out of the box.

The easiest way to see a live card reflow is to try it. Drag the slider below and watch .card-wrap cross 28rem (448px at a 16px base font): the card switches to two columns with zero media queries, and its heading scales smoothly from 5cqi.

Live Demo: Card Reflow via @container and clamp(cqi)

Watch the status line under the slider: below 360px, the heading hits its 18px floor, then grows smoothly with the container until it caps at 28px at 560px. The switch between vertical and horizontal layout happens right at 448px, no matter how wide the monitor is.

Fluid Math with clamp(), min(), max(), and Separation of Concerns

Even with @media and @container, developers often add extra breakpoints just to nudge font sizes on tablets or tighten padding on phones. clamp(), min(), and max() eliminate that boilerplate in a single line across all modern browsers.

/* Minimum 16px, fluid scaling with viewport, maximum 32px */
h2 {
  font-size: clamp(1rem, 2.5vw, 2rem);
}

/* At most 600px, but shrinks to 100% in a narrow parent */
.box-limited {
  width: min(100%, 600px);
}

/* At least 300px, or half the parent on wide screens */
.box-floor {
  width: max(50%, 300px);
}

clamp() arguments always follow the order "minimum, preferred, maximum". With a 16px root font, the heading stays clamped between 16px and 32px: at 480px window width it remains 16px (since 2.5vw is 12px, below the minimum), grows to 20px at 800px, and hits 22.5px at 900px. min() and max() are even simpler: the first picks the smaller value, the second picks the larger. Inside a 1000px block, min(100%, 600px) gives 600px, and max(50%, 300px) gives 500px.

Put all these tools together, and you get a clean separation of duties where each layer handles its own job.

Layout TaskRight CSS Tool
Page grid columns, header, sidebar menu@media on window width
Card, widget, block placed in varying columns@container on wrapper width
Font sizes and spacing inside a componentclamp() with cqi units
Print, dark theme, reduced motion, pointer type@media (print, prefers-*, hover, pointer)

Size-based container queries have been in Baseline since February 2023, so they've worked across all major engines for years. If you still need to support an ancient corporate browser, just keep the vertical layout as your base rule outside @container—the card stays tidy and simply skips expanding to two columns.

Responsive Graphics: srcset + sizes vs.

A fluid grid won't save you if a phone on mobile data downloads a 1600px, 1 MB hero banner. HTML offers two distinct responsive image tools that shouldn't be mixed up: srcset with sizes picks the right resolution of the same image for the screen and DPR, while <picture> swaps the image itself or its crop.

<!-- w descriptor: declare the actual width of each file -->
<img
  srcset="
    /img/hero-480.jpg 480w,
    /img/hero-800.jpg 800w
  "
  sizes="(width <= 800px) 100vw, 800px"
  src="/img/hero-800.jpg"
  style="display: block; width: 100%; max-width: 800px; height: auto;"
  alt="Article illustration"
/>

Why write sizes if CSS already sets the image width? It comes down to how browsers load pages. The preload scanner reads HTML and starts downloading images before CSS finishes loading and computing layout. Without sizes, the browser has no idea whether an image fills the screen or a tiny thumbnail. Here we tell it explicitly: up to 800px, the image spans 100vw; above that, it caps at 800px.

Mixed-media paper collage comparing srcset scaling with picture element cropping

When an image has a fixed CSS pixel size (like a 320x180 thumbnail), sizes isn't needed—use x density descriptors instead.

<!-- x descriptor: pick based on device pixel ratio (DPR) -->
<img
  srcset="
    /img/thumb-320.jpg 1x,
    /img/thumb-480.jpg 1.5x,
    /img/thumb-640.jpg 2x
  "
  src="/img/thumb-320.jpg"
  width="320"
  height="180"
  alt="Card thumbnail"
/>

At 1x density, the browser fetches the 320px file; on a 3 DPR screen, Chromium picks the sharpest available option, 640px.

Art direction is a different problem: when a wide landscape photo shrinks into a thin strip on a phone where the subject disappears. srcset can't help there—you need <picture> with <source media="...">.

<!-- Serve a portrait crop on narrow screens, landscape above 800px -->
<picture>
  <source
    media="(width < 800px)"
    srcset="/img/hero-vertical-480x640.jpg"
  />
  <img
    src="/img/hero-wide-800x450.jpg"
    style="display: block; max-width: 100%; height: auto;"
    alt="Article illustration"
  />
</picture>

You can check which file the browser actually picked in the console via img.currentSrc. At 1x density with zero body margin, the difference between the two approaches is immediate.

Window Widthsrcset + sizes<picture> (Art Direction)
400px480x270480x640 (portrait)
800px800x450800x450 (landscape)

One practical srcset quirk: if you open a page on a wide screen and then shrink the DevTools window, the browser might skip downloading the smaller file. It already has a sharper version in cache, and the HTML spec allows keeping it to save a network request. For an honest test, enable Disable cache in the Network tab and reload.

Building a Working Template and Checking Browser Support

Now let's combine every layer into one working card.html file. The page grid uses @media to step from one column to two and three, the first .wide card always spans the full grid, and each .card-wrap hosts a self-contained @container card.

<!doctype html>
<html lang="ru">
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <style>
    :root {
      color-scheme: light dark;
      --gap: clamp(0.75rem, 2vw, 1.5rem);
    }
    body { margin: 0; font-family: system-ui, sans-serif; }

    /* Page grid: mobile-first with window-width breakpoints */
    .grid {
      display: grid;
      grid-template-columns: minmax(0, 1fr);
      gap: var(--gap);
      padding: var(--gap);
    }
    @media (width >= 40rem) {
      .grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    }
    @media (width >= 64rem) {
      .grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
    }
    .wide { grid-column: 1 / -1; }

    /* Card: adapts to the width of its own grid cell */
    .card-wrap { container: card / inline-size; }
    .card {
      display: grid;
      gap: 0.75rem;
      padding: 1rem;
      border: 1px solid #8884;
      border-radius: 0.5rem;
      overflow-wrap: anywhere;
    }
    .card img {
      width: 100%;
      height: auto;
      aspect-ratio: 16 / 9;
      object-fit: cover;
    }
    .card h2 {
      margin: 0;
      font-size: clamp(1.125rem, 5cqi, 1.75rem);
    }
    @container card (width >= 28rem) {
      .card {
        grid-template-columns: minmax(0, 2fr) minmax(0, 3fr);
        align-items: start;
      }
    }
    @media (prefers-reduced-motion: no-preference) {
      .card { transition: transform 0.2s; }
      .card:hover { transform: translateY(-2px); }
    }
  </style>
</head>
<body>
  <main class="grid">
    <div class="card-wrap wide">
      <article class="card">
        <img src="/img/hero-800.jpg" alt="" />
        <div>
          <h2>Главная новость</h2>
          <p>Карточка на всю ширину сетки.</p>
        </div>
      </article>
    </div>
    <div class="card-wrap">
      <article class="card">
        <img src="/img/hero-800.jpg" alt="" />
        <div><h2>Карточка 2</h2></div>
      </article>
    </div>
    <div class="card-wrap">
      <article class="card">
        <img src="/img/hero-800.jpg" alt="" />
        <div><h2>Карточка 3</h2></div>
      </article>
    </div>
  </main>
</body>
</html>

Three small details in this template prevent half of future layout bugs. First, minmax(0, 1fr) instead of bare 1fr: plain 1fr won't let a column shrink below its longest unbreakable word, whereas minmax(0, 1fr) plus overflow-wrap: anywhere keeps long URLs from pushing the grid off-screen. Second, grid-column: 1 / -1 stretches the first card from the first grid line to the last regardless of column count. Third, aspect-ratio: 16 / 9 paired with object-fit: cover keeps image proportions clean without squishing.

Here is how this layout renders across three test widths at a 16px base font (column widths rounded to whole pixels).

WindowGrid ColumnsWide CardWide HeadingRegular Heading
375px351pxsingle column18px18px
700px329px + 329px250px + 376px28px18px
1200px368px x 3442px + 664px28px18.4px

On the wide screen, cqi shines: a regular card's container is 368px wide, and 5% of that gives 18.4px—slightly larger yet compact. Meanwhile, the wide card at the top hits the clamp() ceiling, and both variants live on the same page without a single extra class.

What about browser support? By late November 2026, every key piece of this stack—including style() on custom properties and name-only @container queries—has reached stable Chrome, Firefox, and Safari. Minimum versions according to mdn/browser-compat-data look like this.

CSS FeatureChromeFirefoxSafariStatus
Range syntax for width10410216.4stable
or operator in media queries1046416.4stable
prefers-reduced-motion746310.1stable
prefers-color-scheme766712.1stable
container-type, size @container10511016stable (Baseline since Feb 2023)
style() on custom properties11115118stable
Name-only @container14814926.4stable

Support for style() and name-only queries is recent (2026), so if you maintain older browsers, keep a sensible base style outside those rules as a fallback. Range @media, system prefers-*, and size-based @container queries are already battle-tested.

Seven Common Pitfalls and Automated Checks Without a Shelf of Phones

CSS doesn't throw console errors when a rule has a typo or a condition misses—the browser just silently skips the block. That's why the most frustrating responsive bugs look like "I wrote everything right and nothing changed." Here are the seven most frequent traps.

First: forgetting <meta name="viewport">. If a phone shows a shrunken desktop site, don't rewrite your media queries yet. Check <head>: without width=device-width, initial-scale=1, the browser lays out the page in virtual 980px.

Second: using strict > instead of >= when converting min-width. Rewriting @media (min-width: 400px) as @media (width > 400px) drops the styles at exactly 400px. Always use >= and <= when converting min-width and max-width.

Third: container-type: size without an explicit height. Writing size instead of inline-size out of habit makes a text card collapse to zero height. Use inline-size for blocks whose height depends on content, and save size for fixed-height containers.

Fourth: cqw and cqi suddenly measuring against the window. If a 5cqi heading blows up huge, inspect the ancestor tree in DevTools: likely no parent defines container-type on that axis, so the browser fell back to svw.

Fifth: trying to style the container itself inside @container. A query always checks an ancestor and styles its descendants. Putting container: card / inline-size on .card and changing .card's grid-template-columns inside @container does nothing. The container always belongs on the outer .card-wrap.

Sixth: blocking zoom with user-scalable=no or maximum-scale=1. That old workaround for accidental input zoom hurts people with low vision who enlarge small text. Besides, iOS Safari has ignored it for pinch-zoom since iOS 10.

Seventh: keeping only screen and obsolete media types. The only prefix was a hack for early-2000s browsers, and types like projection or tv don't work. Modern conditions use media features: width, orientation, hover, pointer, and prefers-*.

You don't need a drawer full of test phones to catch these mistakes. In DevTools, checking card.html at three widths (375px, 700px, 1200px) is enough to verify one column on mobile and two or three columns with a wide top card on larger screens.

To avoid clicking through DevTools before every release, automate the check in Playwright. You can set viewport size, pixel density, color scheme, and reduced motion right in the browser context.

# Example Playwright context emulating a mobile screen and dark mode
context = browser.new_context(
    viewport={"width": 390, "height": 844},
    device_scale_factor=3,
    color_scheme="dark",
    reduced_motion="reduce",
    # Enables mobile meta viewport handling in Chromium and WebKit
    is_mobile=True,
)
page = context.new_page()

Better yet, you can toggle theme and motion mid-test without recreating the context by calling page.emulate_media(color_scheme="light", reduced_motion="no-preference") (or page.emulateMedia(...) in JS/TS). That's how every example in this guide was verified across three engines—Chromium 159, Firefox 159, and WebKit 27.2 via Playwright for Python 1.65.2.

Quick Pre-Release Responsive Layout Checklist

  • <head> includes <meta name="viewport" content="width=device-width, initial-scale=1" /> without user-scalable=no.
  • Range media queries converted from min-width / max-width use >= and <= so boundary pixels aren't missed.
  • Component wrappers set container-type: inline-size, and @container rules target descendants rather than the wrapper itself.
  • CSS Grid columns use minmax(0, 1fr) with overflow-wrap: anywhere on text blocks.
  • Images serve appropriate sizes via srcset + sizes (or <picture> for art direction), and currentSrc is verified with cache disabled.
  • Responses to prefers-color-scheme: dark and prefers-reduced-motion: reduce are tested in DevTools or Playwright.
Want a deep diveVotes help us choose what to break down in detail. This material has 0 votes.
Sources · 11