CSS container-type Property
The container-type property establishes whether an element can serve as a size query container, allowing its descendants to respond to the container's dimensions.
It determines which dimensions can be evaluated by CSS @container size queries. With an appropriate container type, components can adapt to the available space inside their parent containers rather than relying entirely on viewport dimensions.
The property supports three values: normal, inline-size, and size. Each value determines the element's size query capabilities and associated containment behavior.
container-type Syntax
The container-type property accepts one of three keywords that determine whether an element supports size container queries.
selector {
container-type: inline-size;
}
General Syntax
selector {
container-type: normal | size | inline-size;
}
Normal Container
.component-wrapper {
container-type: normal;
}
Inline-Size Container
.component-wrapper {
container-type: inline-size;
}
Size Container
.component-wrapper {
container-type: size;
}
CSS-Wide Keywords
.component-wrapper {
container-type: inherit;
}
.component-wrapper {
container-type: initial;
}
.component-wrapper {
container-type: unset;
}
.component-wrapper {
container-type: revert;
}
.component-wrapper {
container-type: revert-layer;
}
The property accepts one container type at a time. Values such as inline-size size are invalid.
Property Values
| Value | Description |
|---|---|
normal |
Does not establish a size query container. The element can still be eligible for supported style and scroll-state queries. |
inline-size |
Establishes a query container for the logical inline dimension and applies the required containment. |
size |
Establishes a query container for both logical dimensions and applies full size containment. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | normal |
| Inherited | No |
| Applies to | All elements |
| Computed value | Specified keyword |
| Animation type | Discrete |
| Percentages | Not applicable |
| Shorthand | container |
The property establishes size query capabilities but does not assign a container name. Use container-name when a query must target a specific named ancestor.
How It Works
Container size queries allow descendant styles to respond to the dimensions of an eligible ancestor.
Step 1: Create a Component
<div class="card-wrapper">
<div class="card">
<h3>CSS Reference</h3>
<p>Explore CSS properties and examples.</p>
</div>
</div>
Step 2: Establish a Size Query Container
.card-wrapper {
container-type: inline-size;
}
The wrapper now supports queries along its logical inline dimension.
Step 3: Define Default Styles
.card {
padding: 15px;
background-color: #f1f5f9;
}
Step 4: Add a Container Query
@container (min-width: 400px) {
.card {
padding: 25px;
}
}
Step 5: Respond to Available Space
When the selected query container is at least 400 pixels wide, the card receives the larger padding.
Important Distinction
The query evaluates the container's dimensions rather than the viewport. A container query generally styles descendants of the selected container, not the container itself.
The normal Value
The normal value is the initial value of container-type. It does not enable size queries.
.component-wrapper {
container-type: normal;
}
No Size Query Capability
.component-wrapper {
container-type: normal;
}
@container (min-width: 400px) {
.component-content {
padding: 25px;
}
}
The wrapper is not eligible for this width query. The query may instead select another eligible ancestor if one exists.
Named Normal Container
.component-wrapper {
container-name: component;
container-type: normal;
}
The element has the name component, but it still does not support size queries.
Style Queries
.component-wrapper {
container-type: normal;
--theme: dark;
}
@container style(--theme: dark) {
.component-content {
color: #ffffff;
background-color: #222222;
}
}
Supported custom property style queries can select a normally typed ancestor without requiring size containment.
When to Use
Use normal when size queries are unnecessary or when removing previously applied size containment.
The inline-size Value
The inline-size value establishes a query container for the logical inline dimension.
.card-wrapper {
container-type: inline-size;
}
Width Query
@container (min-width: 400px) {
.card {
display: grid;
grid-template-columns: 1fr 2fr;
}
}
Logical Inline-Size Query
@container (inline-size >= 400px) {
.card {
padding: 25px;
}
}
Maximum Width Query
@container (max-width: 300px) {
.card {
padding: 10px;
}
}
Block Dimension
An inline-size query container does not support size queries along its logical block dimension.
Content-Based Height
In horizontal writing mode, inline-size containment generally allows the container's height to remain dependent on its content.
Typical Use
This is the most common value for responsive cards, navigation panels, search forms, and reusable components that need to adapt to available width.
The size Value
The size value establishes a query container for both logical dimensions.
.panel-wrapper {
container-type: size;
}
Width Query
@container (min-width: 400px) {
.panel-content {
padding: 25px;
}
}
Height Query
@container (min-height: 300px) {
.panel-content {
display: grid;
grid-template-rows: auto 1fr;
}
}
Combined Width and Height Query
@container (min-width: 400px) and (min-height: 300px) {
.panel-content {
padding: 30px;
}
}
Provide Dimensions
.panel-wrapper {
container-type: size;
width: 500px;
height: 350px;
max-width: 100%;
}
Full Size Containment
The size value applies full size containment, so descendant content does not determine the container's intrinsic dimensions.
Intrinsic Size Fallback
.panel-wrapper {
container-type: size;
contain-intrinsic-size: 400px 250px;
}
The contain-intrinsic-size property can provide substitute intrinsic dimensions when full size containment is applied.
When to Use
Use size when responsive behavior must depend on both dimensions and the container's sizing can be controlled appropriately.
Comparing Container Types
The three container types differ in their size query capabilities and containment behavior.
| Feature | normal |
inline-size |
size |
|---|---|---|---|
| Inline size queries | No | Yes | Yes |
| Block size queries | No | No | Yes |
| Inline-size containment | No | Yes | Yes |
| Full size containment | No | No | Yes |
| Typical use | Normal elements and supported style queries | Responsive components | Two-dimensional responsive regions |
Recommended Starting Point
.component-wrapper {
container-type: inline-size;
}
For most responsive components in horizontal writing mode, inline-size provides the required width query capability without imposing full size containment.
Size Containment
Size containment prevents circular dependencies between an element's dimensions and the styles of its descendants.
Why Containment Is Required
A container query can change descendant styles based on the container's size. Without containment, those descendant styles could change the container's intrinsic size, potentially creating a circular dependency.
Inline-Size Containment
.card-wrapper {
container-type: inline-size;
}
This applies containment of intrinsic sizing along the logical inline dimension, along with the layout and style containment required for size queries.
Full Size Containment
.panel-wrapper {
container-type: size;
}
This applies full size containment, preventing descendant content from contributing to the container's intrinsic dimensions in either direction.
Layout Effects
Full size containment can cause automatically sized containers to have unexpected dimensions when no explicit or substitute intrinsic size is provided.
Providing a Minimum Height
.panel-wrapper {
container-type: size;
min-height: 250px;
}
Providing an Intrinsic Size
.panel-wrapper {
container-type: size;
contain-intrinsic-size: 400px 250px;
}
Practical Consideration
Use the least restrictive container type that supports the queries required by the component.
Intrinsic Sizing
Intrinsic sizing determines how an element's natural dimensions are influenced by its content.
Normal Content-Based Sizing
.panel {
container-type: normal;
}
The panel can normally use its descendants to determine intrinsic dimensions.
Inline-Size Containment
.panel {
container-type: inline-size;
}
The container's intrinsic inline size is independent of its descendants, while the block dimension can still depend on content.
Full Size Containment
.panel {
container-type: size;
}
Neither intrinsic dimension is determined by descendant content.
Intrinsic Size Fallback
.panel {
container-type: size;
contain-intrinsic-size: 400px 250px;
}
This provides substitute intrinsic inline and block sizes for the contained element.
Remembered Intrinsic Size
.panel {
container-type: size;
contain-intrinsic-size: auto 400px auto 250px;
}
The auto keywords allow a previously remembered natural size to be used when one is available, with the specified lengths serving as fallbacks.
Practical Consideration
When using container-type: size, consider how the container will obtain useful dimensions without relying on its descendants.
Writing Modes
The inline-size and size values use logical dimensions, which depend on the element's writing mode.
Horizontal Writing Mode
.component-wrapper {
writing-mode: horizontal-tb;
container-type: inline-size;
}
In horizontal writing mode, the inline dimension corresponds to width.
Vertical Writing Mode
.component-wrapper {
writing-mode: vertical-rl;
container-type: inline-size;
}
In vertical writing mode, the inline dimension corresponds to height.
Logical Inline-Size Query
@container (inline-size >= 400px) {
.component-content {
padding: 25px;
}
}
Logical Block-Size Query
.component-wrapper {
container-type: size;
}
@container (block-size >= 300px) {
.component-content {
padding: 30px;
}
}
Comparison
| Writing Mode | Inline Dimension | Block Dimension |
|---|---|---|
horizontal-tb |
Width | Height |
vertical-rl |
Height | Width |
vertical-lr |
Height | Width |
Logical size queries are useful when building components that must work across different writing modes.
Working with Container Names
The container-name property assigns one or more identifiers to an element so queries can target a specific ancestor.
Unnamed Container
.card-wrapper {
container-type: inline-size;
}
Named Container
.card-wrapper {
container-name: card;
container-type: inline-size;
}
Named Query
@container card (min-width: 400px) {
.card {
padding: 25px;
}
}
Multiple Names
.card-wrapper {
container-name: card layout;
container-type: inline-size;
}
Query Different Names
@container card (min-width: 400px) {
.card {
padding: 25px;
}
}
@container layout (min-width: 600px) {
.card {
display: grid;
grid-template-columns: 1fr 2fr;
}
}
Practical Consideration
Container names are optional for size queries. Use them when the query must select a specific ancestor rather than the nearest eligible container.
Working with the container Shorthand
The container shorthand combines container-name and container-type.
Longhand Properties
.card-wrapper {
container-name: card;
container-type: inline-size;
}
Equivalent Shorthand
.card-wrapper {
container: card / inline-size;
}
Multiple Names
.card-wrapper {
container: card layout / inline-size;
}
Unnamed Container
.card-wrapper {
container: none / inline-size;
}
Size Container
.panel-wrapper {
container: panel / size;
}
Shorthand Reset Behavior
.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.
Practical Consideration
Use the shorthand when defining names and types together. Use container-type directly when changing only the query type.
Container Queries
The @container at-rule applies descendant styles when the selected ancestor container satisfies 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 Query
.panel-wrapper {
container-type: size;
height: 350px;
}
@container (min-height: 300px) {
.panel-content {
padding: 30px;
}
}
Combined Conditions
@container (min-width: 400px) and (max-width: 700px) {
.card {
grid-template-columns: 1fr 2fr;
}
}
Important Distinction
The selected ancestor must support the dimensions requested by the query. An inline-size container cannot satisfy a query that requires its logical block size.
Container Selection
Container queries select an eligible ancestor based on the query's name and required features.
Nearest Eligible Container
.outer-wrapper {
container-type: inline-size;
}
.inner-wrapper {
container-type: inline-size;
}
@container (min-width: 400px) {
.card {
padding: 25px;
}
}
For a card nested inside both wrappers, the unnamed query selects the nearest eligible ancestor, .inner-wrapper.
Named Container Selection
.outer-wrapper {
container: page / inline-size;
}
.inner-wrapper {
container: card / inline-size;
}
@container page (min-width: 800px) {
.card {
font-size: 1.125rem;
}
}
The named query selects the outer page container.
Different Query Capabilities
.outer-wrapper {
container-type: size;
}
.inner-wrapper {
container-type: inline-size;
}
@container (min-height: 300px) {
.card {
padding: 30px;
}
}
In horizontal writing mode, the height query can select the outer wrapper because the inner wrapper does not support queries along its block dimension.
No Eligible Container
If no ancestor supports the requested query features, the size query does not match.
Practical Consideration
Use container names when nested components need to respond to specific ancestors.
Container Query Length Units
Container query length units allow CSS dimensions to be calculated relative to an eligible query container.
| Unit | Description |
|---|---|
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-type: inline-size;
}
.card h3 {
font-size: clamp(1rem, 4cqi, 2rem);
}
Container-Relative Padding
.card {
padding: 5cqi;
}
Container-Relative Gap
.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 fluid component sizing 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-type: inline-size;
}
@container (min-width: 400px) {
.card {
display: grid;
grid-template-columns: 1fr 2fr;
}
}
This rule responds to the width of the selected query 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 | Respond to viewport conditions | Respond to available container dimensions |
Using Both
@media (max-width: 700px) {
.page-layout {
grid-template-columns: 1fr;
}
}
@container (min-width: 400px) {
.card {
grid-template-columns: 1fr 2fr;
}
}
Media queries can control the overall page layout while container queries control individual components.
Responsive Components
The container-type property allows components to adapt to the space available in different layout regions.
Responsive Cards
.card-wrapper {
container-type: inline-size;
}
.card {
display: block;
}
@container (min-width: 450px) {
.card {
display: grid;
grid-template-columns: 1fr 2fr;
}
}
Responsive Navigation
.navigation-wrapper {
container-type: inline-size;
}
.navigation-links {
display: flex;
flex-direction: column;
}
@container (min-width: 500px) {
.navigation-links {
flex-direction: row;
}
}
Dashboard Widgets
.widget-wrapper {
container-type: inline-size;
}
.widget-content {
display: block;
}
@container (min-width: 350px) {
.widget-content {
display: grid;
grid-template-columns: 1fr 1fr;
}
}
Search Forms
.search-wrapper {
container-type: inline-size;
}
.search-form {
display: flex;
flex-direction: column;
gap: 10px;
}
@container (min-width: 450px) {
.search-form {
flex-direction: row;
}
}
Responsive Typography
.content-wrapper {
container-type: inline-size;
}
.content-title {
font-size: clamp(1.25rem, 4cqi, 2rem);
}
Practical Consideration
Container queries are especially useful for reusable components that appear in sidebars, main content areas, and dashboards with different available widths.
Practical Uses
Use container-type when a component needs to respond to the dimensions of an ancestor rather than the viewport.
Documentation Cards
.documentation-wrapper {
container-type: inline-size;
}
@container (min-width: 400px) {
.documentation-card {
display: grid;
grid-template-columns: 1fr 2fr;
}
}
Sidebar Panels
.sidebar-wrapper {
container-type: inline-size;
}
@container (min-width: 300px) {
.sidebar-panel {
padding: 25px;
}
}
Content Sections
.content-wrapper {
container-type: inline-size;
}
@container (min-width: 600px) {
.content-grid {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
}
}
Two-Dimensional Panels
.dashboard-panel {
container-type: size;
min-height: 300px;
}
@container (min-height: 250px) {
.panel-content {
padding: 30px;
}
}
When Not to Use
Size containment is unnecessary when components do not need size queries or container-relative units. Avoid applying container-type: size without considering its effects on intrinsic sizing.
container-type Example
This example demonstrates how container-type: inline-size allows the same card component to respond differently in narrow and wide containers.
The narrow container is 280 pixels wide, while the wide container is 550 pixels wide. A container query changes the card layout when the available width reaches 400 pixels.
<div class="container-type-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 CSS 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 CSS properties and examples.</p>
</div>
</div>
</div>
</div>
</div>
.container-type-demo {
display: grid;
gap: 20px;
}
.container-type-demo .demo-group {
padding: 15px;
border: 1px solid #cccccc;
background-color: #f8f9fa;
}
.container-type-demo h3 {
margin: 0 0 12px;
font-size: 1.125rem;
}
.container-type-demo .card-wrapper {
container-type: inline-size;
max-width: 100%;
}
.container-type-demo .narrow {
width: 280px;
}
.container-type-demo .wide {
width: 550px;
}
.container-type-demo .demo-card {
display: grid;
gap: 15px;
padding: 15px;
border: 2px solid #336699;
background-color: #ffffff;
}
.container-type-demo .card-icon {
display: flex;
align-items: center;
justify-content: center;
min-height: 80px;
background-color: #336699;
color: #ffffff;
font-weight: bold;
}
.container-type-demo .card-content h4 {
margin: 0 0 8px;
font-size: 1.125rem;
}
.container-type-demo .card-content p {
margin: 0;
}
@container (min-width: 400px) {
.container-type-demo .demo-card {
grid-template-columns: 120px 1fr;
align-items: center;
}
}
Narrow Container
CSS Reference
Explore CSS properties and examples.
Wide Container
CSS Reference
Explore CSS properties and examples.
The narrow container does not satisfy the 400-pixel query, so its card retains the default single-column layout.
The wide container satisfies the query, so its card changes to a two-column layout.
Both cards use the same HTML structure and CSS rules. Their layouts depend on their container widths rather than the viewport width.
Accessibility
Container queries can improve component responsiveness, but accessible design still depends on semantic HTML, readable content, and usable interactions.
Preserve Source Order
Keep a logical HTML source order so content remains understandable when responsive layouts change.
Support Text Resizing
Test browser zoom and enlarged text to ensure components remain readable at different container sizes.
Prevent Overflow
Use flexible widths, appropriate minimum sizes, and responsive spacing to prevent content from overflowing narrow containers.
Maintain Keyboard Navigation
Ensure changes between single-column and multi-column layouts do not create a confusing keyboard focus order.
Avoid Hiding Essential Content
Do not remove important information or controls solely because a container becomes smaller.
Practical Consideration
Test components in narrow sidebars, wide content regions, different writing modes, and high zoom levels.
Important Considerations
Keep the following points in mind when using container-type:
- Purpose: Establishes an element's size query capabilities.
- Initial value:
normal. - Inheritance: The property is not inherited.
- normal: Does not enable size queries.
- inline-size: Enables queries along the logical inline dimension.
- size: Enables queries along both logical dimensions.
- Size containment: Prevents circular dependencies between container dimensions and descendant styling.
- Intrinsic sizing: Full size containment can change automatic content-based sizing behavior.
- Writing modes: Logical inline and block dimensions depend on the element's writing mode.
- Container names: Optional for unnamed size queries.
- Named queries: Require a matching container name and support for the requested query features.
- Unnamed queries: Select the nearest eligible ancestor supporting the requested features.
- Nested containers: Different query conditions may select different ancestors.
- Container query units: Support lengths relative to eligible query container dimensions.
- Style queries: Supported style queries do not necessarily require a size query container.
- Shorthand: The
containerproperty sets bothcontainer-nameandcontainer-type. - Media queries: Remain useful for viewport-level responsive design.
- Accessibility: Responsive layouts should preserve readable content and logical navigation.
For most responsive components, begin with container-type: inline-size. Use size only when the component needs queries along both dimensions and its sizing behavior has been considered.
Browser Support
The CSS container-type property and size container queries are widely supported 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 browser versions.
Basic Example
.card-wrapper {
container-type: inline-size;
}
@container (min-width: 400px) {
.card {
padding: 25px;
}
}
Feature Detection
@supports (container-type: inline-size) {
.card-wrapper {
container-type: inline-size;
}
}
Feature queries can detect support for the property value, but they do not guarantee support for every newer container query feature.
For current compatibility information, visit Can I Use? .
Specifications
The container-type property is defined in CSS Containment Module Level 3.
CSS Containment Module Level 3
The specification defines query container types, size containment requirements, container selection, and the relationship between container types and size queries.
