ProgramGuru

CSS Container Queries

CSS Container Queries

Container queries let a component react to the size of its own containing region, even when the overall browser is wide.

What you’ll learn

  • Distinguish viewport and container queries.
  • Declare a query container.
  • Change component layout based on local width.

Understand the concept

Declare container-type: inline-size on a parent. Then use @container (min-width: 320px) to change styles for descendants when that parent is at least 320px wide.

Queries evaluate an eligible ancestor, not the same element declaring container-type. This makes components reusable in dashboards, sidebars and article columns.

What the browser evaluates

The parent establishes an inline-size query container; its child receives extra rules only when the container—not viewport—is wide enough.

Small local container

A narrow host shows the component in one column.

HTML

<div class="component-host narrow"><article class="feature"><strong>Saved places</strong><p>Find trips you bookmarked.</p></article></div>

CSS

.component-host { container-type: inline-size; width: min(100%, 260px); padding: 8px; background: #f1f5f9; box-sizing: border-box; } .feature { display: grid; gap: 8px; background: #dbeafe; padding: 12px; border-radius: 8px; } .feature p { margin: 0; } @container (min-width: 320px) { .feature { grid-template-columns: 1fr 1fr; } }

Rendered output

Saved places

Find trips you bookmarked.

Observe: The query does not match because the host is narrower than 320px.

Wide local container

Use the same component rules in a wider host.

HTML

<div class="component-host wide"><article class="feature"><strong>Saved places</strong><p>Find trips you bookmarked.</p></article></div>

CSS

.component-host { container-type: inline-size; width: min(100%, 460px); padding: 8px; box-sizing: border-box; background: #f1f5f9; } .feature { display: grid; gap: 8px; background: #ccfbf1; padding: 12px; border-radius: 8px; } .feature p { margin: 0; } @container (min-width: 320px) { .feature { grid-template-columns: 1fr 1fr; align-items: center; } }

Rendered output

Saved places

Find trips you bookmarked.

Observe: When the parent has 320px available, the feature switches to two columns.

Explain the result

The parent establishes an inline-size query container; its child receives extra rules only when the container—not viewport—is wide enough.

Compare the two examples: identify the selector, each changed property, and which visible part of the output proves that the CSS took effect.

Common mistakes to avoid

  • Container queries require a suitable ancestor container, not just @container by itself.

  • The query container cannot query its own size to restyle itself in this pattern.

Try it yourself

Put the wide component inside a narrower parent and notice when the two-column rule stops matching.

Before changing the code, predict what will change. Then verify by checking the rendered result.

Check your understanding

What width is checked by an inline-size container query?
Choose one answer. You can try again.

Answer and explanation

An eligible ancestor query container

A size container query evaluates an eligible containing ancestor.

What you should remember

Prefer container queries for reusable components whose placement changes independently of the page.

Revisit the rendered examples if the rule still feels abstract. Change one declaration at a time so you can connect cause and effect.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: CSS Variables (Custom Properties) →