Sub topic 16
Putting it together: a card component
This chapter combines the utilities from earlier chapters into one card. It assumes you can read Tailwind class lists, know the box model, and have a working Tailwind project or playground. It uses the default palette, so it works the same in v3 and v4.
Plan the card in layers
Build the card from the outside in. Each group of classes has one job.
| Layer | Utilities | Purpose |
|---|---|---|
| Shape | `rounded-xl` | Rounds the corners |
| Edge | `border border-slate-200` | Defines the edge with a 1px line |
| Surface | `bg-white text-slate-900` | Sets the background and default text color |
| Depth | `shadow-md` | Lifts the card off the page |
| Dark mode | `dark:` variants | Replaces the colors when dark mode is active |
The base card
<div class="rounded-xl border border-slate-200 bg-white p-6 text-slate-900 shadow-md dark:border-slate-700 dark:bg-slate-800 dark:text-slate-100">
<h2 class="text-lg font-semibold">Project status</h2>
<p class="mt-2 text-slate-600 dark:text-slate-300">All checks passed.</p>
</div>Every light-mode color has a dark: partner. In dark mode the border, background, and text each swap to a different shade. If you forget one, that part keeps its light color and can become unreadable. For example, text-slate-600 on bg-slate-800 has poor contrast.
Add structure and an action
Use divide-y to separate sections without adding a border class to each one. Add a button with a focus ring so keyboard users can see where they are.
<div class="overflow-hidden rounded-xl border border-slate-200 bg-white text-slate-900 shadow-md dark:border-slate-700 dark:bg-slate-800 dark:text-slate-100">
<div class="divide-y divide-slate-200 dark:divide-slate-700">
<div class="p-6">
<h2 class="text-lg font-semibold">Project status</h2>
<p class="mt-2 text-slate-600 dark:text-slate-300">All checks passed.</p>
</div>
<div class="p-4">
<button class="rounded-md bg-indigo-600 px-4 py-2 text-white hover:bg-indigo-500 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2 dark:focus:ring-offset-slate-800">
View details
</button>
</div>
</div>
</div>Three details matter here:
overflow-hiddenclips child backgrounds to the rounded corners. Without it, a colored child section can poke pastrounded-xl.divide-slate-200needs its owndark:partner, because divider color is a separate setting from border color.focus:ring-offset-2leaves a gap between the button and the ring. The offset color defaults to white, sodark:focus:ring-offset-slate-800makes the gap match the dark card.
Check contrast
Before you ship, check each text and background pair in both modes. Slate 600 on white and slate 300 on slate 800 are both readable. Lighter shades such as slate 400 on white often fail the WCAG 4.5:1 ratio for body text. A focus ring should also stand out against the surface around it, which is why the offset color is set explicitly.
Exercise
The card's top section has a bg-slate-50 header that pokes out past the rounded corners. Which class on the outer card fixes this, and why does it work?
Show answer
Add overflow-hidden to the outer card. It clips child content, including backgrounds, to the card's rounded border box, so the corners stay round.
Exercise
You add dark:bg-slate-800 to the card but the dividers still look bright in dark mode. What is missing?
Show answer
A dark:divide-slate-700 class (or a similar dark shade) on the element that has divide-y. Divider color is set separately from border and background colors.