CSS container Property

The container property is a shorthand that defines an element's container name and container query type, allowing descendant elements to respond to the size of their containing component.

It combines the container-name and container-type properties into a single declaration. When a size query container is established, descendants can use @container rules to change their styles based on the container's dimensions rather than the viewport.

Container queries are useful for reusable cards, navigation panels, dashboards, and other components that may appear in different-sized areas of a page.

container Syntax

The container shorthand accepts a container name, optionally followed by a slash and a container type.

selector {
  container: card / inline-size;
}

General Syntax

selector {
  container: <container-name> [ / <container-type> ]?;
}

Named Inline-Size Container

.card-wrapper {
  container: card / inline-size;
}

Named Size Container

.panel-wrapper {
  container: panel / size;
}

Container Name Only

.component-wrapper {
  container: component;
}

When the type is omitted, container-type is reset to normal.

Unnamed Container

.component-wrapper {
  container: none / inline-size;
}

Multiple Container Names

.component-wrapper {
  container: card layout / inline-size;
}

CSS-Wide Keywords

.component-wrapper {
  container: inherit;
}
.component-wrapper {
  container: initial;
}
.component-wrapper {
  container: unset;
}
.component-wrapper {
  container: revert;
}
.component-wrapper {
  container: revert-layer;
}

CSS-wide keywords must be used alone. The slash separates container names from the container type and is required when specifying both.

Property Values

The shorthand accepts container names and the container types defined by container-type.

Value Description
none Assigns no container name and resets the container type to normal.
card Assigns the name card and uses the default normal container type.
card / inline-size Names the container card and enables size queries along its logical inline dimension.
card / size Names the container card and enables size queries along both logical dimensions.
card / normal Names the container without enabling size queries.
none / inline-size Creates an unnamed inline-size query container.
none / size Creates an unnamed size query container for both dimensions.
card layout / inline-size Assigns two container names and enables inline-size queries.

Property Characteristics

Characteristic Value
Property type Shorthand
Initial value none for container-name and normal for container-type
Inherited No
Applies to All elements
Longhand properties container-name and container-type
Animation type As defined for the individual longhand properties

The container shorthand defines query container characteristics. It does not directly set the element's width, height, or layout mode.

How It Works

Container queries allow descendants to respond to the dimensions of an ancestor container instead of the viewport.

Step 1: Create a Container

<div class="card-wrapper">
  <div class="card">
    <h3>CSS Reference</h3>
    <p>Explore CSS properties and examples.</p>
  </div>
</div>

Step 2: Define the Query Container

.card-wrapper {
  container: card / inline-size;
}

The wrapper becomes a named query container that supports queries based on its inline size.

Step 3: Define Default Styles

.card {
  padding: 15px;
  background-color: #f1f5f9;
}
.card h3 {
  font-size: 1.125rem;
}

Step 4: Add a Container Query

@container card (min-width: 400px) {
  .card {
    padding: 25px;
  }
  .card h3 {
    font-size: 1.5rem;
  }
}

Step 5: Respond to Container Size

When the named container is at least 400 pixels wide, the query applies the larger padding and heading size to the descendant card.

Important Distinction

The @container rule styles descendants of the selected query container. It does not use the query to style that same container element directly.

Shorthand Behavior

The container property sets two longhand properties:

  • container-name
  • container-type

Equivalent Longhand Declarations

.card-wrapper {
  container-name: card;
  container-type: inline-size;
}

Equivalent Shorthand

.card-wrapper {
  container: card / inline-size;
}

Omitting the Type

.card-wrapper {
  container: card;
}

This sets container-name: card and container-type: normal.

Resetting Existing Values

.card-wrapper {
  container-name: card;
  container-type: inline-size;
  container: panel;
}

The final declaration changes the name to panel and resets the type to normal.

Overriding the Type

.card-wrapper {
  container: card / inline-size;
  container-type: size;
}

The name remains card, while the container type becomes size.

Resetting Both Longhands

.card-wrapper {
  container: none;
}

This resets the container name to none and the container type to normal.

Container Names

Container names identify specific query containers and allow @container rules to select an appropriate ancestor.

Named Container

.card-wrapper {
  container: card / inline-size;
}

Querying by Name

@container card (min-width: 400px) {
  .card {
    display: grid;
    grid-template-columns: 1fr 2fr;
  }
}

Different Container Names

.sidebar {
  container: sidebar / inline-size;
}
.main-content {
  container: content / inline-size;
}

Separate Named Queries

@container sidebar (min-width: 250px) {
  .sidebar-card {
    padding: 20px;
  }
}
@container content (min-width: 600px) {
  .content-card {
    display: grid;
    grid-template-columns: 1fr 2fr;
  }
}

Naming Rules

Container names use CSS identifiers. They cannot be CSS-wide keywords or the reserved name none.

Practical Consideration

Choose descriptive names such as card, sidebar, or content to make container queries easier to understand and maintain.

Container Types

The container-type portion determines which size dimensions can be queried.

normal

.component-wrapper {
  container: component / normal;
}

The normal value does not establish a size query container. The element can still be eligible for style queries and scroll-state queries where supported.

inline-size

.component-wrapper {
  container: component / inline-size;
}

The inline-size value enables size queries along the logical inline dimension.

size

.component-wrapper {
  container: component / size;
}

The size value enables size queries along both logical inline and block dimensions.

Comparison

Type Inline Size Queries Block Size Queries
normal No No
inline-size Yes No
size Yes Yes

Recommended Starting Point

Use inline-size for most responsive components that need to respond to available horizontal space in a standard horizontal writing mode.

Use size when queries must evaluate both dimensions and the layout can accommodate the additional size containment.

Container Queries

The @container at-rule applies CSS declarations when a selected ancestor container meets specified conditions.

Minimum Width

@container (min-width: 400px) {
  .card {
    padding: 25px;
  }
}

Maximum Width

@container (max-width: 300px) {
  .card {
    padding: 10px;
  }
}

Range Syntax

@container (width >= 400px) {
  .card {
    display: grid;
    grid-template-columns: 1fr 2fr;
  }
}

Width Range

@container (300px <= width <= 600px) {
  .card {
    gap: 20px;
  }
}

Logical Inline Size

@container (inline-size >= 400px) {
  .card {
    padding: 25px;
  }
}

Height Queries

.panel-wrapper {
  container: panel / size;
}
@container panel (min-height: 300px) {
  .panel-content {
    padding: 30px;
  }
}

Height queries require a container that supports querying the corresponding dimension, such as container-type: size in horizontal writing mode.

Combining Conditions

@container (min-width: 400px) and (max-width: 700px) {
  .card {
    grid-template-columns: 1fr 2fr;
  }
}

Container queries evaluate the selected ancestor container rather than the viewport.

Named Container Queries

Named queries select an ancestor with a matching container name that supports the requested query features.

Define a Named Container

.dashboard-wrapper {
  container: dashboard / inline-size;
}

Query the Named Container

@container dashboard (min-width: 600px) {
  .dashboard-card {
    grid-template-columns: 1fr 2fr;
  }
}

Multiple Named Containers

.sidebar-wrapper {
  container: sidebar / inline-size;
}
.content-wrapper {
  container: content / inline-size;
}

Independent Queries

@container sidebar (min-width: 250px) {
  .sidebar-card {
    padding: 20px;
  }
}
@container content (min-width: 500px) {
  .content-card {
    padding: 30px;
  }
}

When to Use

Named queries are useful when a component has multiple container ancestors or when the stylesheet must target a specific layout region.

Unnamed Container Queries

Container queries do not require names. An unnamed query selects the nearest ancestor that supports the requested query features.

Unnamed Inline-Size Container

.card-wrapper {
  container: none / inline-size;
}

Query Without a Name

@container (min-width: 400px) {
  .card {
    padding: 25px;
  }
}

Using the Longhand

.card-wrapper {
  container-type: inline-size;
}

This also creates an unnamed inline-size query container.

Nearest Eligible Ancestor

If multiple ancestors establish size query containers, an unnamed size query selects the nearest ancestor that supports the requested features.

When to Use

Unnamed containers are convenient for simple components where the nearest eligible ancestor is the intended query target.

Multiple Container Names

A single element can have multiple container names separated by spaces.

Assign Multiple Names

.component-wrapper {
  container: card layout / inline-size;
}

Query the First Name

@container card (min-width: 400px) {
  .card-content {
    padding: 25px;
  }
}

Query the Second Name

@container layout (min-width: 600px) {
  .card-content {
    display: grid;
    grid-template-columns: 1fr 2fr;
  }
}

Equivalent Longhand

.component-wrapper {
  container-name: card layout;
  container-type: inline-size;
}

Practical Consideration

Multiple names allow one container to participate in different groups of named queries without creating additional wrapper elements.

Nested Containers

Container queries can be used with nested components. Each query selects an appropriate ancestor based on the name and required query features.

Nested HTML Structure

<div class="page-layout">
  <div class="card-wrapper">
    <div class="card">
      <p>CSS Reference</p>
    </div>
  </div>
</div>

Define Both Containers

.page-layout {
  container: page / inline-size;
}
.card-wrapper {
  container: card / inline-size;
}

Query the Outer Container

@container page (min-width: 800px) {
  .card {
    font-size: 1.125rem;
  }
}

Query the Inner Container

@container card (min-width: 350px) {
  .card {
    padding: 25px;
  }
}

Unnamed Query

@container (min-width: 350px) {
  .card {
    border-width: 2px;
  }
}

For the card in this example, the unnamed size query selects the nearest eligible container, .card-wrapper.

Practical Consideration

Use descriptive container names when nested components need to respond to different ancestors.

Container Query Length Units

Container query length units allow dimensions to be calculated relative to the size of an eligible query container.

Unit Meaning
cqw 1% of the query container's width.
cqh 1% of the query container's height.
cqi 1% of the query container's logical inline size.
cqb 1% of the query container's logical block size.
cqmin The smaller of cqi and cqb.
cqmax The larger of cqi and cqb.

Container-Relative Font Size

.card-wrapper {
  container: card / inline-size;
}
.card h3 {
  font-size: clamp(1rem, 4cqi, 2rem);
}

Container-Relative Padding

.card {
  padding: 5cqi;
}

Container-Relative Gaps

.card-grid {
  gap: 2cqi;
}

Container-Relative Width

.card-image {
  width: 50cqi;
}

Fallback Behavior

If no eligible query container exists for the requested dimension, container query length units fall back to the corresponding small viewport dimensions.

Practical Consideration

Use container query units for component-level scaling and @container rules when styles need to change at specific size thresholds.

Container Queries vs. Media Queries

Container queries and media queries both support responsive design, but they evaluate different conditions.

Media Query

@media (min-width: 800px) {
  .card {
    display: grid;
    grid-template-columns: 1fr 2fr;
  }
}

This rule responds to the viewport width.

Container Query

.card-wrapper {
  container: card / inline-size;
}
@container card (min-width: 400px) {
  .card {
    display: grid;
    grid-template-columns: 1fr 2fr;
  }
}

This rule responds to the width of the selected card container.

Comparison

Feature Media Queries Container Queries
Query target Viewport or device environment Eligible ancestor container
Typical use Page-level responsive layouts Component-level responsive layouts
Requires a size query container No Yes, for size queries
Reusable components Depends on viewport conditions Can adapt to the space provided by a parent component

Using Both

@media (max-width: 700px) {
  .page-layout {
    grid-template-columns: 1fr;
  }
}
@container card (min-width: 400px) {
  .card {
    grid-template-columns: 1fr 2fr;
  }
}

Media queries can control the overall page layout while container queries control how individual components adapt within that layout.

Size Containment

Size query containers apply containment to prevent circular dependencies between a container's dimensions and the styles of its descendants.

Inline-Size Query Container

.card-wrapper {
  container: card / inline-size;
}

This establishes containment of intrinsic sizing along the inline dimension, along with the containment behavior required for size queries.

Two-Dimensional Size Query Container

.panel-wrapper {
  container: panel / size;
}

This applies full size containment, preventing descendants from contributing to the container's intrinsic sizing in either dimension.

Why Containment Matters

If a container's size depended on its descendants while those descendants changed styles based on the container's size, a circular sizing dependency could occur.

Potential Layout Effects

Full size containment can cause an automatically sized container to lose its normal content-based dimensions.

Providing Dimensions

.panel-wrapper {
  container: panel / size;
  min-height: 250px;
}

Intrinsic Size Fallbacks

.panel-wrapper {
  container: panel / size;
  contain-intrinsic-size: 400px 250px;
}

Intrinsic size fallback properties can supply substitute intrinsic dimensions when size containment applies.

Practical Consideration

Use inline-size unless queries require the block dimension. It generally allows more natural content-based sizing in the block direction.

Responsive Components

Container queries make it possible to build components that adapt to the space available in different parts of a page.

Card Layout

.card-wrapper {
  container: card / inline-size;
}
.card {
  display: block;
}
@container card (min-width: 450px) {
  .card {
    display: grid;
    grid-template-columns: 1fr 2fr;
  }
}

Responsive Navigation

.navigation-wrapper {
  container: navigation / inline-size;
}
.navigation-links {
  display: flex;
  flex-direction: column;
}
@container navigation (min-width: 500px) {
  .navigation-links {
    flex-direction: row;
  }
}

Dashboard Widgets

.dashboard-widget {
  container: widget / inline-size;
}
.widget-content {
  display: block;
}
@container widget (min-width: 350px) {
  .widget-content {
    display: grid;
    grid-template-columns: 1fr 1fr;
  }
}

Responsive Typography

.content-wrapper {
  container: content / inline-size;
}
.content-title {
  font-size: clamp(1.25rem, 4cqi, 2rem);
}

Reusable Components

A component can use the same stylesheet in a sidebar, main content area, or dashboard without relying entirely on viewport-based breakpoints.

Practical Uses

The container shorthand is useful wherever components need to adapt independently of the viewport.

Documentation Cards

.documentation-card-wrapper {
  container: documentation-card / inline-size;
}
@container documentation-card (min-width: 400px) {
  .documentation-card {
    display: grid;
    grid-template-columns: 1fr 2fr;
  }
}

Sidebar Components

.sidebar {
  container: sidebar / inline-size;
}
@container sidebar (min-width: 300px) {
  .sidebar-content {
    padding: 25px;
  }
}

Search Forms

.search-wrapper {
  container: search / inline-size;
}
.search-form {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
@container search (min-width: 450px) {
  .search-form {
    flex-direction: row;
  }
}

Content Panels

.content-panel {
  container: panel / inline-size;
}
@container panel (min-width: 600px) {
  .panel-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
  }
}

When Not to Use

Container queries may be unnecessary when a component does not need container-dependent styling or when simple fluid sizing and existing layout techniques already meet the design requirements.

container Example

The following example uses two containers with different widths. Both contain the same card component, but only the wider container satisfies the 400-pixel container query.

The card changes from a vertical layout to a two-column layout when its container is wide enough.

<div class="container-demo">
  <div class="demo-group">
    <h3>Narrow Container</h3>
    <div class="card-wrapper narrow">
      <div class="demo-card">
        <div class="card-icon">CSS</div>
        <div class="card-content">
          <h4>CSS Reference</h4>
          <p>Explore properties and examples.</p>
        </div>
      </div>
    </div>
  </div>
  <div class="demo-group">
    <h3>Wide Container</h3>
    <div class="card-wrapper wide">
      <div class="demo-card">
        <div class="card-icon">CSS</div>
        <div class="card-content">
          <h4>CSS Reference</h4>
          <p>Explore properties and examples.</p>
        </div>
      </div>
    </div>
  </div>
</div>
.container-demo {
  display: grid;
  gap: 20px;
}
.container-demo .demo-group {
  padding: 15px;
  border: 1px solid #cccccc;
  background-color: #f8f9fa;
}
.container-demo h3 {
  margin: 0 0 12px;
  font-size: 1.125rem;
}
.container-demo .card-wrapper {
  container: card / inline-size;
  max-width: 100%;
}
.container-demo .narrow {
  width: 280px;
}
.container-demo .wide {
  width: 550px;
}
.container-demo .demo-card {
  display: grid;
  gap: 15px;
  padding: 15px;
  border: 2px solid #336699;
  background-color: #ffffff;
}
.container-demo .card-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 80px;
  background-color: #336699;
  color: #ffffff;
  font-weight: bold;
}
.container-demo .card-content h4 {
  margin: 0 0 8px;
  font-size: 1.125rem;
}
.container-demo .card-content p {
  margin: 0;
}
@container card (min-width: 400px) {
  .container-demo .demo-card {
    grid-template-columns: 120px 1fr;
    align-items: center;
  }
}
Browser Result

Narrow Container

CSS

CSS Reference

Explore properties and examples.

Wide Container

CSS

CSS Reference

Explore properties and examples.

The narrow container is 280 pixels wide, so the card retains its default single-column layout.

The wide container is 550 pixels wide, satisfying @container card (min-width: 400px). Its card changes to a two-column layout.

Both cards use the same HTML structure and CSS rules. Their different layouts are determined by their container widths rather than the viewport width.

Accessibility

Container queries can improve responsive layouts by adapting components to the space available, but accessibility still depends on appropriate HTML structure and usable styling.

Preserve Source Order

Use meaningful HTML source order so content remains understandable when layouts change between columns and rows.

Support Text Resizing

Test browser zoom and enlarged text. Components may need to change layouts at different container widths when text occupies more space.

Avoid Content Overflow

Use flexible tracks, appropriate minimum sizes, and responsive spacing to prevent content from overflowing narrow containers.

Maintain Keyboard Navigation

Ensure responsive layout changes do not create a confusing keyboard focus order.

Avoid Hiding Essential Content

Do not remove important information or controls solely because a container becomes narrow.

Practical Consideration

Test components in narrow sidebars, wide content areas, high zoom levels, and different writing modes.

Important Considerations

Keep the following points in mind when using the container property:

  • Property type: Shorthand for container-name and container-type.
  • Initial values: none for the name and normal for the type.
  • Inheritance: Neither longhand property is inherited.
  • Container names: Identify containers for named queries.
  • Multiple names: A container can have several space-separated names.
  • Slash separator: Required between names and an explicitly supplied container type.
  • normal: Does not enable size queries.
  • inline-size: Enables queries along the logical inline dimension.
  • size: Enables queries along both logical dimensions.
  • Unnamed queries: Select the nearest eligible ancestor supporting the requested features.
  • Named queries: Select an appropriate ancestor with the specified name.
  • Descendant styling: A container cannot style itself through a size query that selects that same element.
  • Nested containers: Different queries can target different ancestor containers.
  • Size containment: Prevents circular dependencies between container dimensions and descendant styling.
  • Content-based sizing: Full size containment can affect automatic intrinsic dimensions.
  • Container query units: Allow lengths to respond to eligible container dimensions.
  • Media queries: Remain useful for viewport-level responsive design.
  • Responsive components: Can adapt to different layout regions without relying entirely on viewport breakpoints.
  • Accessibility: Preserve reading order, keyboard navigation, and content usability.

Use container: name / inline-size as a practical starting point for reusable components that need to respond to their available inline space.

Browser Support

The CSS container shorthand and size container queries have widespread support in modern browsers, including Chrome, Edge, Firefox, and Safari.

Support for newer container query features, including some style queries and scroll-state queries, may vary between browsers and versions.

Basic Example

.card-wrapper {
  container: card / inline-size;
}
@container card (min-width: 400px) {
  .card {
    padding: 25px;
  }
}

Feature Detection

@supports (container-type: inline-size) {
  .card-wrapper {
    container: card / inline-size;
  }
}

Feature queries can detect support for the required container type, but they do not guarantee support for every newer container query feature.

For current compatibility information, visit Can I Use? and search for container queries.

Specifications

The container shorthand is defined in CSS Containment Module Level 3.

CSS Containment Module Level 3

The specification defines container names, query container types, container queries, and container query length units.