Container Queries by kirupa (https://www.kirupa.com/me/index.htm) | filed under HTML and CSS (https://www.kirupa.com/html5/learn_html_css.htm) TL;DR: A component can change its layout based on the space it sits in, instead of the whole screen, using a container query. We mark an ancestor with container-type: inline-size and write rules inside @container (width >= 300px), so the same card can stack in a narrow sidebar and sit side by side in a wide main column. - An element can't query itself, so the container is always an ancestor, often a plain wrapper around the component. - A size container can't get its size from its content, so a container that would shrink to fit its content can collapse to nothing. - container-name lets a query skip nearby containers and ask a specific one, like @container sidebar (width < 250px). - Container query units like cqi are a percentage of the container's size, so 6cqi works out to 18px in a 300px container. We build a lovely product card. It has a picture, a title, a line of text, and an Add to cart button, and on a wide screen it puts the picture next to the text. Then somebody drops the very same card into a narrow sidebar, and it falls apart. The picture gets squished into a sliver, and the text wraps after every other word. Media queries can't save us here, because they only know about the screen, and the screen is the same size for both cards. In this article, we'll learn how to let a component change its layout based on the space it actually has, wherever we put it. Onwards! Why Aren't Media Queries Enough? A media query (https://www.kirupa.com/html5/responsive_design_media_queries.htm) asks a question about the viewport, like "is it at least 300px wide?" Every element on the page gets the same answer. Here's our card in a 1200px-wide window, once in the main column and once in the sidebar, with a media query that switches the card to its side-by-side layout on wide screens: [Image: A 1200px-wide browser window with a wide main column and a narrow sidebar. The same product card appears in both. A media query only knows the viewport is 1200px wide, so both cards get the side-by-side layout, and the card in the narrow sidebar ends up squished.] (https://www.kirupa.com/html5/images/cq_problem.png) The card in the main column has plenty of room, so the side-by-side layout looks great. The card in the sidebar gets the same side-by-side layout, because the viewport is just as wide from its point of view. It only has about 200px to work with, so the picture and text get crammed together. For years, we worked around this. We'd add a class like card--narrow to every card that went into a sidebar and hope nobody forgot. Or we'd measure each card with JavaScript and swap classes whenever it was resized. Both work, and both are a bit of a chore. What Is a Container Query? What if each card could ask about the box it sits in instead of the screen? The card in the main column would hear "you have 500px," the card in the sidebar would hear "you have 200px," and each one could pick the layout that fits. That's exactly what a container query does. It's a lot like a media query, except the question is about an element on the page, called a container, instead of the whole viewport. Here's the same page using container queries: [Image: The same browser window, main column, and sidebar. This time, each card checks the width of the container it sits in. The card in the wide main column keeps its side-by-side layout, and the card in the narrow sidebar stacks its picture on top of its text.] (https://www.kirupa.com/html5/images/cq_fixed.png) The card in the main column still has its side-by-side layout. The card in the sidebar noticed that it only has a narrow space and stacked its picture on top of its text. Getting there takes two steps. First, we mark the elements we want to ask about as containers. Then we write our rules inside an @container rule instead of an @media rule: main, aside { container-type: inline-size; } .card { display: grid; gap: 8px; } @container (width >= 300px) { .card { grid-template-columns: 90px 1fr; } .card img { grid-row: span 3; } } The container-type: inline-size line turns main and aside into containers that can be asked about their width. The card is stacked by default, and the @container rule switches it to a two-column grid whenever its container is at least 300px wide. Notice that the card itself doesn't know or care whether it's in a main column, a sidebar, or a pop-up. It only knows how much room it has. Size container queries have worked in every major browser since Firefox 110 shipped in February 2023, following Chrome 105 and Safari 16 the year before. How Do Container Queries Decide Which Container to Ask? When the browser sees an @container rule, it looks for the nearest ancestor of the element we're styling that's a container. In our example, the card's nearest container is the main or the aside it lives in, so that's whose width gets checked. One thing that follows from this is that an element can't query itself. If we put container-type on the .card and then wrote rules for .card inside @container, those rules would look past the card to one of its ancestors. Rules for the card's children would use the card, though. When we want a component to respond to its own width, a common pattern is to put container-type on a plain wrapper element around it. The container-type property has three values. With inline-size, we can ask about the container's width (or more precisely, its size along the direction text flows, which we talk about in CSS Logical Properties (https://www.kirupa.com/html5/css_logical_properties.htm)). With size, we can ask about its height too. With normal, which is the default, the element isn't a size container at all. The other detail to keep in mind, and this is a BIG detail, is that a container's size can't depend on what's inside it. That's how the browser avoids an endless loop where the container's width changes the card's layout, which changes the container's width, which changes the card's layout again. So when we set container-type: inline-size, the browser sizes the container as if it were empty. If the container gets its width from outside, like a block element filling its parent or a grid track, that's no problem at all. If its width would have come from its content, like an inline-block element or a flex item with no width of its own, it can collapse to nothing. And for container-type: size, the same is true of its height, so a size container needs a height that doesn't come from its content. How Do We Give One Card Several Layouts? Just like media queries, we can stack several container queries to give a component a few layouts. Here's our card in three containers of different widths, with a stacked layout under 300px, a side-by-side layout from 300px, and a details panel that joins in from 450px: [Image: The same card in containers that are 240px, 360px, and 520px wide. Under 300px, the picture sits on top of the text. From 300px, the picture sits beside the text. From 450px, a details panel also appears on the right.] (https://www.kirupa.com/html5/images/cq_layouts.png) The card at 240px is too narrow for anything side by side, so it stacks. The card at 360px has room for the picture and the text next to each other. The card at 520px has enough left over for the details panel on the right. Here's the extra rule for that third layout: .card .details { display: none; } @container (width >= 450px) { .card { grid-template-columns: 90px 1fr 120px; } .card .details { display: block; grid-column: 3; grid-row: 1 / span 3; } } Since this is the same mobile-first idea from media queries, the stacked layout is our default, and each container query adds to it as the space grows. How Do We Name Containers? Sometimes the nearest container isn't the one we want to ask about. Maybe our card sits inside a small container of its own, but we want it to react to the width of the whole sidebar. We can give containers names with container-name, and then mention the name in our query: aside { container-name: sidebar; container-type: inline-size; } @container sidebar (width < 250px) { .card .buy { width: 100%; } } That query skips over any unnamed containers in between and only asks the nearest container named sidebar. There's also a container shorthand that sets both at once, with the name first and the type after a slash: container: sidebar / inline-size. What Are Container Query Units? Containers also come with their own units, which are sizes relative to the container instead of the viewport. The one we'll use the most is cqi, where 1cqi is 1% of the container's inline size (its width, in a normal horizontal layout). There's also cqb for the block size, cqw and cqh for width and height, and cqmin and cqmax for the smaller and larger of the two. They're great for text that should scale with a component: .card h3 { font-size: clamp(1rem, 6cqi, 1.5rem); } In a 300px container, 6cqi works out to 18px, so the title is 18px. In a 200px container, 6cqi is only 12px, which is smaller than our 1rem minimum, so clamp() keeps the title at 1rem (16px by default). In a 500px container, it would be 30px, but the 1.5rem maximum caps it at 24px. If an element has no container above it, these units fall back to sizes based on the viewport. Let's Play With Container Queries Here's our card in a main column and a sidebar, using the same 300px and 450px breakpoints. You can change how wide the sidebar is and switch between letting media queries or container queries pick each card's layout: Live demo: The same waffle maker product card in a main column and a sidebar, with a Sidebar width slider, buttons to let a Media query or a Container query pick the layout, and readouts of each column's width and the layout it gets (https://www.kirupa.com/html5/examples/container_queries_lab.htm) Try it: 1. Drag the Sidebar width slider 2. Press Media query 3. Press Container query again With media queries, both cards always get the same layout no matter where the slider is, because they share one viewport. With container queries, the two cards trade layouts as the sidebar grows and the main column shrinks. Can We Query More Than Size? Yes, with style queries. Instead of asking how big a container is, a style query asks about the value of a CSS custom property on it. Every element can be asked about its custom properties, so we don't need container-type for these: .promo { --tone: loud; } @container style(--tone: loud) { .card { border: 4px solid #e0605a; } } Any card inside an element with --tone: loud gets a bright border, without needing a separate class on the card itself. Style queries for custom properties became available in every major browser in May 2026, when Firefox 151 shipped them. The specification also allows style queries for regular properties, like asking whether a container's display is flex, but at the time of writing, no browser supports that, so stick with custom properties for now. Should We Still Use Media Queries? Absolutely. The two answer different questions. Media queries are still the right tool for decisions about the whole page, like how many columns the page layout has, and for settings that come from the person and their device, like prefers-color-scheme or prefers-reduced-motion. A container has no idea whether someone prefers dark mode. Container queries are the right tool for components that get reused in different places. A good way to think about it is that media queries lay out the page, and then container queries let each component make itself comfortable in the space the page gave it. Frequently Asked Questions What Is the Difference Between Container Queries and Media Queries? A media query asks about the whole viewport, so every element on the page gets the same answer. A container query asks about an element around a component, so the same component can get different answers in a wide main column and a narrow sidebar. Why Is My Container Query Not Working? A common reason is that no ancestor has container-type set, or the rule styles the container itself, which can't query itself. Another is a container that collapsed because its width would have come from its content. Giving the container a width from outside, like a grid track or a block element filling its parent, fixes that. What Does container-type: inline-size Do? It turns an element into a container that its descendants can ask about its width, or more precisely its size along the direction text flows. The browser also sizes it as if it were empty in that direction, so its width has to come from outside instead of from its content. What Is the cqi Unit? 1cqi is 1% of the inline size of the nearest container, which is its width in a normal horizontal layout. In a 300px container, 6cqi works out to 18px. If an element has no container above it, the unit falls back to a size based on the viewport. Do Container Queries Work in All Browsers? Size container queries have worked in every major browser since Firefox 110 shipped in February 2023. Style queries for custom properties joined them in May 2026, when Firefox 151 shipped them. At the time of writing, style queries for regular properties don't work in any browser. Conclusion Container queries fix one of the longest-standing gaps in responsive design. We mark an element as a container with container-type: inline-size, and the components inside it can write @container rules that react to how much room they actually have. Named containers let a component ask a specific ancestor, cqi units let sizes scale with the container, and style queries let a container pass along a custom property for its children to react to. The main thing to remember is the containment rule: a size container can't get its size from its content. Give it a width from the outside, and everything else falls into place. Before you go, here are a few things you can do: 1. Test your knowledge by taking the quiz below. 2. Check out some related content: - Responsive Design and Media Queries (https://www.kirupa.com/html5/responsive_design_media_queries.htm). Adapt a whole page to any screen with media queries and breakpoints. - CSS Grid Fundamentals (https://www.kirupa.com/html5/css_grid_fundamentals.htm). Size columns with fr, place items by line number, and line up cards with subgrid. - CSS Logical Properties (https://www.kirupa.com/html5/css_logical_properties.htm). See why inline-size follows the direction text flows. 3. Stuck on something, or curious about a detail we skipped? Ask on the forums (https://forum.kirupa.com/), and we'll help you out. [Mastery Quiz] Test your knowledge! Take the Reviewing Container Queries Quiz: https://www.kirupa.com/quizzes/container_queries.htm Practice with 4 multiple-choice questions and an explanation after every answer. [/Mastery Quiz]