03. CSS Container Queries (@container & cqw)
🔴 Advanced
1. Definition
Container Queries (@container (min-width: 400px) { ... }) allow component cards to style themselves based on their parent container width rather than global viewport screen dimensions, enabling true modular component architecture.
2. Why It Is Used
Media Queries only inspect window width. If a card component is placed inside a narrow sidebar, a media query assumes desktop viewport width and renders a broken layout. Container Queries solve this by observing the parent box width.
3. How It Works
An ancestor container declares container-type: inline-size. Child components evaluate @container (min-width: 400px) relative to that ancestor's inline width.
4. Syntax
.card-wrapper {
container-type: inline-size;
container-name: cardContainer;
}
@container cardContainer (min-width: 450px) {
.card {
display: flex;
flex-direction: row;
}
}
5. Media Queries vs Container Queries Table
| Query Type | Evaluates Dimensions Of | Best Practice Usage |
|---|---|---|
Media Query (@media) | Browser Viewport Screen Window | Global page layouts & body wrappers |
Container Query (@container) | Nearest Parent Container Box | Reusable Independent UI Components |
6. Beginner Example
.widget-box {
container-type: inline-size;
}
@container (min-width: 350px) {
.widget-title { font-size: 1.5rem; }
}
7. Intermediate Example
.profile-card-container {
container-type: inline-size;
}
@container (min-width: 500px) {
.profile-card {
display: flex;
align-items: center;
gap: 20px;
}
}
8. Advanced Component Design System Example
.card-container {
container-type: inline-size;
container-name: card;
}
@container card (min-width: 600px) {
.card-avatar { width: 120px; height: 120px; }
.card-body { padding: 2rem; }
}
9. Line-by-Line Code Explanation
container-type: inline-size;: Establishes a container query context listening to horizontal width changes on the parent wrapper.@container card (min-width: 600px): Applies internal flex/grid layout enhancements whenever the parent container exceeds 600px width.
10. Expected Terminal / Browser Output
A reusable card component that renders as a horizontal flex layout in main content areas and automatically converts to a vertical stack when dragged into narrow sidebars.
11. Common Mistakes & Pitfalls
- Forgetting
container-type: inline-sizeon parent: Child@containerqueries fail silently unless an ancestor has declared acontainer-type.
12. Important Rules & Limitations
- Container Queries are supported across all modern evergreen browsers (Chrome 105+, Safari 16+, Firefox 110+).
13. Multilingual Explanation: English
Container Queries (@container) style components based on parent container width rather than viewport width.
14. Multilingual Explanation: Hindi (Roman Script)
Container Queries (@container) component ko parent container ki width ke anusar style karte hain.
15. Multilingual Explanation: Marathi (Roman Script)
Container Queries mule components screen size mivaje parent box width nusar badaltat.
16. Practice Questions
- Why do Container Queries enable better component modularity than Media Queries?
17. Practical Coding Challenge
Build a .news-item rule with container-type: inline-size that switches layout from column to row at min-width: 400px container width.