Sub topic 6
Building a reusable card that adapts to its container
This chapter assumes you know flexbox and grid, Tailwind utility classes, and responsive variants such as md:. It also assumes you have marked an element with @container and used variants like @md. Here you combine those pieces into one reusable component.
The problem with viewport breakpoints
Say you build a product card. On a wide page it sits in a three-column grid. In a sidebar, the same card is narrow, even on a large screen. If the card uses md:flex-row, it reacts to the viewport. On a large screen it switches to a row layout inside a 250px sidebar and looks cramped.
A reusable component should react to the space it is given, not to the screen. Container queries do this. The card asks how wide its parent is and picks a layout from the answer.
The design
The card has two layouts:
- Narrow container: image on top, text below, stacked in a column.
- Wide container: image on the left at a fixed width, text on the right.
The component splits into two parts. The wrapper is the query container. Inside it is the card layout, which holds the @md variants.
Step 1: the card
<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>The outer div carries @container. The inner div is a child, so it can use container variants. Below the md container size (28rem by default), the card is a column and the image fills the width. At @md and up, @md:flex-row makes it a row and @md:w-48 fixes the image width.
Notice that the variants sit on descendants of the container. An element cannot query itself. This matters in the next step.
Step 2: place the card in different layouts
Now put the same markup in two places. The grid below changes its own column count with a container variant, and each cell holds a card.
<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>This grid goes from one to two to four columns as its container widens. If each cell wraps the card from Step 1 in its own @container, each card measures its own cell. A card in a narrow cell stacks. A card in a wide cell becomes a row. Two cards on the same page can look different, and neither needs to know where it was placed.
Remember that the container is the card's wrapper, not the grid cell's contents alone. Put @container on the element whose width you want to measure.
Step 3: tune with max and range variants
Sometimes you want a layout rule that applies only below a size. @max-md applies when the container is narrower than md:
<div class="@container">
<div class="flex flex-row @max-md:flex-col">
<div>Left</div>
<div>Right</div>
</div>
</div>You can also combine @min-md and @max-xl to target a range:
<div class="@container">
<div class="hidden @min-md:@max-xl:block">Visible only between @md and @xl</div>
</div>For a card, a range is useful when you want a compact variant in the middle and a different layout at both extremes. Prefer the mobile-first style (@md: adds rules as space grows) for most work, and reach for @max-* when it removes an override.
Checklist for a reusable component
- Wrap the component in an element with
@container. - Write the narrow layout as the default, with no variant.
- Add
@sm,@mdand larger variants to widen the layout. - Do not use viewport variants such as
md:for the component's own layout. - Test the component in a narrow sidebar and in a wide main area.
Exercise
A card uses md:flex-row and sits in a 250px sidebar on a wide desktop screen. Why does it look cramped, and what two changes fix it?
Show answer
md: responds to the viewport width, which is large, so the row layout applies even though the sidebar is narrow. Fix it by adding @container to a wrapper around the card and replacing md:flex-row with @md:flex-row on the card's inner element.
Exercise
You want the card to stack in a column only when its container is narrower than md, and to stay a row otherwise. Write the flex classes.
Show answer
flex flex-row @max-md:flex-col on an element inside an @container wrapper.