SiteTidy
HomeGuidesMastering CSS Grid Layouts (Part 4): Responsive Grid, Container Queries and Fluid Components
Design

Mastering CSS Grid Layouts (Part 4): Responsive Grid, Container Queries and Fluid Components

Build genuinely responsive CSS Grid components with auto-fit, minmax(), container queries, container units, fluid sizing and breakpoints based on available space rather than device labels.

Published on August 15, 2026Updated August 31, 2026
Mastering CSS Grid Layouts (Part 4): Responsive Grid, Container Queries and Fluid Components

Responsive CSS used to mean one thing: ask how wide the viewport is, then switch the page into a different layout.

That model still matters, but modern component systems need something more precise.

A card might be 900px wide on a dedicated landing page and 320px wide in a dashboard sidebar, even though the browser viewport is 1440px in both cases. A product component does not really care whether the user owns an iPad or an ultrawide monitor. It cares how much space its parent has actually given it.

CSS Grid already solves part of that problem through intrinsic track sizing and auto-fit. Container queries finish the job by allowing components to respond to their container, not only the viewport.

This part is about using those tools together without creating a maze of breakpoints.

Start with layout that adapts before you add a query

A surprising amount of responsive behaviour can be expressed directly in the grid definition.

The classic example is:

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr));
  gap: 1rem;
}

This already answers a useful question:

How many cards can fit if each one should remain roughly 16rem or wider?

No media query is required.

The browser continuously evaluates the available space and creates the number of columns that fit.

That means your first responsive-design decision should often be:

Can the layout express this constraint intrinsically?

Only introduce a conditional query when the design itself needs to change, not merely because the viewport crossed an arbitrary number.

minmax() gives Grid a responsive contract

Consider:

grid-template-columns: repeat(auto-fit, minmax(18rem, 1fr));

The minimum says:

Do not make this track narrower than 18rem if you can avoid it.

The maximum says:

If there is spare room, let the track expand to an equal fraction of it.

That is much more useful than declaring:

grid-template-columns: repeat(4, 1fr);

and then writing several media queries to reduce the column count manually.

The browser already knows how many 18rem tracks fit. Let it calculate.

Protect very narrow containers

A common responsive pattern has a small edge case:

grid-template-columns: repeat(auto-fit, minmax(18rem, 1fr));

What if the entire container becomes only 15rem wide?

Your track’s minimum is larger than the container.

A robust version can use min():

.cards {
  grid-template-columns:
    repeat(auto-fit, minmax(min(100%, 18rem), 1fr));
}

Now the minimum is whichever is smaller:

  • the container’s full available width;
  • 18rem.

That prevents the track minimum itself from forcing overflow in extremely narrow contexts.

Use viewport breakpoints for page-level changes

Media queries are not obsolete.

They remain a natural tool when the layout decision genuinely relates to the viewport or device environment.

For example, an application shell might change from permanent sidebar to single-column navigation at a page-level breakpoint:

.shell {
  display: grid;
  grid-template-columns: 16rem minmax(0, 1fr);
}

@media (width < 52rem) {
  .shell {
    grid-template-columns: minmax(0, 1fr);
  }
}

That is a coherent viewport-level decision because the entire page mode changes.

But an individual card nested somewhere inside that page should not necessarily use the same breakpoint.

That is where container queries become valuable.

Container queries let a component respond to its actual space

Suppose a reusable article card can appear:

  • full-width on a landing page;
  • half-width in a two-column feed;
  • narrow inside a sidebar.

Viewport media queries do not tell the card how much width it received.

Mark its parent as a query container:

.card-slot {
  container-type: inline-size;
}

Then the card can react to that containing width:

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

@container (width >= 34rem) {
  .card {
    grid-template-columns: 12rem minmax(0, 1fr);
  }
}

Now the card becomes horizontal whenever its own container is wide enough.

The viewport could be 500px or 2500px. The component behaves according to the space it actually occupies.

Prefer inline-size for width-driven components

To create a size-query container, you can use:

container-type: inline-size;

or:

container-type: size;

For most responsive components concerned with width, inline-size is the practical default.

It establishes containment on the inline axis and lets you query that dimension.

size applies size containment in both axes. That can be useful, but it has stronger layout implications because the element’s size must be computable independently of its descendants in both dimensions.

Do not use the stronger containment merely because it sounds more complete.

Name containers in complex components

Unnamed container queries target the nearest eligible ancestor.

That is often convenient:

.widget-host {
  container-type: inline-size;
}

@container (width > 40rem) {
  .widget { ... }
}

In deeply nested layouts, explicit names can make intent clearer:

.dashboard-panel {
  container-name: panel;
  container-type: inline-size;
}

or with the shorthand:

.dashboard-panel {
  container: panel / inline-size;
}

Then query it:

@container panel (width >= 42rem) {
  .stats-grid {
    grid-template-columns: repeat(4, 1fr);
  }
}

The component now states which layout context it depends on.

Do not turn container queries into a second breakpoint framework

Container queries are powerful enough that it is tempting to reproduce a framework’s entire viewport breakpoint scale:

small
medium
large
xl
xxl

inside every component.

That misses their real value.

A better process is to resize the component and ask:

At what point does this component’s current design stop working well?

That is the breakpoint.

A profile card might need to switch at 28rem. A data table wrapper might need 46rem. A pricing panel might not need a query at all because auto-fit already handles it.

Responsive components become stronger when their thresholds are driven by content and layout, not global device categories.

Grid and container queries complement each other

Container queries should not replace intrinsic Grid behaviour.

Use Grid to absorb gradual size changes:

.products {
  display: grid;
  grid-template-columns:
    repeat(auto-fit, minmax(min(100%, 14rem), 1fr));
  gap: 1rem;
}

Then use a container query only when the component’s structure should change:

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

@container product (width >= 32rem) {
  .product-card {
    grid-template-columns: 10rem minmax(0, 1fr);
    align-items: start;
  }
}

The Grid handles continuous flexibility. The query handles a discrete layout-mode transition.

That division keeps responsive CSS smaller and easier to understand.

Container query units make sizing relative to the component

Modern CSS also provides container query length units.

Useful examples include:

  • cqw — 1% of container width;
  • cqh — 1% of container height;
  • cqi — 1% of container inline size;
  • cqb — 1% of container block size;
  • cqmin — smaller of the relevant container dimensions;
  • cqmax — larger of them.

For a component whose typography should scale gently with its container:

.card-title {
  font-size: clamp(1.1rem, 1rem + 2cqi, 1.75rem);
}

This can produce more portable component scaling than vw, which responds to the entire viewport even if the card occupies only a small portion of it.

Use these units with restraint. Text should remain readable and predictable; fluid scaling is not a reason to let typography swing wildly across contexts.

clamp() is useful for fluid Grid dimensions too

A page shell might want a sidebar that grows modestly but remains bounded:

.layout {
  display: grid;
  grid-template-columns:
    clamp(13rem, 18vw, 18rem)
    minmax(0, 1fr);
}

Or a content grid can use bounded spacing:

.grid {
  gap: clamp(0.75rem, 2vw, 2rem);
}

The general pattern is:

clamp(minimum, preferred-fluid-value, maximum)

This is useful when a property should scale gradually but still respect sensible limits.

Build breakpoints around failure, not hardware

A strong responsive workflow is practical:

  1. Build the component at a comfortable width.
  2. Slowly shrink its container.
  3. Find the point where the layout becomes awkward.
  4. Add a query just before that failure.
  5. Repeat for larger-layout opportunities only when useful.

For example, a card may work perfectly in one-column form until its container reaches 36rem. At that point, there is enough room to put the image beside the text.

Then this makes sense:

@container card (width >= 36rem) {
  .card {
    grid-template-columns: 12rem minmax(0, 1fr);
  }
}

The number has a reason.

Compare that with choosing 768px merely because a CSS framework calls it “md”.

A responsive dashboard widget

Suppose this widget appears in several dashboard regions.

Markup:

<section class="widget-host">
  <div class="analytics-widget">
    <header class="widget-header">...</header>
    <div class="chart">...</div>
    <aside class="summary">...</aside>
  </div>
</section>

Container:

.widget-host {
  container: analytics / inline-size;
}

Default narrow layout:

.analytics-widget {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 1rem;
}

Medium container:

@container analytics (width >= 34rem) {
  .analytics-widget {
    grid-template-columns: minmax(0, 1fr) 12rem;
  }

  .widget-header {
    grid-column: 1 / -1;
  }
}

Large container:

@container analytics (width >= 52rem) {
  .analytics-widget {
    grid-template-columns: minmax(0, 1fr) 16rem;
    gap: 1.5rem;
  }
}

The widget can now be dropped into a wide main panel or narrow sidebar without knowing which page placed it there.

That is a major improvement for component libraries.

Container queries and subgrid work well together

Imagine a responsive form panel whose outer container controls whether labels sit beside fields.

.form-panel {
  container-type: inline-size;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
}

.field-row {
  display: grid;
  grid-template-columns: subgrid;
  grid-column: 1 / -1;
}

When enough room exists:

@container (width >= 40rem) {
  .form-panel {
    grid-template-columns: 12rem minmax(0, 1fr);
  }
}

The subgridded field rows automatically inherit the new two-column geometry.

The parent decides the responsive layout. The children preserve alignment.

That is a strong modern CSS architecture.

Media queries still handle environment-level concerns

Container queries answer questions about container state and size.

Media queries remain appropriate for things such as:

@media (prefers-reduced-motion: reduce) { ... }
@media (prefers-color-scheme: dark) { ... }
@media (hover: hover) { ... }
@media (orientation: landscape) { ... }

They can also still control page-level viewport layouts.

The right question is not “media query or container query?” in the abstract.

It is:

Is this rule responding to the user’s environment/viewport, or to the space allocated to a component?

Use the mechanism that matches the dependency.

Responsive design is not just width

A component can fit physically and still fail functionally.

A responsive Grid review should also consider:

  • long translated text;
  • user zoom;
  • browser font-size preferences;
  • validation messages;
  • large numeric values;
  • empty states;
  • missing images;
  • unusually long names/emails/URLs;
  • keyboard focus indicators;
  • form controls with platform-specific sizing;
  • tables/charts with intrinsic minimum widths.

Do not test only lorem ipsum at five fashionable viewport sizes.

Content is part of the layout system.

Avoid hiding important content merely because the component is narrow

A common responsive shortcut is:

@container (width < 30rem) {
  .secondary-content {
    display: none;
  }
}

Sometimes that is appropriate. Sometimes it silently removes information or functionality because the component was placed in a sidebar.

Ask whether content can instead:

  • move below;
  • collapse behind an explicit disclosure;
  • simplify visually while retaining meaning;
  • become horizontally scrollable where appropriate;
  • use a different presentation.

Responsive design should adapt information, not casually delete it.

Avoid creating layout loops with containment assumptions

Container size queries work because the browser can calculate the query container’s size independently enough to avoid circular logic.

That is why size containment exists.

If a container’s width depends entirely on the exact size of its descendants, then asking descendants to change based on that width could create a feedback loop.

In normal block layouts, container-type: inline-size is straightforward because the container usually receives its inline size from its surrounding layout.

If a query container unexpectedly collapses or sizes oddly, inspect where its dimensions actually come from.

container-type belongs on the wrapper more often than the component itself

This is a practical pattern:

<div class="card-container">
  <article class="card">...</article>
</div>
.card-container {
  container-type: inline-size;
}

Then .card queries its ancestor.

Why not always place container-type on .card itself?

Because container queries style descendants based on an ancestor container. A component generally cannot size-query itself in the naive way developers first expect.

A host/wrapper also clearly represents the available layout slot.

Design systems can formalise this through layout primitives or component hosts.

A useful division of responsive responsibility

For a complex app, this architecture scales well:

Page shell
  -> viewport media queries
  -> major navigation/layout mode

Layout regions
  -> CSS Grid / subgrid
  -> distribute available space

Components
  -> container queries
  -> respond to their allocated width

Internal repeated content
  -> auto-fit + minmax()
  -> adapt without explicit queries

Not every layer needs all four mechanisms.

The point is to let each part react to the thing it actually knows about.

Responsive layout is only one part of page quality

After deployment, SiteTidy can inspect the surrounding page for performance, technical SEO, metadata and security issues while you test component behaviour across real content and containers.

Run Free Audit →

Common responsive Grid mistakes

Writing media queries before trying intrinsic layout

Use auto-fit, minmax(), flexible tracks and clamp() first where they genuinely express the design.

Querying the viewport for component decisions

A card in a sidebar does not care that the viewport is 1600px wide.

Creating a container around everything

Containment has layout implications. Establish query contexts deliberately.

Using global framework breakpoints for every component

Let component breakpoints follow component failure points.

Using vw for component typography

Viewport units can scale oddly when a component is narrow inside a huge screen. Container units may better express the relationship.

Forgetting very narrow cases

minmax(18rem, 1fr) can still overflow a 14rem container. Bound the minimum when necessary.

Treating responsive layout as visual-only testing

Zoom, localization, validation errors and dynamic content are often where the real failures appear.

Build components that know less about the page

The strongest result of container queries is architectural, not syntactic.

A reusable component no longer needs to know:

if viewport < 768px, I must be in a phone layout

It can simply know:

if I have less than 34rem, use my compact layout

That makes it easier to reuse components in dashboards, sidebars, modal panels, split-screen editors and future layouts nobody had imagined when the component was first built.

Grid provides the fluid geometry. Container queries provide context-aware mode changes. Media queries remain available for actual viewport/environment decisions.

Used together, they make responsive CSS far less dependent on guessing what device the user owns.

In Part 5, we finish the CSS Grid series with production-level patterns and debugging: Grid vs Flexbox decisions, overflow, dense layouts, accessibility, performance, layout testing and the common “almost works” bugs that appear with real data.

Ready to audit your site?

Put this guide into practice immediately using our free tools.

Browse 180+ Free Tools