For years, responsive design has depended almost entirely on media queries. We ask questions like:
“If the screen is 768px wide, do this.”
“If the screen is 1024px wide, change that.”
But modern web layouts are no longer just about the viewport. We build components that live inside cards, sidebars, modals, grids, and dashboards. A component might be wide in one place and narrow in another—even on the same screen.
Learn software development and programming.
This is the problem CSS Container Queries solve.
Instead of responding to the screen size, elements can now respond to the size of their parent container.
Thousands are already learning how to code online.
Why Media Queries Are No Longer Enough
Imagine a reusable card component:
- On desktop: it sits in a 3-column grid.
- On mobile: it becomes full-width.
- In a sidebar: it is very narrow.
With media queries, you only know the viewport width, not the card’s actual width. So the card’s layout might break when reused in different contexts.
Container queries flip the logic:
“Style this component based on how much space it actually has.”
What Are Container Queries?
Container queries allow an element to apply styles depending on the size of its container, not the viewport.
You define a container:
.card-wrapper {
container-type: inline-size;
}
Then you query it:
@container (min-width: 400px) {
.card {
display: grid;
grid-template-columns: 1fr 1fr;
}
}
Meaning:
If the container is at least 400px wide, change the layout of
.card.
Step 1: Defining a Container
To use container queries, you must mark an element as a container.
.container {
container-type: inline-size;
}
Common values:
inline-size→ responds to width (most common)size→ responds to both width and height
You can also name containers:
.container {
container-type: inline-size;
container-name: layout;
}
Then target it like this:
@container layout (min-width: 600px) {
.item {
font-size: 1.2rem;
}
}
Step 2: Writing a Container Query
Basic syntax:
@container (min-width: 500px) {
/* styles */
}
With class targeting:
@container (max-width: 300px) {
.profile-card {
flex-direction: column;
}
}
This means:
When the parent container becomes narrow, stack the content vertically.
Real-World Example: A Responsive Card
.card-wrapper {
container-type: inline-size;
}
.card {
display: flex;
gap: 1rem;
}
@container (max-width: 350px) {
.card {
flex-direction: column;
}
}
Now the card automatically adapts when placed in:
- A wide main section → horizontal layout
- A narrow sidebar → vertical layout
No viewport logic. No hacks.
Container Queries vs Media Queries
| Media Queries | Container Queries |
|---|---|
| Based on screen size | Based on parent size |
| Page-level logic | Component-level logic |
| Hard to reuse components | Perfect for reusable components |
| Layout-driven | Context-driven |
They work best together, not as replacements.
Container Query Units
New units based on container size:
cqw– 1% of container widthcqh– 1% of container heightcqi– inline sizecqb– block sizecqmin,cqmax
Example:
.title {
font-size: 5cqw;
}
Font size now scales with the container, not the viewport.
Practical Use Cases
- Dashboard widgets
Each widget adapts to the column it’s placed in. - Card components
Same card works in grids, modals, sidebars. - Design systems
Truly responsive components, not page-specific ones. - Embeddable components
Your UI adapts even when embedded in unknown layouts.
Browser Support
Container Queries are supported in all modern browsers:
- Chrome
- Edge
- Firefox
- Safari
They are now safe for production.
Mental Shift for Developers
Old thinking:
“How big is the screen?”
New thinking:
“How much space does this component have?”
This is a major shift toward true component-driven design, which aligns perfectly with modern frameworks like React, Vue, and Angular.
Final Thought
Media queries made websites responsive.
Container queries make components intelligent.
They are one of the biggest CSS breakthroughs in the last decade because they finally allow:
Layout logic to live where it belongs — inside the component itself.

Latest tech news and coding tips.