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.
Imagine a reusable card component:
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.”
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.
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 heightYou 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;
}
} 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.
.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:
No viewport logic. No hacks.
| 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.
New units based on container size:
cqw – 1% of container widthcqh – 1% of container heightcqi – inline sizecqb – block sizecqmin, cqmaxExample:
.title {
font-size: 5cqw;
} Font size now scales with the container, not the viewport.
Container Queries are supported in all modern browsers:
They are now safe for production.
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.
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.
In JavaScript, it’s commonly used for: Recursive functions (like Fibonacci) Heavy calculations Repeated API/data processing…
1. What is Task Scheduling? Task scheduling is the process of automatically running commands, scripts,…
Here’s a comprehensive, clear differentiation between a Website and a Web App, from purpose all the…
Visual Studio Code (VS Code) is powerful out of the box, but its real strength…
1. What Is a Variable in JavaScript? A variable is a named container used to store data…
1. What Is a Queue? A Queue is a linear data structure that follows the principle: FIFO – First…