Skip to content
tezvyn:

CSS Container Queries: Style Components, Not Just Pages

Source: developer.mozilla.orgHardHow cards are made

CSS Container Queries: Style Components, Not Just Pages

Container queries let components respond to their own size, not the viewport's. Use them for cards or sidebars that must adapt to different layouts. The footgun: you must first declare a parent as a "containment context" with container-type for it to work.

Why it exists

Traditional media queries only know about the browser viewport. This makes it hard to build components that can be dropped into any layout, as their styling is tied to the page size, not their own allocated space. Container queries solve this by letting components query their parent's dimensions and adapt accordingly.

The mental model

Think of container queries as media queries for elements. Instead of asking "how wide is the browser window?", a component asks "how wide is the box I've been placed in?". This decouples the component's styling from the page's layout, making it truly portable and self-contained.

How it works

Using container queries is a two-step process. First, you must establish a "containment context" on a parent element. You do this with the container-type property, for example, container-type: inline-size;. This tells the browser to start tracking that element's size. Second, you write an @container rule targeting a child element. This rule contains a condition, like (min-width: 400px), and applies specific styles only when the container meets that condition. You can also name containers with container-name for more complex layouts.

When to use it

Use container queries for any component that needs to adapt its layout based on the space it's given. This is ideal for card-based UIs, dashboard widgets, sidebars, and any component in a design system intended for reuse across different contexts. They allow a single component to look right in a main content area or a narrow aside without modification.

When not to use it

For global, page-level layout changes, like switching a whole website from a three-column desktop view to a single-column mobile view, traditional media queries are still the right tool. Container queries are for component-level adaptation, not rearranging the entire page structure.

One canonical example

Imagine a product card. In a wide column, the image is beside the text. In a narrow column, the image should stack on top. First, define the container: .grid-cell { container-type: inline-size; }. Then, write the query for the card inside it: @container (max-width: 350px) { .product-card { flex-direction: column; } }. Now the card's layout is determined by the grid cell's width, not the whole page.

Interview question

A product card needs to adapt its internal layout based on the width of the container it's placed in. Which CSS feature is best suited for this?

  • a.Using JavaScript to detect the parent's width and apply styles
  • b.Applying flex-direction directly to the product card
  • c.A traditional media query targeting the viewport width
  • d.A CSS Container Query on the product card's parentCorrect
Why?

Container queries are designed for components to adapt their styling based on the space provided by their parent, making them ideal for reusable components like a product card. Traditional media queries respond to the overall viewport size, not a component's specific allocated space, which is explicitly stated as a limitation container queries solve.

Just read this? Test yourself on what you have been reading.

Read the original → developer.mozilla.org

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on css — each one lists the topics its interview covers.

See open roles