October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk4 min

CSS Container Queries: How to Use Them

Make components respond to their containing space with CSS container queries. Learn the core pattern, container selection, sizing trade-offs, units, support, and troubleshooting.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use a CSS container query when a component should respond to the space available around it rather than to the browser window. Give an ancestor container-type: inline-size, then put a conditional @container rule around styles for its descendants.

How to write a basic container query

Start with the component’s default styling, make an ancestor an eligible query container, and then define the change inside @container. The query applies to descendants of that container; it does not select an arbitrary sibling or query the viewport.

<div class="post">
  <article class="card">
    <h2>Card title</h2>
    <p>Card content</p>
  </article>
</div>
.post {
  container-type: inline-size;
}

.card h2 {
  font-size: 1em;
}

@container (width > 700px) {
  .card h2 {
    font-size: 2em;
  }
}

The 700px threshold is illustrative, not a recommended universal breakpoint. Choose a threshold based on when the component’s content and layout need to change. For the syntax and further examples, see MDN’s CSS container queries guide.

Choose between container queries and media queries

Use @container when a component’s behavior depends on the size of its containing region. This makes a card reusable in a wide main column or a narrow sidebar without needing to know where it will appear. Use @media when the trigger is a viewport or device condition. The right choice depends on what should cause the style change.

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

Select the query container and axis

Use inline-size for width-responsive components

container-type: inline-size enables queries along the inline axis, which is usually the relevant dimension for a component adapting to available width. The inline axis is logical: it follows the writing mode and is not always identical to physical width.

Use size only when you need both axes

container-type: size enables queries against both inline and block dimensions. It also applies size containment: the container’s size is computed independently of its contents. Establish its dimensions through its layout context or explicit sizing; otherwise, the element may collapse because its children no longer determine its size. See MDN’s container-type reference for the property’s containment behavior.

Name a container when selection should be explicit

An unnamed query uses the nearest eligible ancestor. In nested components, that may not be the region you intended. Give the container a name and use that name in the query:

.post {
  container: sidebar / inline-size;
}

@container sidebar (width > 700px) {
  .card {
    font-size: 2em;
  }
}

The container shorthand sets the name and type; you can instead set container-name and container-type separately. Naming makes the target explicit when multiple eligible ancestors or nested components are involved. More detail is in MDN’s @container reference.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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

Use container-relative length units when fluid scaling helps

Container query length units let descendant lengths scale with the eligible size container. The units represent these proportions:

  • cqw: one percent of the container’s width.
  • cqh: one percent of its height.
  • cqi: one percent of its inline size.
  • cqb: one percent of its block size.
  • cqmin and cqmax: the smaller and larger, respectively, of cqi and cqb.

If no eligible container exists for the relevant axis, a container query length unit falls back to the corresponding small viewport unit. These units are useful for locally scaling type or spacing, but they are optional: use them only when fluid sizing suits the component. MDN documents the units in its container query guide.

Understand support for newer query types

MDN describes the established @container feature as widely available across browsers since February 2023, while cautioning that support varies for some parts. That broad availability statement should not be taken as a guarantee for every newer query type or syntax. Check the current compatibility data for the particular browsers and feature you plan to use.

Container queries include more than size queries: MDN’s current guide lists size, style, name-only, scroll-state, and anchored query types. Style queries are a separate feature family, not a way to inspect arbitrary computed CSS declarations. MDN’s guide discusses custom-property style queries and states that ordinary declaration/property checks through style() are not supported in any browser covered by that page. Its note about custom-property-only support is dated February 2024, so check the current compatibility information rather than treating that note as a current browser-version audit. Consult MDN’s container size and style query guide and the at-rule reference.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Troubleshoot common container-query problems

  • The query has no effect: confirm that an ancestor of the styled element has an eligible container-type. A query cannot style the container based on itself; the rule targets its descendants.
  • The wrong region triggers the rule: an unnamed query selects the nearest eligible ancestor. Add a container-name and refer to it in the @container condition.
  • The container becomes unexpectedly small or collapses: check whether it uses container-type: size. Size containment prevents its children from determining its dimensions; provide sizing through the layout context or an explicit constraint, or use inline-size if only inline-axis queries are needed.
  • A query or unit seems unsupported: verify the exact syntax and query family in the browser versions you target. Support for the established size-query feature does not establish support for every newer query type.
  • Container-relative sizing falls back unexpectedly: confirm there is an eligible query container for the axis used by the unit; without one, the unit uses the corresponding small viewport unit.

Or skip the browser setup

For a screenshot of a page demonstrating your component, you can use ScreenshotNeo’s API instead of setting up a browser capture script. This one-call cURL request saves a WebP screenshot; replace the URL with the page you want to capture and use your API key.

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

See the ScreenshotNeo API documentation for request options. ScreenshotNeo removes cookie banners, newsletter popups, and chat widgets before the shot; bot checks, blank pages, and failed loads are never billed. Its MCP server lets AI agents take screenshots. The free plan includes 1,000 screenshots a month with no card, and paid plans start at $5 for 3,000. For the product details, visit ScreenshotNeo.

Sign up free for 1,000 screenshots a month, with no card.

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.

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

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. Shenzhen desk3 min
    HONOR Expands Beyond Smartphones With Humanoid Robot RevealHONOR said it unveiled its first humanoid robot at MWC 2026 and named shopping assistance, workplace inspections, and supportive companionship as intended uses. Later Robotics D1 claims and a reported…
  2. Cupertino desk5 min
    Apple Unveils AirPods Max 2: The Upgrade That Should Have Happened Years AgoAirPods Max 2 adds H2-powered audio features and Apple claims up to 1.5× more effective ANC, but its design, Smart Case, and 20-hour battery rating are unchanged. Wired lossless audio…
  3. Cupertino desk4 min
    Apple’s OLED Touch MacBooks Are Coming—but the Dynamic Island Is the Real GambleApple has not announced an OLED touchscreen MacBook, but reports point to high-end models arriving in late 2026 or early 2027. The reported Mac Dynamic Island could be useful, but…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

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.