KodingKaro
CSS · Advanced · Container Queries

Declaring query containers

A component may appear in a narrow sidebar or a wide main column. Container queries let it respond to that local space.

✓ What you will learn in this topic

  • Explain the key idea: Establish a query container on a wrapper and adapt its descendants to local space.
  • Apply the concept: The card’s descendant layout changes when its named container—not merely the viewport—reaches the threshold.
  • Avoid this common mistake:

Concept explanation

Set container-type: inline-size on a parent to establish an inline-size query container. Optionally name it with container-name. Descendant rules can then use @container conditions. The container cannot generally style itself based on its own size; the query styles its descendants.

Containment changes how the browser treats layout, so apply it to the component wrapper that owns the size relationship.

Syntax

A CSS rule has a selector followed by declarations. Each declaration is a property and a value; the braces group the declarations that apply to matching elements.

.demo {
  container-type: inline-size;
}
@container (min-width: 30rem) {
  .demo p { max-width: 60ch; }
}

Example 1: a small page element

Use this HTML as the target and place the CSS in a stylesheet linked to the page, or inside a <style> element while experimenting.

<main class="demo card-grid course-grid dashboard-panel course-card-list dashboard-grid card-layout"><h2>Learning card</h2><p>Clear styles make this information easier to scan.</p><nav class="site-nav"><a href="/learn">Courses</a></nav><label class="form-field"><span>Email</span><input type="email" aria-invalid="true"></label><article class="card course-card dashboard-card theme-dark"><span class="badge status">New</span><div class="course-card__image"></div><h2 class="course-card__title card__title">Course update</h2><p class="status">New lessons are available this week.</p><a href="/learn" aria-current="page">Read the details</a><div class="actions course-card__actions dashboard-panel__actions"><button type="button">View lessons</button><button type="button" disabled>Unavailable</button></div></article></main>
.demo {
  container-type: inline-size;
}
@container (min-width: 30rem) {
  .demo p { max-width: 60ch; }
}

Expected result: The card’s descendant layout changes when its named container—not merely the viewport—reaches the threshold.

Example 2: a practical interface card

The same idea can style a reusable card on a course page. The class selector keeps the rule attached to this component rather than changing every element of that type.

<section class="demo card-grid course-grid dashboard-panel course-card-list dashboard-grid card-layout"><h2>Learning card</h2><p>Clear styles make this information easier to scan.</p><nav class="site-nav"><a href="/learn">Courses</a></nav><label class="form-field"><span>Email</span><input type="email" aria-invalid="true"></label><article class="card course-card dashboard-card theme-dark"><span class="badge status">New</span><div class="course-card__image"></div><h2 class="course-card__title card__title">Course update</h2><p class="status">New lessons are available this week.</p><a href="/learn" aria-current="page">Read the details</a><div class="actions course-card__actions dashboard-panel__actions"><button type="button">View lessons</button><button type="button" disabled>Unavailable</button></div></article></section>
.card-shell {
  container: course-card / inline-size;
}
@container course-card (min-width: 34rem) {
  .card { display: grid; grid-template-columns: 8rem 1fr; }
}

Expected result: The course update reads as a distinct interface card, and its content follows the spacing, color, and layout choices in the rule.

Common mistake

Querying a container that has no containment context, or trying to style the container based on its own size.

Best practice

Declare an appropriate containment type on the parent and test the component in different parent widths.

Practice exercise

Create a card that switches from stacked to horizontal layout when its container becomes wide enough. Apply it to the example above and inspect the result at a narrow and a wide viewport.

Section summary

  • Establish a query container on a wrapper and adapt its descendants to local space.
  • Declare an appropriate containment type on the parent and test the component in different parent widths.

Knowledge check

Which element do container query rules style?

Reveal the explanation

The container provides the measurement context; matching descendant rules respond to it.

Finish the module lessons, then take the server-scored module quiz to check your understanding.

Practical example

css
.demo {
  container-type: inline-size;
}
@container (min-width: 30rem) {
  .demo p { max-width: 60ch; }
}

Expected result:

The card’s descendant layout changes when its named container—not merely the viewport—reaches the threshold.

Hands-on practice task

Create a card that switches from stacked to horizontal layout when its container becomes wide enough.

In this section

Other lessons in Container Queries

Broaden your knowledge

Related learning topics