Ever wish your components could adapt based on their container’s size instead of the whole viewport? With CSS Container Queries, you can! This new, powerful feature lets your elements respond to the space they live in — perfect for flexible, reusable components that stay looking great in any layout. In this MiniCoursey quick guide, you’ll learn how Container Queries work, see practical examples, and discover how they unlock smarter responsive designs.
What Are Container Queries?
Container Queries check the size of a parent container and apply styles when it matches certain conditions. Unlike media queries that look at the viewport, Container Queries look at the element’s context.
Basic Syntax
To use Container Queries, first declare a container using container-type
. Then write a @container
rule to style based on its size.
.card {
container-type: inline-size;
}
@container (min-width: 400px) {
.card-content {
display: flex;
}
}
In this example, the .card-content
switches to flex layout when its parent .card
is at least 400px wide.
Why Use Container Queries?
- Make truly reusable components.
- Handle nested layouts that adjust naturally.
- Reduce complex media queries for simpler CSS.
💡 Pro Tip: Combine Container Queries with Flexbox or Grid for maximum layout power.
⚠️ Common Mistake: Don’t forget to set
container-type
— the query won’t work without it!
FAQ
Q: Do Container Queries replace media queries?
A: Not entirely — they work together! Use media queries for viewport changes and Container Queries for component context.
Q: Are they widely supported?
A: Modern browsers like Chrome, Edge, and Safari support them — check MDN for the latest updates.
Related Link
👉 Check out previous: CSS Clamp() — Fluid Sizing with Min and Max
Where to Learn More
Check MDN’s Container Queries docs for practical examples and start experimenting with adaptive, component-based layouts.
Conclusion
Congrats — you now know how to build smarter, context-aware components with CSS Container Queries! It’s one of the biggest leaps forward for responsive design.
✨ Bookmark MiniCoursey for more quick & free mini courses!