Skip to content
Guest
Learn CSS · Course Syllabus Show lessons
Responsive & Modern Selectors · 9 min read · Beginner

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

  1. Mark the parent wrapper with container-type: inline-size;.
  2. 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!

Live @container Query Demo
Live Result

Check Your Understanding

Score: 0 / 1

1. What CSS property must you set on a parent element before querying its width with @container?

Study Workspace

Finished this lesson?

Your progress is saved locally in your browser, no account needed.

Search courses, lessons, and references

Type to search across all courses, lessons, and quick references.

Press Esc to close Powered by Pagefind