π What is @container?
@container is a modern CSS at-rule that enablescontainer queries β meaning elements can adapt based on the size of their parent container instead of the entire viewport! This solves one of the biggest limitations in responsive design.
Note
π― Why Do We Need @container?
- Responsive components that adapt to their parent width
- No more relying only on viewport-based media queries
- Reusable, modular UI components
- Better control inside grids, cards, sidebars, etc.
π How @container Works
Two steps:
- Enable containment on a parent container.
- Write container queries that react to the containerβs size, not the viewport.
π§± Step 1 β Enable Containment
Apply container-type to a parent element:
Enable container
.card {
container-type: inline-size; /* reacts to width only */
}inline-size β listens to width
size β listens to width + height
Note
π§± Step 2 β Write a Container Query
Basic Container Query
@container (min-width: 400px) {
.title {
font-size: 2rem;
}
}The `.title` becomes larger only when its parent container becomes 400px wide β not the viewport.
π Visual Example

π₯ Full Component Example
Card With Container Query
.card {
padding: 20px;
border: 1px solid #ddd;
container-type: inline-size;
}
.card-content {
display: flex;
flex-direction: column;
}
@container (min-width: 500px) {
.card-content {
flex-direction: row;
gap: 20px;
}
}When the card becomes wide enough, the layout switches to side-by-side view (even if the viewport is still small!).
π¨ Responsive Typography Using @container
Container-based typography
.section {
container-type: inline-size;
}
@container (min-width: 600px) {
h1 {
font-size: 3rem;
}
}
@container (max-width: 599px) {
h1 {
font-size: 2rem;
}
}The heading adjusts based on section width instead of screen size.
π¦ Container Names
Naming Containers
.wrapper {
container-type: inline-size;
container-name: layout;
}
@container layout (min-width: 800px) {
.sidebar {
display: block;
}
}Naming allows writing container queries that target specific container types.
π§ Nested Containers
Container queries work independently for each container β meaning **each component is fully responsive inside its own space**.

π± Example β Buttons Growing With Container
Button Container Query
.button-group {
container-type: inline-size;
}
@container (min-width: 350px) {
button {
padding: 1rem 2rem;
font-size: 1.2rem;
}
}Buttons get larger only when their parent group expands β perfect for toolbars or cards.
π§ͺ Range Queries + @container (Level 4 Syntax)
Range Query with Container
@container (400px <= width <= 800px) {
.grid {
grid-template-columns: repeat(2, 1fr);
}
}Works exactly like viewport range queries, but applied to container width.
β οΈ Common Mistakes
- Forgetting to set `container-type` β queries wonβt work
- Applying @container to elements that never shrink or grow
- Confusing @container with @media (one is component-based, one is viewport-based)
- Using height-based queries without `container-type: size`
Note
@container = component responsive
Use BOTH for best results!
π₯ Summary
@container is a groundbreaking feature that makes CSS truly modular and adaptive. It enables components to respond to their available space instead of the entire viewport, making reusable UI systems far more reliable and flexible.
Learn more βMDN Docs π