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. It assumes you know the spacing scale, p-/m- utilities, flex gap-*, max-w-*, font size and leading utilities, and the md: breakpoint prefix. It also assumes Tailwind v3.4 or later, or v4.
The plan before the classes
Decide what each part of the card needs before you write any classes.
| Concern | Utility choice |
|---|---|
| Centered, limited width | `mx-auto max-w-prose` |
| Breathing room that grows | `p-4 md:p-8` |
| Heading hierarchy | `text-2xl font-semibold leading-snug`, larger at `md:` |
| Body text | `text-base/7 text-gray-600` |
| Gaps between blocks | `mt-*` on siblings |
| Gaps inside a row | `flex gap-*` |
Style the smallest screen first. Unprefixed utilities are the mobile layout, and prefixed ones override them as the screen widens.
The card
<article class="mx-auto max-w-prose p-4 md:p-8">
<p class="text-xs font-medium uppercase tracking-wide text-indigo-600">
Guide
</p>
<h2 class="mt-2 text-2xl font-semibold leading-snug md:text-3xl">
Spacing, sizing, and type in one component
</h2>
<p class="mt-3 line-clamp-3 text-base/7 text-gray-600 md:text-lg/8">
A card is a good test of the utilities you have learned. Each class maps
to one CSS declaration, so you can read the layout straight from the
markup. Long summaries are cut after three lines with an ellipsis, which
keeps a list of cards the same height.
</p>
<div class="mt-6 flex flex-col gap-2 sm:flex-row sm:gap-4">
<a href="#" class="text-sm font-medium text-indigo-600">Read more</a>
<a href="#" class="text-sm font-medium text-gray-500">Share</a>
</div>
</article>How the pieces work
- Width.
mx-autocenters the block, but only becausemax-w-proselimits its width (about 65 characters). Without a width limit, the block fills its parent and there is nothing to center. - Padding and box sizing. Tailwind's base styles set
box-sizing: border-box. The padding therefore counts insidemax-w-prose, and the text column is slightly narrower than 65 characters. - Type hierarchy. The eyebrow label (the small line above the heading) is small, uppercase, and tracked out. The heading is bold and tight. The body is lighter in color and has a roomy line height.
text-base/7sets size and line height together, andmd:text-lg/8replaces both at the breakpoint. - Vertical rhythm. Each block carries its own top margin (
mt-2,mt-3,mt-6). Spacing flows downward from the previous sibling, so removing the first element does not leave a stray margin at the top. - Link row. The links stack in a column on mobile with a small gap. From
sm:upward they sit in a row with a larger gap.gap-*andflex-colwork together here, becausegapapplies in either direction.
Common errors
- Putting
text-lgandtext-lg/8on the same element. Both set line height, and only one of them wins. - Using
space-y-*on a flex or grid container. It adds margins to children, so it behaves poorly withflex-row. Usegap-*instead. - Adding
md:classes without a base value.md:p-8leaves the phone layout with no padding. - Using
line-clamp-3on text inside a container with a fixed height. The clamp needs the text to size itself.
Exercise
The card needs a 12-unit-wide square avatar to the left of the heading from md: upward, with 4 units of space between them, and nothing beside the heading on mobile. Which classes do you add to the wrapper and the avatar?
Show answer
Wrap the avatar and heading in a div with md:flex md:gap-4, so the layout stays a normal block on mobile. Give the avatar hidden md:block size-12 shrink-0. The heading stays unchanged. size-12 sets width and height together, and shrink-0 stops the flex row from squeezing the avatar.