KodingKaro
CSS · Advanced · Container Queries

Component layouts with @container

Container queries make a component portable because it can choose its own arrangement wherever it is placed.

✓ What you will learn in this topic

  • Explain the key idea: Use container queries when component behavior depends on its local allocation.
  • Apply the concept: The card remains stacked in a narrow container and becomes a horizontal composition in a wider one.
  • Avoid this common mistake:

Concept explanation

A card can stack its image and text at a small inline size, then switch to columns at a larger size. This avoids coupling its behavior to global viewport breakpoints. Named containers are useful when a component has more than one possible query ancestor.

Do not add a query for every small visual difference. Prefer a fluid base and introduce a query when the component’s composition genuinely needs to change.

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;
}
.demo .card { display: block; }
@container (min-width: 28rem) {
  .demo .card { display: flex; gap: 1rem; }
}

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;
}
.demo .card { display: block; }
@container (min-width: 28rem) {
  .demo .card { display: flex; gap: 1rem; }
}

Expected result: The card remains stacked in a narrow container and becomes a horizontal composition in a wider one.

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>
.course-card-list { container-type: inline-size; }
@container (min-width: 42rem) {
  .course-card { grid-template-columns: 10rem 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

  • Use container queries when component behavior depends on its local allocation.
  • Declare an appropriate containment type on the parent and test the component in different parent widths.

Knowledge check

Why can a container query make a reusable card more portable?

Reveal the explanation

The component adapts wherever it is placed, including sidebars and main content columns.

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

Practical example

css
.demo {
  container-type: inline-size;
}
.demo .card { display: block; }
@container (min-width: 28rem) {
  .demo .card { display: flex; gap: 1rem; }
}

Expected result:

The card remains stacked in a narrow container and becomes a horizontal composition in a wider one.

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