Lesson 4 of 8
Container queries
- 14 min
- 4 checks
- 3 hints
- Go to task
Goal
A component that adapts to its container
Explanation
An @media query asks about the width of the screen. But the same card can appear in a narrow column and in a wide area on the very same page, and the screen width can't tell the difference.
@container asks about the width of the container instead. You declare an element a container with container-type: inline-size, then write rules that apply based on its own width.
This is a big shift, because the component no longer depends on where it sits: you write it once and drop it anywhere you like.
Your task
Make .wrap a container, and make .card turn into a row (flex) when the container is at least 380 pixels wide.
Checks
4 checks- The .wrap element is a container
- There is an @container rule
- The card is a row when the container is wide
- The items stack again when the container narrows