CSS @container At-Rule

Beginner
6 examples
Updated: Nov 2026

Definition and Usage

@container conditionally applies styles according to a containing element instead of the viewport. This lets one card, toolbar, or navigation component adapt wherever it is placed.

Create the container first.

For width queries, set container-type: inline-size on an ancestor. The query styles descendants.

CSS Syntax

syntax.css
.wrapper { container-type: inline-size; }
@container (width >= 30rem) {
  .card { grid-template-columns: 1fr 2fr; }
}

Query Forms

FormPurposeTry it
minimum widthEnhance when the container is wide enoughTry it
maximum widthSimplify in a narrow containerTry it
named containerTarget a specific ancestorTry it
range syntaxLimit a layout to a width intervalTry it
container unitsSize content with cqi unitsTry it
nested containersUse the nearest matching query containerTry it

CSS Examples

Responsive card

card.css
.card-shell { container-type: inline-size; }
@container (min-width: 30rem) {
  .card { display: grid; grid-template-columns: 10rem 1fr; }
}
Try It Yourself

Named query container

named.css
.sidebar { container: sidebar / inline-size; }
@container sidebar (width > 20rem) {
  .nav-label { display: inline; }
}
Try It Yourself

Container query units

units.css
.hero-title { font-size: clamp(1.5rem, 8cqi, 4rem); }
Try It Yourself

Browser Support

Size container queries are browser layout features.

✓ Baseline · Widely available

@container

Inline-size queries work in current Chrome, Edge, Firefox, Safari, and Opera.

94% Modern browsers
Google Chrome 105+
Full support
Mozilla Firefox 110+
Full support
Apple Safari 16+
Full support
Microsoft Edge 105+
Full support
@container Widely supported

Bottom line: Use size queries in current browsers. Keep a one-column baseline outside the query for older clients.

Frequently Asked Questions

It conditionally styles descendants based on a query container's size, style, or scroll state, depending on browser support and query type.
Set container-type: inline-size on an ancestor, optionally with container-name.
A size query normally styles descendants, not the element whose dimensions are being queried.
Container queries respond to a component's available space; media queries respond to the viewport or device environment.
Current Chrome, Edge, Firefox, Safari, and Opera support inline-size container queries.

Next: @namespace

Declare namespaces for SVG and XML selectors.

@namespace tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful