Layout
Container Queries
Master CSS Container Queries, covering container-type declaration, query scopes, container units, and building truly reusable responsive components.
1. Learning Objectives
In this lesson, you will master CSS Container Queries. By the end of this topic, you will be able to:
- Differentiate between standard media queries and container queries.
- Configure container context wrappers using the
container-typeproperty. - Write
@containerrules to target container width changes. - Use Container Query units (like
cqw,cqh, andcqi) to scale elements. - Build modular component cards that adapt to the width of their parent container.
2. Overview
Container queries allow you to style elements based on the dimensions of their parent container rather than the browser viewport. This is a major improvement for component-driven design, allowing you to build modular cards or widgets that adapt their layout depending on where they are placed on the page (e.g. in a narrow sidebar vs a wide main content area).
3. Why This Topic Matters
Container queries make components truly modular and reusable:
- Component Layout Breaks: Standard media queries look only at the screen width. If you place a search widget inside a narrow sidebar on a desktop display, a viewport media query assumes there is plenty of screen space and renders the wide desktop layout. This can cause the widget to overlap and break the sidebar layout.
- Prerequisite Config Failures: Container queries will not work if the parent container is missing the
container-type: inline-sizedeclaration. Forgetting this is a common source of styling bugs.
4. Real-World Analogy
Think of container queries like **packing furniture into different rooms**:
- Media Query (The House Size): Deciding what furniture to buy based on the total square footage of the house. This works for overall planning but doesn't tell you how furniture will fit in individual rooms.
- Container Query (The Room Size): Adapting the furniture layout based on the size of the room it is placed in. A modular shelving unit behaves as a tall vertical shelf when placed in a narrow closet (sidebar), but expands into a wide horizontal storage unit when placed in a spacious living room (main content area).
5. Core Concepts
Container queries rely on two key configurations:
- Container Parent (The Context): The wrapper element that defines the layout context. You must apply the
container-typeproperty to this element to declare it as a container. - Container Type Values:
inline-size:Styles are applied based on the container's width (most common).size:Styles are applied based on both the container's width and height.
- Container Units: Sizing units (like
cqwandcqh) that calculate size relative to 1% of the container's width or height.
6. Syntax & API Reference
Below is a CSS configuration for a container parent and a child card element that adapts to its container's width:
7. Visual Diagram
This diagram displays how a child card adapts its layout based on the width of its parent container:
8. Live Example — Full Working Code
A sample HTML document demonstrating container query layout changes:
9. Interactive Playground
Try It Yourself Challenges:
- Change the grid columns property in the example layout and observe how the card layouts adapt dynamically to their new container widths.
- Test how container query units (e.g.
font-size: 4cqw;) scale font sizes relative to the parent container width.
10. Common Mistakes
| Mistake | Why it happens | Wrong | Correct |
|---|---|---|---|
| Missing container-type property | Attempting to write container queries without defining the container-type inline-size property on the parent element. | @container queries defined but parent is static |
Declare container-type: inline-size; on the parent wrapper selector. |
| Styling the container parent within its own query | Attempting to update the width of a container inside its own query, causing infinite rendering loops. | @container (min-width: 400px) { .parent { width: 300px; } } |
Only update child elements (e.g. .child) inside container queries. |
11. Best Practices
- Always declare a container-type on parent containers: Define
container-type: inline-sizeon parent wrapper elements to establish a container query context. - Only style child elements inside container queries: Avoid styling the container parent inside its own query to prevent rendering loops. Use queries only to style nested child elements.
- Use container units for fluid scaling: Use container query units (e.g.
cqw) to scale font sizes and spacing relative to the parent container width. - Apply container names when nesting: Use the
container-nameproperty to label containers when nesting them, allowing you to target specific containers in your queries.
12. Browser Compatibility
| Feature | Chrome | Firefox | Safari | Edge |
|---|---|---|---|---|
| container-type & @container support | Supported (105+) | Supported (110+) | Supported (16+) | Supported (105+) |
13. Interview Questions
🟢 Q1: How do container queries differ from standard media queries, and why are they useful?
Answer: @media queries evaluate styling based on the browser viewport width, which isn't sufficient for modular component design. @container queries evaluate styling based on the width of the parent container wrapper. This allows you to build modular, reusable components that adapt their layout automatically depending on where they are placed on the page.
14. Debugging Exercise
Explain why the card layout remains stacked vertically in both containers, and fix the CSS configuration:
Diagnosis: The parent element (.widget-container) is missing the container-type property. Without it, the browser doesn't calculate container dimensions, and the @container query on the child element is ignored. To resolve this, add container-type: inline-size to the parent container.
Fixed CSS:
15. Practice Exercises
Exercise 1: Responsive Product Grid Widget
Build a product card widget. Define a container parent context, and use container queries to display a three-column grid when the container is wide, and stack elements vertically when it is narrow.
16. Scenario-Based Challenge
The Multi-Layout Sidebar Widget Challenge:
Your company requires a profile header widget that can be placed in either a narrow dashboard sidebar (200px width) or a wide main body header banner (1000px width). Standard viewport media queries are causing layout overlap bugs in the narrow sidebar. Propose an implementation plan using CSS container queries to build this widget.
17. Quick Quiz
Q1: Which container-type value evaluates layouts based on container width only?
A) size
B) inline-size
C) normal
Answer: B — The inline-size value configures the browser to evaluate container queries based on width changes only.
18. Summary & Key Takeaways
- • Container queries style child elements based on the dimensions of their parent container.
- • Always declare container-type: inline-size on parent wrappers to enable container query styles.
19. Cheat Sheet
| Property API | Visual Layout Action |
|---|---|
container-type: inline-size; |
Sets the parent element as a container query context based on its width. |