Lesson 4 of 8

Container queries

Goal

A component that adapts to its container

01

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.

02

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
  1. The .wrap element is a container
  2. There is an @container rule
  3. The card is a row when the container is wide
  4. The items stack again when the container narrows
03

Hints

0/3
Track lessons0 of 8 done

Go deeper into the box model, custom properties, positioning, container queries and more, so you understand what's really going on behind the styles you see.

Intermediate level, 8 lessons, 1.5 hours in total.

index.html
Preview
index.html100%
–/4

Press “Run and check” to grade your code.

4 checks listed under “Your task”.

Track lessons0 of 8 done

Go deeper into the box model, custom properties, positioning, container queries and more, so you understand what's really going on behind the styles you see.

Intermediate level, 8 lessons, 1.5 hours in total.