Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk5 min

CSS Container Queries: Style Components by Their Container’s Size

CSS container queries let a component adapt to the space its containing element provides. Here is how to set them up, choose a container type, handle nesting, and check support.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

CSS container queries let a component change its styles based on the space its containing element provides, instead of the size of the browser window. A card that sits in a wide main column and the same card in a narrow sidebar can each lay itself out correctly from the same stylesheet. Container queries complement media queries: they answer “how much room does this component have here?”, while media queries still handle viewport, device, and user-preference conditions.

How container queries differ from media queries

A media query tests the viewport or the device. A size container query tests the dimensions of an ancestor that you have explicitly declared as a query container. Because the test is attached to the component’s surroundings rather than the page, the same component can respond correctly in layouts you have not yet built.

Question Media query Container query
What is measured? The viewport or device (for example, the browser window width) The inline or block size of a declared ancestor container
What must be set up first? Nothing beyond the @media rule An ancestor with size containment, usually container-type: inline-size
Typical use Page-level navigation, device orientation, reduced-motion preferences Reusable cards, sidebars, widgets that appear in several layouts
Can the rule reach the container itself? Not applicable No. The rule styles descendants of the container
Can the two be combined? Yes Yes

Use a container query when the question is about the component’s available room. Keep a media query when the question is about the device or the user’s preferences.

How do I make a CSS query respond to a container’s width?

Follow these steps. The example uses a card that switches to a two-column layout once its wrapper is wider than 40rem.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Sale
HTML and CSS: Design and Build Websites
  • HTML CSS Design and Build Web Sites
  • Comes with secure packaging
  • It can be a gift option
  1. Declare the wrapper as a query container. Add container-type: inline-size to the element that wraps the component. In horizontal writing modes this queries width; in other writing modes it follows the logical inline axis.
  2. Put the styles on a descendant. The query applies to elements inside the container. The card in the example is a child of .card-shell, not the wrapper itself.
  3. Write the base layout first. Give the card a single-column default so it still works where container queries are unsupported.
  4. Add the @container rule. Change the layout only when the container passes the threshold.
.card-shell {
  container-type: inline-size;
}

.card {
  display: grid;
  grid-template-columns: 1fr;
}

@container (width > 40rem) {
  .card {
    grid-template-columns: 1fr 2fr;
  }
}

Set the threshold from what the content needs, such as when a heading or image stops fitting comfortably. A breakpoint copied from a viewport layout is not a reliable component breakpoint, because the component may sit in a column much narrower or wider than the page.

Name the container when nesting makes the target unclear

An unnamed size query uses the nearest eligible ancestor. When components nest inside other query containers, that ancestor may not be the one you intend. Give the wrapper a name and refer to it explicitly:

.card-shell {
  container: card-region / inline-size;
}

@container card-region (width > 40rem) {
  .card {
    grid-template-columns: 1fr 2fr;
  }
}

The container shorthand sets the name and the type together. The longhand properties are container-name and container-type.

Choosing the container type

The container type determines which dimensions you can query and how much containment the browser applies. Containment is not free: it changes how the element is sized and laid out, so use the least restrictive type that meets the need.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Value Dimensions you can query Containment applied When to choose it
inline-size The logical inline dimension (width in horizontal writing modes) Size containment on the inline axis Most component layouts, where the decision depends on width
size Both inline and block dimensions Stronger size containment on both axes Only when the layout must respond to height as well as width, and the container’s size can be fixed by its own rules

Container query units

Container query units such as cqi and cqw size a value relative to the query container. They suit fluid adjustments, such as a heading that scales with its card, so you do not need a discrete breakpoint for every step:

.card-title {
  font-size: clamp(1rem, 4cqi, 1.5rem);
}

Units and @container rules work well together. Use units for smooth scaling and rules for structural changes such as switching from one column to two.

Rank #3
Sale
Web Design with HTML, CSS, JavaScript and jQuery Set
  • Brand: Wiley
  • Set of 2 Volumes
  • A handy two-book set that uniquely combines related technologies Highly visual format and accessible language makes these books highly effective learning tools Perfect for beginning web designers and front-end developers

Browser support and fallbacks

MDN’s reference for the @container at-rule marks it as Baseline Widely available, with availability across browsers since February 2023. That date covers the at-rule’s core size queries. MDN also notes that some parts of the feature have varying support, so the statement does not extend to every query type.

  • Size queries are the most established category and the one this article covers.
  • Style, scroll-state, and anchored queries are newer. Check MDN’s compatibility table for the exact query form before relying on it in production.
  • Fallbacks: MDN describes using grid or flexbox to produce a usable default layout, then adding media queries for viewport-based changes in browsers without container-query support.

Check the compatibility data for your own browser targets rather than relying on a version number from an older article.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Side effects of declaring a query container

Declaring a size query container adds containment. MDN explains that containment lets the browser avoid re-evaluating every element to see whether a query applies, and it helps prevent cyclic layout, where a descendant’s styles change the container’s own size and the layout must be recalculated. The trade-off is that the container’s sizing behaves differently, so an element that previously grew to fit its content may now take a different size. After adding a container, check the surrounding layout and confirm that nothing collapses or overflows.

Debugging container queries in Chrome DevTools

Chrome’s DevTools documentation describes several features that help when a query does not behave as expected:

  • Query containers are marked in the Elements panel, so you can see which ancestors establish a container.
  • You can overlay a container and its descendants on the page to check which elements the query affects.
  • The Styles pane shows applicable @container declarations, with a link to the parent container that the rule is matched against.

If a rule does not apply, first check that the target is a descendant of the intended container and that the container has an eligible type. Then check whether a name is required to select the correct ancestor.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Standards status

The W3C’s CSS Containment Module Level 3 is published as a Working Draft. A Working Draft is work in progress, and the W3C notes that it may be updated, replaced, or made obsolete. Treat the specification as a living document: the browser behavior described in MDN’s reference is the practical guide, and the specification text is the authority on edge cases. Re-check the specification and compatibility tables before shipping features that depend on newer query categories.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

In practice, container queries are a broadly available layout tool for size-based component styling. The main decisions are which container type to use, where the threshold should sit, and whether a fallback layout is acceptable for browsers that lack support.

Container queries are not a replacement for media queries. Use them for components that need to know their own room, and keep media queries for viewport, device, and preference conditions.

Sources: MDN Web Docs (@container reference and compatibility data); W3C, CSS Containment Module Level 3 (Working Draft); Chrome for Developers, DevTools documentation.

Note: Support details may change, and the statements above reflect MDN’s current reference as reviewed in October 2026.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.