learn.web Back to the curriculum ↗

HTML / CSS / JavaScript / 02

Layout without page breakpoints

Compose with Grid, subgrid, container queries, logical properties, and intrinsic sizing.

Time
85 min
Mode
Learn → Make → Check
Path
Modern Web Platform

By the end, you can…

The viewport is not the component

A viewport media query knows the browser width, not the space a component actually receives. The same card may live in a full-width page, a split panel, or a narrow sidebar at the same viewport size. Container queries let the card respond to its own layout context.

Begin with intrinsic rules: minmax(), min(), max(), clamp(), flex wrapping, and grid auto-placement. They allow content to negotiate space without a list of device guesses. Add a container query only when the component needs a meaningful change in composition.

A component owns its adaptation

Declare container-type on the component’s parent, then query that container with inline-size. Keep breakpoints local and name them after the design change—stacked card, roomy card—not a device.

Stress-test with long titles, translated text, 200% zoom, missing images, and a very narrow parent. The useful breakpoint is where the content stops working, not where a framework says tablet begins.

Working example
.card-region { container-type: inline-size; }
.card { display: grid; gap: 1rem; }
@container (width > 34rem) {
  .card { grid-template-columns: 12rem 1fr; }
}

Your studio task

Make — Create a card rail that changes composition from its own available width.

  1. Place the same component in one narrow and one wide parent.
  2. Use intrinsic sizing to remove avoidable overflow.
  3. Add one container query for a genuine composition change.
  4. Test long content, zoom, and a missing optional element.
Definition of done

The component works in a sidebar and a full page without viewport-specific overrides.

Open the interactive lesson with its workspace ↗

Knowledge check

When is a container query most appropriate?
  1. When a component changes because of its own available width
  2. Whenever the viewport is below 768px
  3. To replace every flex-wrap rule
Reveal answer (A)

Container queries are strongest when reusable components need to adapt independently of the viewport.

Which declaration makes an element a container for inline-size queries?
  1. container-type: inline-size
  2. display: container
  3. position: container
Reveal answer (A)

container-type: inline-size opts the element into container queries for its inline axis.