Responsive Design & Container Queries
Build layouts that adapt to both viewport size (@media) and parent container size (@container) for truly reusable UI components.
What you'll learn
- Write mobile-first responsive rules with min-width media queries
- Make self-contained components adapt to their parent using @container
- Keep media proportions locked with aspect-ratio
Traditional Media Queries (@media) respond to the whole browser viewport. Modern Container Queries (@container) let a component respond to the width of its own parent container, so the same card looks great in both a narrow sidebar and a wide main column!
Setting Up a Container Query
- Mark the parent wrapper with
container-type: inline-size;. - Query it from inside using
@container (min-width: 320px) { ... }.
Try It Yourself
Both cards below use the exact same HTML and CSS class, the top one is in a wide container (horizontal layout) and the bottom one is in a narrow container (stacked layout). Try changing max-width: 240px to 380px!
Check Your Understanding
1. What CSS property must you set on a parent element before querying its width with @container?
Finished this lesson?
Your progress is saved locally in your browser, no account needed.