---
title: "CSS Container Queries"
url: https://www.khanwaseem.com/blog/css-tutorial/css-container-queries
author: "khan Waseem"
published: 2026-12-05
updated: 2026-12-05
topics: ["CSS Tutorial", "course"]
description: "Learn CSS container queries: style a component based on the size of its container instead of the screen, with container-type, @container, container units like cqi, and style queries."
---

# CSS Container Queries

Table of Contents

[Toggle](#)

- [Introduction:](#introduction)
- [Media Queries vs Container Queries:](#media-queries-vs-container-queries)
- [Step 1: Create a Container](#step-1-create-a-container)
- [Step 2: Write @container Rules](#step-2-write-container-rules)
- [A Complete Example: One Card, Two Layouts](#a-complete-example-one-card-two-layouts)[What the browser did:](#what-the-browser-did)

- [Container Query Units:](#container-query-units)
- [Style Queries:](#style-queries)
- [Browser Support:](#browser-support)
- [Common Mistakes to Avoid:](#common-mistakes-to-avoid)[1. Putting the container on the element you want to style:](#1-putting-the-container-on-the-element-you-want-to-style)
- [2. Using container-type: size without a height:](#2-using-container-type-size-without-a-height)
- [3. Forgetting that a container is also a new context:](#3-forgetting-that-a-container-is-also-a-new-context)
- [4. Replacing every media query:](#4-replacing-every-media-query)
- [5. Unnamed containers in nested components:](#5-unnamed-containers-in-nested-components)

- [A Quick Tip for Building Cards:](#a-quick-tip-for-building-cards)
- [Conclusion:](#conclusion)

## Introduction:

In the last CSS lesson, [CSS – Custom Properties (Variables)](https://blog.khanwaseem.com/css-custom-properties-variables/), we made our styles reusable. Today we make our **components** truly reusable with one of the most useful additions to CSS in years: **container queries**.

You already know media queries. `@media (min-width: 600px)` changes styles based on the width of the **screen**. That works well for the overall page layout, but it has a big problem for components.

Imagine a product card. In the wide main column it has plenty of room, so you want the image on the left and the text on the right. The exact same card also appears in a narrow 300px sidebar, where it should stack the image on top. The screen is the same width in both cases, so a media query cannot tell these two cards apart.

A **container query** asks a different question: “How wide is the box **this component sits in**?” In this lesson you will learn how to set up a container, write `@container` rules, use container units like `cqi`, and try style queries. Every result below was measured in Chromium with Playwright.

## Media Queries vs Container Queries:

- **Media query:** “If the viewport is at least 600px wide, do this.” Good for page layout: the number of columns, showing or hiding the sidebar, the navigation style.

- **Container query:** “If my container is at least 400px wide, do this.” Good for components: cards, widgets, forms, anything that can be placed in different spots.

You will use both together. Media queries shape the page, and container queries shape the pieces inside it.

## Step 1: Create a Container

An element does not act as a container by default. You turn it into one with the `container-type` property:

```css
.card-wrapper {
  container-type: inline-size;
}Code language: CSS (css)
```

The values are:

- `inline-size`: queries can use the container’s **width** (in left-to-right languages). This is the one you will use almost every time.

- `size`: queries can use both width and **height**. Use it with care (see the mistakes section).

- `normal`: the default. Not a size container.

You can also give the container a name, so a component can target exactly the container it wants:

```css
.card-wrapper {
  container-type: inline-size;
  container-name: card;
}

/* shorthand: name / type */
.card-wrapper {
  container: card / inline-size;
}Code language: CSS (css)
```

One important rule: **an element cannot query itself.** The `@container` rules style the **children** of the container. That is why we put the container on a wrapper around the card, not on the card.

## Step 2: Write @container Rules

The syntax looks just like a media query:

```css
@container card (min-width: 400px) {
  .card {
    grid-template-columns: 160px 1fr;
  }
}Code language: CSS (css)
```

If you leave out the name, the rule uses the **nearest ancestor** that is a container.

You can use the same features you know from media queries (`min-width`, `max-width`, `width > 400px`, `orientation`) and combine them with `and`, `or` and `not`.

## A Complete Example: One Card, Two Layouts

Here is a page with a main column and a 300px sidebar. The same card is placed in both:

```javascript
<div class="layout">
  <main>
    <div class="card-wrapper">
      <article class="card">
        <img src="product.jpg" alt="Desk lamp">
        <div>
          <h2>Desk lamp</h2>
          <p class="meta">Free delivery · 4.6 stars</p>
          <span class="badge">NEW</span>
        </div>
      </article>
    </div>
  </main>
  <aside>
    <div class="card-wrapper">
      <article class="card">...the same card...</article>
    </div>
  </aside>
</div>Code language: JavaScript (javascript)
```

And the CSS:

```css
.layout {
  display: grid;
  grid-template-columns: 1fr 300px;
  gap: 16px;
  padding: 16px;
}

.card-wrapper {
  container: card / inline-size;
}

/* Small by default: image on top, text below */
.card {
  display: grid;
  gap: 12px;
  padding: 12px;
  border: 1px solid #000;
}
.card img { width: 100%; height: 120px; object-fit: cover; }
.card .meta { display: none; }

/* Medium container: image on the left */
@container card (min-width: 400px) {
  .card { grid-template-columns: 160px 1fr; }
  .card img { height: 100%; }
}

/* Large container: show the extra details */
@container card (min-width: 600px) {
  .card .meta { display: block; }
}Code language: CSS (css)
```

Notice that the card is written **small first**, the same mobile-first idea we use with media queries. Bigger containers add to it.

### What the browser did:

We opened this page in Chromium at two window widths and measured each card:

```javascript
Window 1200px:
  main card    container 852px  -> columns "160px 654px", meta shown
  sidebar card container 300px  -> one column (274px),    meta hidden

Window 900px:
  main card    container 552px  -> columns "160px 354px", meta hidden
  sidebar card container 300px  -> one column (274px),    meta hiddenCode language: JavaScript (javascript)
```

Look at the 1200px row. The **same card** with the **same CSS** shows three different things depending on where it lives: the wide main card gets the side-by-side layout and the extra details, and the sidebar card stays stacked. And when the window shrank to 900px, the main card dropped the details because **its container** became narrower than 600px, even though no media query was involved.

This is the big win: the card no longer needs to know anything about the page. Drop it into a modal, a sidebar, a 3-column grid or a full-width section, and it picks the right layout by itself.

## Container Query Units:

Container queries also come with new length units, measured against the container instead of the viewport:

- `cqw`: 1% of the container’s width

- `cqh`: 1% of the container’s height

- `cqi`: 1% of the container’s **inline size** (width in English)

- `cqb`: 1% of the container’s block size (height in English)

- `cqmin` and `cqmax`: the smaller or larger of `cqi` and `cqb`

They are perfect for text that should grow with the component. Combine them with `clamp()` so the size never gets too small or too big:

```css
.badge {
  font-size: clamp(12px, 5cqi, 24px);
}

@container card (min-width: 600px) {
  .card h2 { font-size: max(1.25rem, 4cqi); }
}Code language: CSS (css)
```

Measured results at a 1200px window:

```
main card    (852px container): badge 24px (5cqi = 42.6px, capped at 24px), h2 34.08px (4% of 852)
sidebar card (300px container): badge 15px (5% of 300)
```

Prefer `cqi` over `cqw`: it follows the writing direction, so it still works correctly for vertical languages.

## Style Queries:

Size is not the only thing you can query. **Style queries** check the value of a custom property on the container:

```css
.theme {
  --variant: dark;
}

@container style(--variant: dark) {
  .themed {
    background: #000;
    color: #fff;
  }
}Code language: CSS (css)
```

```xml
<div class="theme"><p class="themed">Dark box</p></div>
<div><p class="themed">Normal box</p></div>Code language: HTML, XML (xml)
```

In our test, the first paragraph got a black background (`rgb(0, 0, 0)`) and the second stayed transparent. Notice that style queries do **not** need `container-type`: every element can be queried for custom properties.

This is handy for themes and variants: set `--variant: dark` or `--layout: compact` on a section, and every component inside adapts. Size queries work in all modern browsers. Style queries for custom properties work in Chromium-based browsers and Safari, but Firefox support arrived later, so check [caniuse.com](https://caniuse.com/css-container-queries-style) and treat them as an extra rather than something the layout depends on.

## Browser Support:

Size container queries and container units are supported in all modern browsers (Chrome and Edge 105+, Safari 16+, Firefox 110+, all released in 2022 and 2023). If you must support older browsers, write the small layout as the default (like we did), so old browsers simply show the stacked card. You can also wrap the rules in `@supports (container-type: inline-size) { ... }`.

## Common Mistakes to Avoid:

### 1. Putting the container on the element you want to style:

An element cannot query its own size. Put `container-type` on a **parent** wrapper and style the children.

### 2. Using container-type: size without a height:

With `size`, the browser stops using the children to work out the container’s height, to avoid a loop (the content depends on the container, and the container depends on the content). If you do not give it a height, it collapses. In our test, a `container-type: size` element with a paragraph inside measured **0px tall**. Use `inline-size` unless you really need to query the height and you set one.

### 3. Forgetting that a container is also a new context:

`container-type` adds layout and size containment. In rare cases this changes things like how `position: fixed` children or intrinsic widths behave. If something looks odd after adding it, check this first.

### 4. Replacing every media query:

The page layout itself still depends on the screen. Use media queries for the page, container queries for components.

### 5. Unnamed containers in nested components:

Without a name, `@container` uses the nearest container. If you nest components, the nearest one might not be the one you meant. Name your containers.

## A Quick Tip for Building Cards:

Cards like the one in this lesson often use a soft shadow to lift them from the page. Instead of guessing the numbers, you can design one visually with a tool like the [Eskillsfy Box Shadow Generator](https://eskillsfy.com/tools/box-shadow-generator) and copy the CSS into your card.

## Conclusion:

Media queries ask about the screen. Container queries ask about the box a component lives in, which is what components really care about. Turn a wrapper into a container with `container-type: inline-size` (and a name), write `@container name (min-width: ...)` rules for the children, size text with container units like `cqi` and `clamp()`, and use style queries for variants where the browser supports them. The result is components you can drop anywhere and trust to look right.

In the next CSS lesson, we will learn the **CSS :has() Selector**, the “parent selector” that lets an element change its style based on what it contains.

### Related posts:

[CSS – Block & inline & inline-Block](https://blog.khanwaseem.com/css-block-inline-inline-block/)
[CSS – Measurement Units](https://blog.khanwaseem.com/css-measurement-units/)
[CSS – @ Rules](https://blog.khanwaseem.com/css-rules/)
[CSS – Margin](https://blog.khanwaseem.com/css-margin/)

Powered by [YARPP](https://yarpp.com).
