# Container Queries

> Style elements based on the size of their parent container, not the whole browser viewport, unlocking true component reusability.

Subject: CSS | Lesson 27 of 44 | 6 min read
Interactive lesson: https://codewithanimation.in/css/container-queries
Previous: [Media Queries & Mobile-First](https://codewithanimation.in/css/media-queries.md) | Next: [Fluid Type & Responsive Units](https://codewithanimation.in/css/fluid-typography-and-units.md)

## What they are

Container Queries (`@container`) are conditional CSS rules, just like Media Queries. But instead of checking the width of the entire browser window, they check the width of a specific parent element (the container).

This is one of the most powerful modern CSS features, allowing components to govern their own layout independently of where they are placed on the page.

## Why it matters

Imagine you build a beautiful 'Product Card' component. You want it to display horizontally (image left, text right) when there's plenty of space, and vertically (stacked) when space is tight.

If you use Media Queries, the card only knows about the browser window. If a user is on a massive desktop screen, but you place the card inside a narrow 300px sidebar, the media query sees the giant screen and applies the horizontal layout, breaking the card out of the sidebar!

Container queries solve this by letting the card ask: 'How much space do I actually have right here in this sidebar?'

## How it works

1. **Define the Container**: First, you must tell the browser which parent element is the container. You do this by applying `container-type: inline-size;` to the parent wrapper.

2. **Query the Container**: Then, on the child elements, you write a query using `@container (min-width: 400px) { ... }`.

Now, regardless of the browser size, the styles will only apply if that specific parent wrapper is at least 400px wide.

## Common mistake: Forgetting the container-type

The most common mistake with container queries is writing `@container` rules but forgetting to set `container-type: inline-size;` on the parent element. If you don't define a container, the browser doesn't know what to measure, and your container queries will silently fail to do anything.

## Tip: Tailwind Container Queries

Tailwind v4 supports container queries out of the box! You apply the `@container` class to the parent wrapper. Then, on the child elements, you use modifiers like `@md:flex-row` instead of `md:flex-row`. The `@` prefix tells Tailwind to look at the container size, not the screen size.

## Try it: Try it

Resize the parent container by dragging the handle. Watch how the card inside automatically adapts its layout from a vertical stack to a horizontal layout based on the space it has, completely ignoring the browser window size.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/css/container-queries

## Check yourself

### Question 1

What is the primary difference between a media query and a container query?

- A. Media queries measure the browser viewport, container queries measure a specific parent element.
- B. Media queries are for mobile, container queries are for desktop.
- C. Container queries are written in HTML, media queries are written in CSS.

Correct answer: A

Media queries (`@media`) look at the size of the entire browser window. Container queries (`@container`) look at the size of the specific parent element they are placed inside.

- Why not B: Both can be used on any device. The difference is what they measure.
- Why not C: Both are CSS features.

### Question 2

What CSS property must you apply to a parent element before you can use container queries on its children?

- A. container-type: inline-size;
- B. display: container;
- C. position: relative;

Correct answer: A

You must establish a container context by setting `container-type: inline-size` (or `normal` or `size`) on the parent. Without this, the browser won't track its dimensions for querying.

- Why not B: There is no 'display: container' value in CSS.
- Why not C: While often used for positioning, it has nothing to do with container queries.

### Question 3

In Tailwind CSS, how do you apply a flex-row layout only when the container is wider than the 'md' breakpoint?

- A. @md:flex-row
- B. md:flex-row
- C. container-md:flex-row

Correct answer: A

Tailwind uses the `@` symbol to denote container queries. Adding the `@container` class to the parent, and `@md:` modifiers to the children achieves this.

- Why not B: This is a standard media query based on the viewport, not the container.
- Why not C: Tailwind uses the `@` prefix for container queries.

## Remember this

- Container queries allow components to respond to their parent's width instead of the browser viewport.
- This makes components truly modular and reusable in any context (like a narrow sidebar).
- You must define a container context using `container-type: inline-size`.
- Tailwind uses the `@container` class on parents, and `@sm:`, `@md:` modifiers on children.
