Sub topic 1
Why Container Queries: Viewport Versus Container
Prerequisites
This chapter assumes you already know the following:
- Basic HTML and CSS layout, including flexbox and grid.
- Tailwind utility classes such as
flex,gap-4, andp-4. - Responsive variants such as
md:andlg:. - What a media query is, and that breakpoints are usually based on the viewport width.
The problem with viewport breakpoints
Tailwind's md: and lg: variants are built on media queries. A media query looks at the viewport, which is the visible area of the browser window. When the window is at least 768px wide, md: styles apply. This works well for page-level layout, such as showing a sidebar on wide screens.
It works poorly for reusable components. A component does not know how much room it has. Think of a product card. On a wide screen it might sit in a narrow sidebar, or it might fill the main column. The viewport width is the same in both places, but the space available to the card is very different.
With viewport breakpoints, you often end up writing the card's layout around where it is placed. You might add extra props, override classes per page, or write separate variants of the same card. That makes the component harder to reuse.
The container query model
A container query asks a different question. It does not ask "how wide is the window?" It asks "how wide is the nearest ancestor I have chosen to measure?"
You mark one ancestor as a query container. Its descendants can then change their styles based on that container's width. The same card placed in a 300px sidebar and in a 900px main column will pick different layouts, even though the viewport is the same size in both cases.
| Feature | Viewport query (`md:`) | Container query (`@md:`) |
|---|---|---|
| Measures | Browser window width | Width of the nearest marked ancestor |
| Needs a marked ancestor | No | Yes, an element with `@container` |
| Best for | Page-level layout | Reusable components |
| Reacts to sidebar or panel resizing | No | Yes |
The diagram shows the difference in what each kind of query measures.
A first example
Here is the core idea in code. The outer div is the query container. Its child switches from a column to a row once the container is at least @md wide, whatever the viewport width.
<div class="@container">
<div class="flex flex-col gap-4 @md:flex-row">
<img class="w-full @md:w-48" src="/photo.jpg" alt="Product photo" />
<div>
<h2 class="text-lg font-semibold">Product name</h2>
<p class="text-sm text-gray-600">Short description of the product.</p>
</div>
</div>
</div>Three things are worth noticing:
@containermarks the outerdivas the container whose width is measured.@md:flex-rowis a container variant. The@prefix tells Tailwind to use the container's width, not the viewport's.- Without a prefix,
flex-colis the default. The row layout applies only when the container is wide enough.
If you place this markup in a narrow sidebar, the image stacks above the text. If you place it in a wide column, they sit side by side. You do not change the component to get this behavior.
Choosing between the two
Use both kinds of query. They solve different problems.
- Use viewport variants (
md:,lg:) for the overall page: navigation, the number of page columns, and global spacing. - Use container variants (
@md:,@lg:) for components that appear in several places or that live inside resizable regions.
A common pattern is a page layout that uses viewport variants to decide the width of a sidebar, and cards inside it that use container variants to adapt to whatever width the sidebar gives them.
What comes next
This chapter covered why container queries exist and how they differ from viewport queries. The next chapters show how to mark containers, use the full set of variants, and build components that adapt to their surroundings.
Exercise
A card uses md:flex-row and sits in a 280px sidebar on a 1400px-wide screen. Does the card show a row layout? What would you change to make the card respond to its sidebar width instead?
Show answer
Yes, it shows a row layout. md: reads the viewport, which is 1400px, so the card stays in a row even though the sidebar is only 280px wide. To respond to the sidebar, add @container to an ancestor of the card and change md:flex-row to @md:flex-row. The card then stays in a column until its container is at least the @md size.
Exercise
Name one part of a page that suits viewport variants and one component that suits container variants.
Show answer
Page-level navigation, or the number of page columns, suits viewport variants because it depends on the window size. A product card that appears in both a sidebar and a main column suits container variants because it depends on the space it is given.