{"canonical_url":"https://engineer.company/glossary/css-container-query/","content_markdown":"# CSS container query\n\n## What it is\n\nA CSS container query applies rules to descendants according to conditions on an\nancestor container. A size query can ask whether the container's inline size is\nat least a given value. This lets a component respond to the space the page\ngives it. The same card can use a stacked layout in a narrow sidebar and a split\nlayout in the wider article column, even when both cards appear in the same\nbrowser window.\n\nFor a size query along the inline axis, declare an ancestor as a query container\nwith `container-type: inline-size`. Then put the conditional styles for its\ndescendants in an `@container` rule. A threshold expressed as\n`inline-size \u003e= 32rem` refers to the eligible container's inline dimension. In\nan ordinary horizontal writing mode that is the width. Inline size follows the\nwriting mode, which matters when layouts also support vertical text.\n\nWithout a name, the query uses the nearest eligible ancestor for the queried\nfeatures. An explicit container name can tie the condition to a particular\nwrapper when a component contains nested query containers. Use a name that\ndescribes the layout relationship so the rule makes sense when reading the\nstylesheet apart from the HTML.\n\n## Why it matters\n\nViewport media queries work well when a choice depends on the whole page or the\nuser's environment. Reusable components also encounter differences within one\nviewport. A search result could appear in a one-column mobile layout, a\nfull-width desktop panel and a desktop drawer. The drawer may be narrower than\nthe mobile result. Making a component depend on the space it receives keeps its\nlayout decision relevant in all three placements.\n\nThis can reduce the number of page-specific CSS overrides. It also makes the\nlayout easier to verify. The same article card can be exercised at a range of\ncontainer sizes in a test page with no change to the browser window. The\ncomparison shows which adjustments belong to the component and which still\nbelong to the overall page.\n\nThe declaration creates a containment boundary and can change intrinsic sizing.\nIn the queried axis the element no longer determines its size from its children\nas it did before. Consider a wrapper that would otherwise shrink to the natural\nwidth of its text. Give the wrapper a predictable size from its parent grid or\nnormal flow before its contents depend on that size.\n\n## What it looks like in practice\n\nStart with a reference card whose image, heading and description stack in normal\nflow. Put it in a wrapper that receives space from the parent layout and set\n`container-type: inline-size` and `container-name: reference-card` on the\nwrapper. When that container crosses the point where the picture and the prose\nfit comfortably beside one another, use\n`@container reference-card (inline-size \u003e= 32rem)` to turn the card itself into\na two-column grid.\n\nPlace the same card in an article grid and in a narrow sidebar at a single\ndesktop viewport. Verify the baseline stacking in the sidebar and the wider\narrangement in the article. Then resize just the sidebar and test immediately on\neither side of the condition. Set the threshold from the real content. A long\nheading, larger user text or translated description can show that a visually\nappealing threshold leaves too little room for the reading column.\n\nEngineer's\n[typographic scale case study](/portfolio/replaced-27-font-sizes-with-a-6-step-scale/)\ndescribes replacing 27 font sizes with six deliberate steps and reviewing the\nsmall text on a phone. Use the same kind of inspection for the card. A width\ncondition can set the arrangement, while the type decisions still need to\nproduce readable text in both arrangements. Review the same card with the\nlongest useful copy, including a title that wraps over multiple lines in a\ntranslated edition.\n\nKeep the baseline styles complete. The links, source order and heading should\nremain usable when the rule does not apply. Compare keyboard movement and visual\norder through both layouts. A responsive rule can move a picture visually\nwithout gaining any reason to reorder the controls.\n\n## Where it goes wrong\n\nA frequent mistake is putting the changing layout on the same element meant to\nprovide the query size. A query conditions styles on an eligible ancestor of the\nelement being styled. Use a wrapper as the query container and put the layout\nrule on the inner card. Record that relationship in the component so a refactor\npreserves it.\n\nNesting can change which size is read. An unnamed rule may start responding to a\nnewly introduced wrapper closer to the target. Inspect the matched container\nbefore tuning a threshold that appears wrong. Use a name when a rule\nintentionally measures a specific ancestor, and check that each instance still\nsupplies it.\n\nContainment also exposes layouts that depend on the width of their contents. If\na wrapper loses its expected size in a flexible layout, inspect the parent's\ngrid tracks or sizing rules. Fix the allocation of space at that level. Raising\nor lowering the query threshold cannot recover a dimension the parent never\nallocated.\n\nFinally, test the condition at useful extremes. Check a narrow column, a wide\none, a screen with increased text size and a second language that wraps\ndifferently. A finished component explains its baseline layout, the change it\nmakes at a measured width and the reading behaviour people experience on both\nsides.\n\n## Further reading\n\n- [CSS Conditional Rules on container queries](https://drafts.csswg.org/css-conditional-5/#container-queries).\n  Defines the syntax and selection of a query container.\n- [MDN's container query guide](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Containment/Container_queries).\n  Shows how to declare a container and style its descendants.\n- [CSS Containment Level 3](https://drafts.csswg.org/css-contain-3/). Explains\n  containment and links to the current definitions for container queries.\n\n\u003chttps://engineer.company/glossary/css-container-query/\u003e\n","date_modified":"2026-11-25T07:18:46Z","description":"Use one component in two widths at the same viewport and test its baseline layout, containment and width-dependent rules.","formats":{"gemtext":"https://engineer.company/glossary/css-container-query/index.gmi","gopher":"https://engineer.company/glossary/css-container-query/gophermap","html":"https://engineer.company/glossary/css-container-query/","json":"https://engineer.company/glossary/css-container-query/index.json","markdown":"https://engineer.company/glossary/css-container-query/index.md","pdf":"https://engineer.company/glossary/css-container-query/index.pdf"},"kind":"page","language":"en","title":"What is a CSS container query?","url":"https://engineer.company/glossary/css-container-query/","version":1}