Marcus Andrey Study
Ler em português Library

Visual design · Step 4 · 7 min read

Responsive layout with CSS grid and container queries

Responsive layout has three layers: media queries that respond to viewport width, container queries that respond to component width, and intrinsic layouts that respond to available space using auto-fill and auto-fit. How each one works and when to use it.

Responsive layout is not only media queries. Container queries change the component, not the viewport.

What you'll be able to do after reading:

  • Explain the difference between a media query and a container query
  • Use container queries to make a component respond to its own width
  • Write an intrinsic layout with auto-fill and auto-fit
  • Choose the right responsive layer for a given problem

1. The three layers

Responsive layout is not a single technique. It is a stack of three decisions, each working at a different scale:

Media query responds to the viewport. It is the classic approach: at a viewport width of 768px or less, change the layout. This works well for page-level structure — switching a two-column layout to a single column, hiding the sidebar, changing the header. It fails when a component appears in multiple contexts with different widths. If the same card is used in a 300px sidebar and a 900px main area, a media query cannot know which context it is in.

Container query responds to the component's container. The question it asks is: how wide is the box this component lives in? A media query asks about the viewport; a container query asks about the immediate parent. This lets the same component adapt to its context without knowing anything about the page around it. A card can be three-column when its container is 600px wide and single-column when its container is 280px wide — regardless of whether the viewport is 1400px or 400px.

Intrinsic layout responds to available space using CSS grid's auto-fill and auto-fit keywords. No breakpoints, no media queries, no container queries: the browser decides how many columns fit at any given width. This is the most flexible layer, but also the least predictable — it works best when the content (a card grid, an image gallery) can tolerate variable column counts.

2. Media queries: the viewport layer

/* Page-level layout: sidebar collapses at narrow viewport */
.page-layout {
  display: grid;
  grid-template-columns: 240px 1fr;
  gap: 16px;
}

@media (max-width: 768px) {
  .page-layout {
    grid-template-columns: 1fr;
  }
}

Media queries are the right tool for page structure. Use them to change the number of top-level columns, to hide or show navigation, to shift content order at different viewport sizes. The W3C CSS Media Queries specification (W3C, Media Queries Level 4) defines the full syntax.

3. Container queries: the component layer

Container queries require declaring a containment context on the parent and then querying it from the child. They have been supported in all major browsers since 2022–2023 (Chrome 106, Edge 106, Safari 16, Firefox 110) and have approximately 96% global browser coverage as of October 2026 (caniuse, CSS Container Queries). Internet Explorer has no support; some older mobile browsers (UC Browser, QQ Browser, Baidu, KaiOS) also have no support. A polyfill maintained by GoogleChromeLabs is available for projects that must cover those environments.

/* Declare the containment context on the parent */
.card-wrapper {
  container-type: inline-size;
  container-name: work-order-card;
}

/* Query the container width from the child */
.work-order-card {
  display: grid;
  grid-template-columns: 1fr;
  gap: 8px;
}

@container work-order-card (min-width: 480px) {
  .work-order-card {
    grid-template-columns: auto 1fr auto;
    gap: 16px;
  }
}

The Gale Console work-order card (fictional, from Gale.md) uses this pattern. In the sidebar, the container is about 280px wide: the card stacks the status badge, the work-order title and the action button vertically. In the main panel, the same card has a 640px container: the card switches to three columns — badge, summary, actions — without any media query.

One gotcha: container-type: inline-size creates a new stacking context for the element, which can affect z-index behavior. If you see dropdowns or tooltips that appear behind other elements after adding containment, check whether the stacking context is the cause (MDN, CSS Container Queries).

4. Intrinsic layout: the auto-fill/fit layer

Intrinsic layout does not need a query at all. The browser calculates how many columns fit:

/* Auto-fill: empty columns stay, maintaining the grid */
.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 16px;
}

/* Auto-fit: empty columns collapse, stretching remaining items */
.card-grid-fit {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 16px;
}

auto-fill keeps empty column tracks. auto-fit collapses them. The difference is visible when there are fewer items than columns: with auto-fill, items do not stretch to fill the row; with auto-fit, they do. For a card grid where you do not want cards stretching to full width, use auto-fill.

The minmax(240px, 1fr) value sets the minimum card width at 240px. At a 390px viewport with 16px gutters, that leaves 374px — enough for one 240px column. At 800px, two columns fit. No media query needed.

5. Choosing the right layer

Problem Layer to use
Page structure changes at different viewport sizes Media query
A component appears in multiple column widths on the same page Container query
A grid of repeating items should pack as many columns as fit Intrinsic layout
A component needs both page and container awareness Media query + container query

The most common mistake is reaching for a media query when a container query is the right tool. If you are asking "what happens to this card in a narrow sidebar?", that is a container question. If you are asking "what happens to the whole page on a phone?", that is a viewport question.

Step 4 is complete. In Step 5 we go to the details of form: radii, borders, shadows and icons.


6. Common mistakes

  • Putting container-type on the component itself. If you put containment on the .card element, you can only query it from its own children. The containment context must be the parent of the component you want to adapt.
  • Using container-type: size instead of inline-size. size creates containment on both axes, which breaks intrinsic height. Use inline-size unless you specifically need to query height as well.
  • Using media queries for component-level problems. A media query on a component creates tight coupling between the component and the page. When the layout changes, all the component breakpoints have to change too.
  • Ignoring minmax() limits. minmax(0, 1fr) can produce zero-width columns if there are many items. Set a reasonable minimum (e.g., 240px) to prevent invisible columns.

7. Try it

You need: a code editor or browser DevTools.

  1. Create a simple HTML page with a .card-wrapper div containing a .card div with some content.
  2. Add container-type: inline-size to .card-wrapper.
  3. Write a @container rule that changes the card from a stacked layout to a horizontal layout when the container is wider than 400px.
  4. Place the card wrapper inside two columns of different widths (use CSS grid with two columns: 200px 500px).
  5. Confirm in the browser that the card in the narrow column stacks, and the card in the wide column lays out horizontally.

How to know it's good: both cards change their internal layout at the container boundary, not at the viewport boundary. Resizing the browser should not affect which layout the card uses, as long as both columns stay above and below the 400px threshold.


8. If you already practise

  • Container queries also support style queries, which let a component change its visual style (not just its layout) based on a custom property value on its container. Style queries reached broad browser support in 2024 (MDN, CSS style queries).
  • The @container rule supports the same range operators as @media: min-width, max-width, width with range syntax (480px <= width <= 800px). Range syntax is cleaner for bounded queries.
  • In a design system, container queries let a component's tokens (spacing, font size) change per context without duplicating the component. The parent sets a data attribute (data-density="compact") or a custom property; the component reads it via a style query.

Checklist

Learn more

Books, courses and videos to go deeper

Grids for dense screens and marketing pages · Alignment and vertical rhythm · Spacing scales · Corners, borders and shadows with a rule

Saved only on this device.

↑ ↓ to move · Enter to open · Esc to close