Sub topic 6
Building a reusable card that adapts to its container
This chapter builds one product card that works in a wide main column, a two-column grid, and a narrow sidebar. You don't change the markup or add props for each place. The card reads the width of the space it sits in and picks its own layout.
I assume you know flexbox and grid utilities, Tailwind's viewport variants such as md:, and the @container class and @md-style variants from the previous chapters. If you are unsure about those, review them first.
The problem with viewport breakpoints
Suppose you write a card with md:flex-row. At a 1280px viewport the card goes horizontal. Now place the same card in a 288px sidebar. The viewport is still 1280px, so the card still tries to lay out horizontally, and its image and text are squeezed into a few hundred pixels.
The component's layout depends on the width of its slot, not on the width of the window. Viewport variants can't see that. You would need a separate compact prop, or a copy of the component for sidebars.
Container queries fix this. You mark a wrapper with @container, and descendants use @md: and similar variants that compare against that wrapper's width.
Design the card's behavior first
Before writing classes, decide how the card should behave at each width. Container width, not viewport width, is the input.
| Container width | Layout | Details |
|---|---|---|
| Under 20rem (`@xs`) | Stacked | Description hidden, compact padding |
| 20rem up to 28rem | Stacked | Image on top, description shown |
| 28rem (`@md`) and up | Side by side | Square thumbnail beside the text |
| 32rem (`@lg`) and up | Side by side | Larger title and wider thumbnail |
These thresholds come from Tailwind's default container scale: @xs is 20rem, @md is 28rem, and @lg is 32rem. At the default browser font size, 28rem is 448px.
Write the card
The structure has two layers. The outer div carries @container. The inner article and its children carry the variants.
<div class="@container">
<article class="flex flex-col gap-3 rounded-lg bg-white p-3 shadow @md:flex-row @md:gap-6 @md:p-5">
<img
class="aspect-video w-full rounded object-cover @md:aspect-square @md:w-32 @md:shrink-0 @md:self-start @lg:w-44"
src="/photo.jpg"
alt="Trail running shoe in blue"
/>
<div class="flex flex-col gap-2">
<h2 class="text-base font-semibold @lg:text-xl">Trail running shoe</h2>
<p class="text-sm text-gray-600 @max-xs:hidden">
Light, grippy, and built for wet rock and loose gravel.
</p>
<a
href="/products/trail-shoe"
class="mt-2 inline-block self-start rounded bg-blue-600 px-3 py-1.5 text-sm font-medium text-white hover:bg-blue-700"
>
View details
</a>
</div>
</article>
</div>Here is what each part does.
- Stacked by default. Without a variant,
flex-colapplies. Every width starts as a stacked card, which is the safest layout for narrow slots. This is the same mobile-first rule you use withmd:. - Side by side from
@md.@md:flex-rowswitches the row direction once the container is at least 28rem wide. The padding and gap also grow at that point. - Image behavior. Stacked, the image is a full-width 16:9 banner. Side by side, it becomes a square 8rem thumbnail (
@md:w-32).@md:shrink-0stops the flex row from squeezing it.@md:self-startstops the defaultalign-items: stretchfrom overriding the square aspect ratio. At@lgthe thumbnail grows to 11rem. - Hidden description in tiny slots.
@max-xs:hiddenapplies only when the container is narrower than 20rem. Above that width the paragraph shows normally.
The @max-xs:hidden utility is a max variant. Use the unprefixed form for "this size and wider" and @max-* for "narrower than this size". You can stack @min-* and @max-* to target a band, for example @min-md:@max-xl: for widths from @md up to, but not including, @xl.
Use the card in different places
Here is a page that uses the same card in a main grid and in a sidebar. The card markup is identical in both places.
<div class="mx-auto flex max-w-6xl gap-6 p-6">
<main class="grid flex-1 grid-cols-1 gap-6 xl:grid-cols-2">
<div class="@container">
<article class="flex flex-col gap-3 rounded-lg bg-white p-3 shadow @md:flex-row @md:gap-6 @md:p-5">
<img
class="aspect-video w-full rounded object-cover @md:aspect-square @md:w-32 @md:shrink-0 @md:self-start @lg:w-44"
src="/photo.jpg"
alt="Trail running shoe in blue"
/>
<div class="flex flex-col gap-2">
<h2 class="text-base font-semibold @lg:text-xl">Trail running shoe</h2>
<p class="text-sm text-gray-600 @max-xs:hidden">Light, grippy, and built for wet rock.</p>
</div>
</article>
</div>
<!-- more cards, each wrapped in its own @container -->
</main>
<aside class="w-72 shrink-0">
<div class="@container">
<article class="flex flex-col gap-3 rounded-lg bg-white p-3 shadow @md:flex-row @md:gap-6 @md:p-5">
<img
class="aspect-video w-full rounded object-cover @md:aspect-square @md:w-32 @md:shrink-0 @md:self-start @lg:w-44"
src="/photo.jpg"
alt="Trail running shoe in blue"
/>
<div class="flex flex-col gap-2">
<h2 class="text-base font-semibold @lg:text-xl">Trail running shoe</h2>
<p class="text-sm text-gray-600 @max-xs:hidden">Light, grippy, and built for wet rock.</p>
</div>
</article>
</div>
</aside>
</div>Work through the widths on a 1280px-wide screen.
- The page content is capped at
max-w-6xl, which is 72rem or 1152px. Subtract 48px of padding to get 1104px. - The sidebar is
w-72, or 288px, plus a 24px gap. That leaves about 792px formain. - At the
xlviewport breakpoint,mainhas two columns. Each column is (792 − 24) / 2 = 384px, which is 24rem. That is below@md(28rem), so the cards in the grid are stacked. - The sidebar container is 288px, or 18rem, which is below
@xs. Its card is stacked and hides the description.
On a 1000px-wide screen, main is a single column about 640px wide. That is above 28rem, so each card is side by side.
So a card in a wide viewport can end up stacked, while a card in a smaller viewport ends up side by side. A viewport breakpoint can't produce that result, because it ignores the slot a component sits in. This is the behavior you want from a reusable component.
Keep the layout logic in the component
Avoid putting layout decisions into the parent. The parent should decide how much space the card gets (a column, a grid cell, a sidebar), not how the card arranges its insides. If the parent also sets flex-row or hides the image, you are back to coupling the component to its slot.
A practical way to keep this boundary is to ship the card as a component that renders its own @container wrapper. Here is a small version, using a JavaScript template function as a stand-in for a framework component.
function productCard({ title, description, image, href }) {
return `
<div class="@container">
<article class="flex flex-col gap-3 rounded-lg bg-white p-3 shadow @md:flex-row @md:gap-6 @md:p-5">
<img
class="aspect-video w-full rounded object-cover @md:aspect-square @md:w-32 @md:shrink-0 @md:self-start @lg:w-44"
src="${image}"
alt="${title}"
/>
<div class="flex flex-col gap-2">
<h2 class="text-base font-semibold @lg:text-xl">${title}</h2>
<p class="text-sm text-gray-600 @max-xs:hidden">${description}</p>
<a href="${href}" class="mt-2 inline-block self-start rounded bg-blue-600 px-3 py-1.5 text-sm font-medium text-white">View details</a>
</div>
</article>
</div>`;
}
console.log(productCard({
title: "Trail running shoe",
description: "Light, grippy, and built for wet rock.",
image: "/photo.jpg",
href: "/products/trail-shoe",
}));Every class name appears in full in the source. Tailwind finds class names by scanning your files for complete strings, so don't build them from fragments like ` @${size}:flex-row `. Such a class will not be generated.
Escape or sanitize title, description, and image if they come from user input. The sketch above does not.
Common problems with the card
These are the failures you are most likely to see while building a component like this.
- A variant does nothing. The element carrying
@md:must be a descendant of an element with@container. If no ancestor has it, the variant never matches. Check the browser's DevTools forcontainer-type: inline-sizeon a parent. - The variant is on the container itself. A container can't query its own size. If you write
class="@container @md:flex-row"on one element, the@md:part has nothing to compare against. Put the variants on a child, which is why the card uses a wrapperdivand an innerarticle. - The container collapses to zero width.
@containersetscontainer-type: inline-size, so the element's width can no longer depend on its contents. In a block layout or grid cell this is fine, because the width comes from the parent. In a flex row withwidth: auto, the wrapper can shrink to nothing. Give it an explicit width or a flex rule such asflex-1orw-72. - The layout depends on the card's own height.
inline-sizecontainment only measures width. Variants respond to the container's width, never its height. - A thumbnail stretches into a tall rectangle. In a flex row, children stretch by default and override the aspect ratio. Add
self-startas the card does. - The wrong container responds. When containers nest, a child responds to the nearest ancestor with
@container. The next chapter covers named containers for targeting a specific ancestor.
If your project is on Tailwind v3, install and enable @tailwindcss/container-queries in your config first. Tailwind v4 includes the support in core.
Check your understanding
Exercise
The card below is placed in a grid cell that is 30rem wide, but it never switches to a side-by-side layout. Give two possible causes of the bug and the fix for each. <article class="@container flex flex-col @md:flex-row">...</article>
Show answer
First cause: @container and @md:flex-row are on the same element. A container can only be queried by its descendants, so @md: has no ancestor container to measure. Fix: wrap the article in a div class="@container" and keep the variants on the article. Second possible cause: if the element is a flex item with auto width, inline-size containment can collapse its width, so the container measures far less than 30rem. Fix: give the wrapper an explicit width or flex-1. In a grid cell the width comes from the grid, so the first cause is the more likely one.
Exercise
Change the card so the description is hidden when its container is narrower than 20rem and also when its container is 48rem or wider, so it shows only in between. Which classes do you put on the paragraph?
Show answer
Use @max-xs:hidden @min-3xl:hidden on the paragraph. @max-xs: applies below 20rem. @min-3xl: applies at 48rem and wider, because the 3xl container size is 48rem. Together they hide the paragraph at both ends. An alternative that makes the visible range explicit is hidden @min-xs:@max-3xl:block.