UtilityKit

500+ fast, free tools. Most run in your browser only; Image & PDF tools upload files to the backend when you run them.

Container Query Helper

Author @container boilerplate with names and breakpoints for component-driven responsive CSS.

About Container Query Helper

Media queries style elements based on the viewport width, which works for page-level layouts but breaks down for design-system components used in many different contexts. A card component in a narrow sidebar needs different styling than the same card in a three-column main grid — but viewport width does not distinguish between these contexts. CSS container queries solve this by letting elements respond to the size of their direct container instead. The Container Query Helper generates the container-type declaration on the parent and the corresponding @container rule for the child, with threshold pickers for min-width and max-width breakpoints. Resize the live demo container to see the query trigger in real time before copying the CSS block.

Why use Container Query Helper

Component-Level

Container queries respond to the parent element's size rather than the viewport. The same card component adapts its layout in a 240px sidebar and a 960px main area independently, without separate media query breakpoints for every layout context where the component might appear.

Container Type

container-type: inline-size enables width-based queries on the parent without triggering full layout containment. container-type: size enables width and height queries but requires the element to have explicit dimensions. The helper explains the tradeoff and sets the right type for your use case.

Threshold Picker

Set min-width for styles that apply when the container is wider than the threshold, or max-width for styles that apply when the container is narrower. Range syntax (@container (200px <= width <= 400px)) covers interval-based responsive steps for multi-context components.

Reusable Components

Container queries make design-system components truly self-contained. A product card with a compact and expanded layout can ship as a single component that detects its own context, rather than requiring the consuming layout to know about the component's internal breakpoints.

Live Demo

A resizable demo shows the container query triggering in real time as you drag the resize handle. This is the most critical feature — container queries often behave differently than expected when first written, and testing against a live resizable container catches issues immediately.

Copy Block

The output includes both the parent container declaration and the child @container rule as a complete paired block. Container queries require both halves — a standalone @container rule with no container-type on the parent silently does nothing.

How to use Container Query Helper

  1. Set container-type on the parent element — choose inline-size for width-only queries or size for both dimensions
  2. Optionally enter a container-name if you are nesting multiple query containers and need to reference a specific ancestor
  3. Choose a threshold type: min-width or max-width and set the breakpoint value in pixels
  4. Write the CSS properties that should apply inside the @container rule body
  5. Drag the live demo container's resize handle to verify the query triggers at the expected width
  6. Click Copy CSS to export the container declaration and @container query block together

When to use Container Query Helper

  • When a card component needs to switch from single-column to two-column layout based on the space available in its parent, not the viewport
  • When building a design-system component library where each component should adapt to its context without relying on global breakpoints
  • When a sidebar widget needs to hide secondary information when the sidebar is narrow but show it when the sidebar expands
  • When creating a responsive data table that switches from full-column view to stacked compact rows based on its container width
  • When a navigation component is reused in both a narrow mobile drawer and a wide top bar and needs different styling in each
  • When replacing an intrusive media query that was added to a shared component just to handle one specific layout context

Examples

Card switches to two-column at 400px

Input: container-type: inline-size, query: min-width 400px, apply grid-template-columns: 1fr 1fr

Output: .card { container-type: inline-size; container-name: card; } @container card (min-width: 400px) { .card-inner { grid-template-columns: 1fr 1fr; } }

Sidebar widget hides subtitle below 240px

Input: container-type: inline-size, query: max-width 240px, hide .subtitle

Output: .widget { container-type: inline-size; } @container (max-width: 240px) { .widget .subtitle { display: none; } }

Larger heading above 600px

Input: container-type: inline-size, query: min-width 600px, font-size 2rem

Output: .parent { container-type: inline-size; } @container (min-width: 600px) { .heading { font-size: 2rem; } }

Tips

  • The element you query must declare container-type — a @container rule with no corresponding container-type on the ancestor silently does nothing with no error message
  • Use container-type: inline-size for the vast majority of width-responsive components — container-type: size forces explicit height constraints that most components do not need
  • Name your containers whenever you nest multiple query containers to guarantee you are querying the intended ancestor rather than a closer one you did not expect
  • Container queries are ideal for design-system components shipped as a package — they let consumers drop components anywhere without worrying about breakpoints
  • Use cqi units (container query inline size) to scale typography and spacing relative to the container, matching the same mental model as vw but at the component level

Frequently Asked Questions

What is a CSS container query?
A container query is a CSS rule that applies styles to an element based on the size of its nearest ancestor that has declared container-type. Unlike media queries that use viewport dimensions, container queries use the containing element's dimensions, enabling component-level responsive design.
How is @container different from @media?
@media queries respond to the viewport size (or device capabilities). @container queries respond to the size of a specific ancestor element that has container-type set. Container queries enable the same component to adapt differently based on where it is placed, not on the screen size.
What does container-type: inline-size do?
inline-size establishes the element as a query container for its inline dimension (width in horizontal writing modes). It applies only width-based containment without triggering height containment, avoiding the layout restrictions of container-type: size.
Why doesn't my container query trigger?
The most common cause is a missing container-type declaration on the parent. Without container-type, the element is not a query container and @container rules targeting it are silently ignored. Ensure the direct ancestor (or named ancestor) has container-type: inline-size or size.
Can I name multiple containers?
Yes. Use container-name: name on each container element and reference the name in the @container rule: @container name (min-width: 400px). Without a name, @container queries the nearest ancestor with container-type set, which may not be the intended element in nested layouts.
Which browsers support container queries?
Container queries are supported in Chrome 105+, Firefox 110+, Safari 16+, and Edge 105+. Global support exceeds 90% as of 2025. They are safe to use in production for modern web apps and progressive enhancement contexts.
Should I replace media queries with container queries?
Not entirely. Media queries remain appropriate for page-level layout shifts like switching from a one-column to three-column grid at a viewport breakpoint. Container queries are better for component-level adaptations. The two work together: use media queries for scaffolding, container queries for components.
Are container query units like cqw and cqi safe to use?
cqw (1% of query container width), cqi (1% of inline size), cqb, cqh, and cqmax/cqmin are supported in all browsers that support container queries. Use cqi rather than cqw in most cases since inline-size is the more commonly established container dimension.

Explore the category

Glossary

Container Query
A CSS rule using @container that applies styles based on the dimensions of a parent element that has container-type set, enabling component-level responsive design independent of the viewport.
Container Type
The container-type property on a parent element that designates it as a query container. Values are inline-size (width queries only), size (width and height), or normal (not a container).
inline-size
A container-type value that restricts size containment to the inline dimension (width in horizontal writing). Enables width-based container queries without requiring the element to have a fixed height.
Containment
A CSS concept where an element's layout, paint, or size is isolated from the rest of the document. container-type establishes size containment as a prerequisite for @container queries.
Media Query
A CSS conditional rule that applies styles based on viewport dimensions or device characteristics. Used for page-level layout shifts; container queries handle component-level adaptations.
Query Container
An element with container-type set that becomes the dimensional reference for @container rules targeting its descendants.