The Limitation of Media Queries
Css
/* Media Query Example */
.card {
display: flex;
flex-direction: column;
}
@media (min-width: 768px) {
.card {
flex-direction: row;
}
}
Enter Container Queries
How to Use Container Queries
Css
.sidebar {
container-type: inline-size;
/* You can optionally name the container */
container-name: sidebar-container;
}
.main-content {
container-type: inline-size;
container-name: main-container;
}
Css
.card {
display: flex;
flex-direction: column;
}
/* This applies when the PARENT container is at least 400px wide */
@container (min-width: 400px) {
.card {
flex-direction: row;
}
}
Why This is a Game Changer
- If placed in .main-content (which might be 800px wide), the container query triggers, and the card displays as a row.
- If placed in .sidebar (which might be 300px wide), the container query does not trigger, and the card gracefully falls back to a column layout.