Sub topic 3
Styling children with @sm, @md and @lg
This chapter assumes you know flexbox and grid, Tailwind utility classes, and responsive variants such as md: and lg:. It also assumes you have already marked an element with @container, which the previous chapter covered. Here you use container variants to restyle the children of that element.
What a container variant does
A container variant works like a responsive variant, with one difference. md: compares the viewport width to a breakpoint. @md: compares the width of the nearest ancestor marked with @container to a container size.
You write the variant as a prefix on any utility:
@sm:applies when the container is at least thesmcontainer size.@md:applies when the container is at least themdsize.@lg:applies when the container is at least thelgsize.
Like md:, these variants are mobile-first. An unprefixed utility is the base style. A prefixed utility takes over once the container is wide enough, and it stays in effect as the container grows.
A first example
The next snippet shows a product card. It is a column by default and becomes a row when its container is wide enough.
<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>Read it in two parts:
- The outer
divhas@container. It is the query container. - The inner
divand theimguse@md:variants. They respond to the width of the outerdiv.
When the outer div is narrower than @md, the card is a column and the image fills the width. At @md and wider, the card becomes a row and the image is w-48. The viewport size does not matter. If you put this card in a narrow sidebar on a wide desktop screen, it stays a column. If you put it in a wide main area on a small screen, it can still be a row.
Combining several variants
You can put more than one container variant on a single element. Each one adds a step.
<div class="@container">
<div class="grid grid-cols-1 gap-4 @sm:grid-cols-2 @2xl:grid-cols-4">
<div class="rounded bg-white p-4 shadow">One</div>
<div class="rounded bg-white p-4 shadow">Two</div>
<div class="rounded bg-white p-4 shadow">Three</div>
<div class="rounded bg-white p-4 shadow">Four</div>
</div>
</div>The grid has one column by default. It has two columns once the container reaches @sm, and four columns once it reaches @2xl. Because the variants are mobile-first, a container at the @lg size still gets two columns. The @sm rule is the last one that matches, and @2xl has not been reached.
How the nearest container is chosen
A container variant looks up the tree for the closest ancestor with @container. It uses that ancestor's width and ignores any further ones. If you have two nested containers, the inner one wins for its children. Chapter material on named containers later in this tutorial shows how to target the outer one instead.
Viewport variants versus container variants
| Question | Viewport variant (`md:`) | Container variant (`@md:`) |
|---|---|---|
| What is measured? | The browser window width | The nearest `@container` ancestor's width |
| Needs a marker element? | No | Yes, an ancestor with `@container` |
| Same component in a sidebar and a main area | Both look the same | Each adapts to its own space |
Common mistakes at this stage
- Forgetting
@container. If no ancestor is marked, the@md:classes have no container to measure, and the base styles stay in place. Check the markup first when a variant seems to do nothing. - Putting the variant on the container itself. The container's own children respond, but the container does not. Move the variant to a child.
- Mixing up
md:and@md:. The@is easy to drop.md:will respond to the window, which can look right until the component moves to another layout.
Exercise
A card has class="@container flex flex-col @md:flex-row" on a single element. Resizing the card's parent never changes it to a row. Why, and how do you fix it?
Show answer
The @md: variant is on the same element that has @container, but container variants only apply to descendants of the container. Wrap the content: put @container on an outer div and keep flex flex-col @md:flex-row on the inner div.