Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How can I make a layout respond to its container instead of the viewport?

Quick answer

Use a CSS size container and an @container rule when a component should respond to the width available from its parent, rather than the browser window. Set container-type: inline-size on a wrapper, then write a query that styles the wrapper’s descendants at an appropriate width. Unlike a viewport media query, this lets the same component adapt differently in a narrow panel and a wide page.

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

My card layout looks cramped in a sidebar but fine on a full page. How do I make the card respond to the space it actually gets?

Geez

Use a size container around the component, then query that container’s width. A viewport media query responds to the viewport; a size-based @container rule responds to the size of an eligible containing element. This is useful when the same card can appear in places with different widths on the same page. [S1]

For example, give the card’s wrapper container-type: inline-size, then put the query on the card inside it:

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

@container (max-width: 34rem) {
  .product-card {
    grid-template-columns: 1fr;
  }
}

Here, 34rem is an example threshold, not a universal breakpoint. Adjust it to the point where this card’s content needs a different arrangement. Make sure .product-card is a descendant of .card-region and is set up as a grid.

Follow-up question

How do I choose the right element to make the container?

Geez

Choose the element whose available inline size should control the component. In a typical horizontal writing mode, that corresponds to width. Often it is a wrapper around the card, widget, or embedded panel. The size-query container must be explicitly established; container-type: inline-size enables queries based on its inline size. [S2]

One important detail: the query styles descendants of the container, not the container itself. So if the layout change needs to affect the card, put the queryable wrapper outside the card. If you make the card itself the container, its query will target elements inside it instead. Identify what gets narrow in the actual layout, then place the container boundary there.

Follow-up question

What if my wrapper has no set width? Could the container rule make it collapse?

Geez

It can be a concern: size containment means the container’s size needs to come from its context or be explicitly set; otherwise it can collapse. [S2] Check that the wrapper gets a usable width from its surrounding layout. The appropriate sizing fix depends on that layout, so inspect the wrapper’s size rather than assuming the query threshold is the problem.

A practical debugging sequence is: confirm the wrapper has nonzero width; confirm it has container-type: inline-size; then check whether the query condition is met and whether the element you want to style is a descendant. These checks help distinguish a setup issue from a threshold that does not fit your design.

Follow-up question

Can you give me a small pattern I can adapt, and when should I keep a viewport media query?

Geez

Use a container query when the component’s layout should follow its own available space. Keep a viewport media query for changes driven by the overall viewport or device characteristics, such as a page-level arrangement. They address different contexts. [S1]

Here is a pattern to adapt. Replace the class names and threshold to suit your component:

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

.info-card {
  display: grid;
  grid-template-columns: 8rem 1fr;
  gap: 1rem;
}

@container (max-width: 32rem) {
  .info-card {
    grid-template-columns: 1fr;
  }
}

To tune it, resize the parent area rather than only the browser window. Pick a threshold where the two-column version becomes crowded, and check the card in each place it appears.

A practical checklist

  • Identify the parent area whose available width should control the component.

  • Place a wrapper at that boundary and set container-type: inline-size on it.

  • Write the @container rule to style descendants, not the container itself.

  • Choose a threshold based on the component’s content and layout change.

  • Test the component at different parent widths and verify the wrapper has a usable size.

A few more useful answers

Can a container query replace every media query?

No. Use a container query for a component’s containing size and a media query for viewport or device conditions. They address different kinds of context. [S1]

Should I use `container-type: size` instead?

For queries based on inline size, container-type: inline-size is the relevant setup. size enables broader size queries, with additional size-containment considerations. [S2]

Why does my query not affect the element I put `container-type` on?

Size container queries apply to descendants of the container. Put a wrapper around the element you want the query to style, or target suitable descendants instead. [S2]

Sources

References used for this guide.

  1. [S1] Using container size and style queries — CSS | MDN
    developer.mozilla.org · Checked
  2. [S2] container-type CSS property — CSS | MDN
    developer.mozilla.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work