Sub topic 15
Putting it together: a responsive article card
This chapter combines the spacing, sizing, and typography utilities from earlier chapters into one component. You will build the card step by step and then review the decisions behind each class.
It assumes you can read basic HTML and know the CSS box model. It also assumes you have a project with Tailwind CSS v3.4 or later (or v4) installed. size-* needs v3.4, and line-clamp-* and text-base/7 need v3.3.
What we are building
The card has a thumbnail area, a category label, a title, a summary, and a footer row with an author and a link.
- On a phone, the thumbnail stacks above the text.
- From the
mdbreakpoint (768px) upward, the thumbnail sits beside the text. - Padding, gaps, and the title size grow at the same breakpoint.
Step 1: the outer container
Start with the box that sets the card's width and padding.
<article class="mx-auto flex max-w-3xl flex-col gap-4 p-4 md:flex-row md:gap-8 md:p-8">
</article>Each part has a job:
max-w-3xlcaps the width at 48rem. The card never grows past that, but it can still shrink on a narrow screen. A fixedw-*would not shrink.mx-autosets both horizontal margins toauto. This centers the card only because it has a constrained width.flex flex-colstacks the children vertically.gap-4puts 1rem between them.gapworks here without any margin on the children.p-4gives 1rem of padding on all sides.md:p-8raises it to 2rem from 768px up.md:flex-rowswitches the direction at the breakpoint.md:gap-8widens the gap at the same time.
The unprefixed classes are the phone layout. The md: classes override them at wider screens. This is the mobile-first model, so you never write a "phone only" class.
Step 2: the thumbnail
<div class="h-40 w-full shrink-0 rounded-lg bg-gray-200 md:h-auto md:w-1/3" aria-hidden="true"></div>On a phone, the thumbnail is h-40 (10rem) tall and w-full wide. From md up, md:w-1/3 makes it one third of the card's width. md:h-auto removes the fixed height.
In a row flex container, the default align-items: stretch makes the thumbnail as tall as the text column. h-40 and md:h-auto both set height. The md: rule wins because Tailwind emits variant rules after the plain ones, and not because of the order of the classes in your HTML.
shrink-0 stops the flex container from squeezing the thumbnail when the text is long.
Step 3: the text column
<div class="min-w-0 flex-1">
<p class="text-sm font-medium uppercase tracking-wide text-indigo-600">Tutorial</p>
<h2 class="mt-2 text-2xl font-semibold leading-snug tracking-tight text-gray-900 md:text-3xl">
Spacing and type in practice
</h2>
<p class="mt-3 line-clamp-3 text-base/7 text-gray-600">
Utility classes map one-to-one to CSS declarations. Once you know the
spacing scale, you can read and write most layouts without leaving your HTML.
This summary is deliberately long so that the clamp has something to cut.
</p>
</div>flex-1 lets the column take the space the thumbnail leaves. min-w-0 is needed for truncation, as the section on common errors explains.
The text styles work in three tiers:
| Element | Classes | Why |
|---|---|---|
| Label | `text-sm font-medium uppercase tracking-wide` | Small text in capitals looks tight, so `tracking-wide` adds letter spacing. |
| Title | `text-2xl font-semibold leading-snug tracking-tight` | Large text needs less line height and slightly tighter tracking than body text. |
| Summary | `text-base/7 line-clamp-3` | The `/7` suffix sets a 1.75rem line height. `line-clamp-3` cuts the text after three lines. |
text-2xl also sets a default line height, and leading-snug overrides it. If you used text-2xl/8 instead, one class would set both values. Use whichever form is easier to read in your codebase.
The vertical rhythm comes from top margins: mt-2 under the label, mt-3 under the title. Spacing from one side only is easier to reason about than margins on both sides. It also avoids relying on margin collapsing, which does not happen between flex items.
Step 4: the footer row
<div class="mt-6 flex items-center gap-4">
<span class="size-10 shrink-0 rounded-full bg-gray-300"></span>
<p class="truncate text-sm font-medium text-gray-900">Ada Lovelace, Staff Writer, Analytical Engine Quarterly</p>
<a href="#" class="ml-auto shrink-0 text-sm font-medium text-indigo-600">Read more</a>
</div>size-10makes a 2.5rem square for the avatar. Before v3.4 you would writeh-10 w-10.items-centeraligns the avatar, name, and link on their vertical middles.ml-autois an auto margin inside a flex row. It absorbs all the free space on the left of the link and pushes the link to the right edge.truncateis shorthand foroverflow-hidden,text-overflow: ellipsis, andwhite-space: nowrap. It keeps a long name on one line.shrink-0on the avatar and the link means only the name gives up space when the row is tight.
The complete card
This file loads Tailwind's Play CDN, so you can save it as card.html and open it in a browser without a build step. The CDN is for experiments only, not for production.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://cdn.tailwindcss.com"></script>
<title>Article card</title>
</head>
<body class="bg-gray-50 p-4">
<article class="mx-auto flex max-w-3xl flex-col gap-4 rounded-xl bg-white p-4 shadow md:flex-row md:gap-8 md:p-8">
<div class="h-40 w-full shrink-0 rounded-lg bg-gray-200 md:h-auto md:w-1/3" aria-hidden="true"></div>
<div class="min-w-0 flex-1">
<p class="text-sm font-medium uppercase tracking-wide text-indigo-600">Tutorial</p>
<h2 class="mt-2 text-2xl font-semibold leading-snug tracking-tight text-gray-900 md:text-3xl">
Spacing and type in practice
</h2>
<p class="mt-3 line-clamp-3 text-base/7 text-gray-600">
Utility classes map one-to-one to CSS declarations. Once you know the
spacing scale, you can read and write most layouts without leaving your HTML.
This summary is deliberately long so that the clamp has something to cut.
</p>
<div class="mt-6 flex items-center gap-4">
<span class="size-10 shrink-0 rounded-full bg-gray-300"></span>
<p class="truncate text-sm font-medium text-gray-900">Ada Lovelace, Staff Writer, Analytical Engine Quarterly</p>
<a href="#" class="ml-auto shrink-0 text-sm font-medium text-indigo-600">Read more</a>
</div>
</div>
</article>
</body>
</html>Resize the window across 768px and watch the thumbnail move from the top to the left. The summary stays at three lines, and the author name is cut with an ellipsis when space runs out.
Choosing between similar utilities
| Need | Prefer | Avoid |
|---|---|---|
| Space between flex or grid children | `gap-*` | Margins on every child |
| Space between stacked block children | `space-y-*` | Per-child `mt-*` that you must remember to omit on the first child |
| Centered, width-limited block | `mx-auto max-w-*` | A fixed `w-[640px]` |
| Square element | `size-*` | `h-*` and `w-*` repeated |
| Text that reads well | `max-w-prose` on the text container | Letting body copy span a full desktop width |
| Value that appears once | `w-[320px]` | A new theme entry |
| Value that repeats | A theme entry such as `gutter` | The same bracket value in many files |
Common errors
Truncation does not work in a flex row. A flex item's minimum width defaults to its content width, so a long unbroken line pushes the row wider instead of being cut. Add min-w-0 to the flex item that contains the truncated text. The text column above does this.
Line clamping and fixed heights fight each other. line-clamp-3 sets its own overflow and display behavior. Do not add h-* or overflow-visible to the same element.
space-y-* and gap-* on the same container. On a flex container, gap-* and space-y-* stack, and you get double spacing. Pick one.
Mixing up the prefixes. md: means "from 768px up", not "only on medium screens". If a style should apply to phones only, leave it unprefixed and override it at md:.
Using max-w-prose on the whole card. prose means 65 characters, so the width depends on the font size. That suits a text column. For a card with a thumbnail, a size-based cap like max-w-3xl is more predictable, and max-w-prose can go on the text inside.
Classes built from strings. Tailwind scans your source for complete class names. ` gap-${n} will not generate any CSS. Write out full names such as gap-4 and gap-8`, and choose between them in code.
Extending the card with a theme value
If the card's p-8 and gap-8 should become a named "gutter" used across the site, add one theme value instead of repeating [2rem]. In v3 that goes under theme.extend.spacing in tailwind.config.js. In v4 you add --spacing-gutter inside an @theme block. After that, md:p-gutter and md:gap-gutter work like any other scale value. Do this only for values that repeat. A one-off value belongs in square brackets.
Exercise
The author name in the footer row does not show an ellipsis. It pushes the "Read more" link out of the card instead. The name element has truncate. What is missing, and which element needs it?
Show answer
The flex item that holds the text needs min-w-0. In this card the text column already has it, so the missing piece is on the footer row's children. Make sure the name <p> can shrink. It has truncate, and its siblings have shrink-0. If the name still overflows, check that no ancestor flex item lacks min-w-0, because a flex item's default minimum width equals its content width and blocks shrinking.
Exercise
Change the card so the thumbnail appears on the right from md up, and the gap between the thumbnail and the text is 3rem from the lg breakpoint up. Which classes do you change or add?
Show answer
Replace md:flex-row with md:flex-row-reverse on the article, and add lg:gap-12. The scale value 12 equals 3rem, and lg:gap-12 overrides md:gap-8 from 1024px up.