CSS Container Type Inline-Size: Usage and Implementation
In this article
In Google Chrome and other modern browsers, container-type: inline-size is a CSS property that allows an element to act as a query container based on its own width (in horizontal writing modes). This enables child elements to change their styling based on the size of their parent container rather than the size of the entire browser viewport, facilitating truly modular component-based design.
Understanding the Concept of Container Queries
For years, responsive web design relied almost exclusively on Media Queries. Media queries look at the viewport—the total width of the browser window. While effective for page-level layouts, they fail when a component needs to look different depending on where it is placed on a page. For example, a card component might be placed in a narrow sidebar or a wide main content area. A media query cannot distinguish between these two placements because the viewport size remains the same.
Container queries solve this by shifting the point of reference. Instead of asking "How wide is the screen?", the CSS asks "How wide is the container that holds this element?". By setting container-type: inline-size on a parent element, you designate it as a container. Any descendant of that element can then use a @container rule to apply styles based on that parent's dimensions.
How container-type: inline-size Works
The inline-size value specifically tells the browser to track the size of the container along the inline axis. In English and other left-to-right or right-to-left languages, the inline axis is the horizontal width. This is the most common use case because most web layouts fluctuate primarily in width rather than height.
To implement this, you must follow a specific hierarchy. You cannot query an element's own size to style itself; the query must be performed by a child looking at a parent. The process generally follows these steps:
- Define the Container: Apply
container-type: inline-sizeto the parent element. This tells Chrome to start monitoring the width of this specific box. - Name the Container (Optional): You can use
container-name: sidebar-containerto specify which container a child should look at if there are multiple nested containers. - Create the Query: Use the
@containerrule to define the breakpoint. For example,@container (min-width: 400px) { ... }. - Style the Child: Inside that query, write the CSS for the child element that should change when the container reaches that width.
Comparing Inline-Size vs. Size
When using the container-type property, you have two primary options: inline-size and size. Understanding the difference is critical to avoid layout bugs and "infinite loop" rendering issues.
- inline-size: This only monitors the width (in horizontal writing modes). The height of the container is still determined by its content. This is the standard choice for most responsive components because it doesn't interfere with the natural vertical flow of the document.
- size: This monitors both the width and the height. However, using
container-type: sizeforces the container to have a fixed size or be explicitly defined. Because the container's size can no longer be determined by its children (since the children's size might depend on the container), the container effectively collapses to 0px height unless you manually set a height.
Because size is so restrictive, inline-size is the preferred method for 99% of UI components, such as navigation bars, product cards, and image galleries.
Practical Implementation Example
Consider a "Feature Card" component. In a wide area, you want the image on the left and text on the right. In a narrow area, you want the image on top and text below. With media queries, you would have to know exactly where the card is placed. With inline-size, the card handles itself.
First, the wrapper is defined:
.card-container {
container-type: inline-size;
container-name: feature-card;
width: 100%;
}
Then, the child element is styled based on that container:
.card-content {
display: flex;
flex-direction: column;
}
@container feature-card (min-width: 500px) {
.card-content {
flex-direction: row;
gap: 20px;
}
}
In this scenario, if the .card-container is placed in a 300px sidebar, the content remains a column. If it is moved to a 800px main section, it automatically switches to a row, regardless of the browser window's total width.
Common Problems and Misconceptions
One of the most frequent mistakes developers make is attempting to apply container-type: inline-size to the same element they are trying to style within the @container query. This creates a circular dependency: the element's size would determine its styles, but its styles might change its size, leading to a layout loop. Always ensure the container is a parent of the element being queried.
Another common point of confusion is the difference between @media and @container. It is a misconception that container queries replace media queries entirely. Media queries are still the correct tool for global changes, such as changing the site-wide background color or hiding a global navigation menu on mobile. Container queries are for component-level responsiveness.
Additionally, some users struggle with "Layout Shift." Because the browser must calculate the container size before applying the child styles, there can occasionally be a flicker. This is minimized in Chrome's modern rendering engine, but using stable dimensions for containers helps maintain a smooth user experience.
Advanced Usage: Container Query Units
Along with container-type, Chrome introduced new CSS units that allow for fluid typography and sizing relative to the container rather than the viewport. These are essential for creating components that scale proportionally.
- cqw (Container Query Width): 1cqw is 1% of the query container's inline size.
- cqh (Container Query Height): 1cqh is 1% of the query container's block size.
- cqi (Container Query Inline-Size): 1cqi is 1% of the inline size of the query container.
- cqb (Container Query Block-Size): 1cqb is 1% of the block size of the query container.
- cqmin: The smaller value of cqi or cqb.
- cqmax: The larger value of cqi or cqb.
Using font-size: 5cqw means the text will grow and shrink as the container changes size, providing a much more polished look than fixed pixel sizes or viewport-based vw units, which ignore the boundaries of the component.
Frequently Asked Questions
Does container-type: inline-size work in all browsers?
It is supported in all modern versions of Chrome, Edge, Firefox, and Safari. For very old browsers, the @container rules will simply be ignored, so it is recommended to provide a "mobile-first" default style that looks acceptable even if the container query doesn't trigger.
Can I nest multiple containers?
Yes, you can nest containers. If you have multiple containers, the @container query will by default look at the nearest ancestor that is a query container. To target a specific ancestor further up the tree, you must provide a container-name to the parent and reference that name in your query.
Why is my container collapsing to 0px height?
This usually happens if you used container-type: size instead of inline-size. The size value requires the container to have an explicit height because it cannot rely on its children to define its dimensions. Switch to inline-size if you only need to track width.
Is there a performance penalty for using container queries?
While container queries require the browser to perform more calculations than media queries, the impact is negligible for the vast majority of websites. Chrome's rendering engine is highly optimized for these queries, though avoiding hundreds of deeply nested containers on a single page is still a good general practice for performance.
Do I need a specific HTML structure for this to work?
You only need a parent-child relationship. The parent must have the container-type property, and the child (or a descendant of the child) must be the target of the @container rule. There are no special HTML tags required; it works on standard div, section, article, or any other block-level element.