2HAASStart a project
All posts

Tech Tips

CSS container queries: components that fit their space

Media queries ask how wide the screen is. Container queries ask how wide the component's own box is, so one card works in a sidebar, a grid or a full-width row.

The 2HAAS team2 min read

A product card sits in the main column and looks right: image on the left, text on the right. Then someone places the same card in a narrow sidebar. The screen is still wide, so the media query still applies the wide layout, and the card is crushed.

The card should not care how wide the screen is. It should care how much room it has. That is what container queries provide.

The two steps

First, mark an element as a container. Second, write rules that depend on that container's width.

.card-wrap {
  container-type: inline-size;
  container-name: card;
}

.card {
  display: grid;
  gap: 1rem;
}

@container card (min-width: 28rem) {
  .card {
    grid-template-columns: 10rem 1fr;
    align-items: center;
  }
}
<div class="card-wrap">
  <article class="card">
    <img src="/img/mug.webp" alt="Blue ceramic mug" width="400" height="400" />
    <div>
      <h3>Ceramic mug</h3>
      <p>৳ 450</p>
    </div>
  </article>
</div>

When the wrapper is at least 28rem wide, the card lays out side by side. When it is narrower, the card stacks. Put it in the main column, a sidebar or a three column grid, and it picks the right layout each time.

Details that save time

  • A container cannot query itself. The element with container-type is the one being measured. The rules apply to its descendants. That is why the example has a wrapper.
  • inline-size is the usual choice. It watches the width only. The value size also watches the height and requires the container to have a defined height.
  • Naming is optional but wise. Without a name, a rule matches the nearest container. With nested containers, names remove the guesswork.
  • There is a shorthand: container: card / inline-size;.

Container units

Inside a container, cqi is one percent of its inline size. It makes type and spacing scale with the component.

.card h3 {
  font-size: clamp(1.125rem, 4cqi, 1.75rem);
}

Media queries still have a job

Use Tool
Page layout: columns, navigation, overall grid Media queries
A component's internal layout Container queries
User preferences such as reduced motion or dark mode Media queries

A typical page uses both. Media queries arrange the page, and container queries let each component adapt to the slot it was given.

Browser support

Size container queries work in all current major browsers and have done so since 2023. For older ones, write the narrow layout as the base style. A browser that ignores @container then shows the stacked version, which works everywhere.

Why it matters

Container queries make components portable in practice. A design system can ship a card, a table or a form that behaves correctly wherever it is dropped, without a list of special cases per page. That means less CSS to maintain and fewer layout bugs when a page is rearranged.

It is also a good example of the web platform closing a long-standing gap. See what the Interop project fixes for how features like this now reach every browser.

Keep reading

  1. Guides4 min read

    Mastering Responsive Layouts with CSS Container Queries

    Learn how to implement CSS Container Queries for truly responsive, component-based layouts. This guide covers setup, syntax, and practical examples.

  2. Industry Updates2 min read

    Web interoperability: what the Interop project fixes

    The Interop project gets Chrome, Firefox, Safari and Edge to agree on how web features behave. Here is how it works and what it changes for everyday frontend work.

  3. Tech Tips2 min read

    Client-side caching for API responses

    Refetching the same data on every visit wastes time and bandwidth. HTTP cache headers, an in-memory cache and browser storage each solve a different part of the problem.

Tell us what’s in the way.

A few lines is enough. You get a candid yes, a candid no, or a sharper question.

  • Working software in week four
  • A reply in one business day
  • Handover is the deliverable

Prefer email? [email protected]

The problem, who it is for, and what is in the way right now.

One business day to a real reply. No sales sequence, no call required.