Sub topic 16
Putting it together: a card component
This chapter combines the earlier utilities into one card: colors, opacity, borders, radius, dividers, shadows, focus rings, hover states, and dark mode. It assumes you know the individual utilities from the previous chapters, such as bg-white, border-slate-200, shadow-md, and the hover: and dark: prefixes. It also assumes you can run a Tailwind v3 or v4 project. The class names used here work in both versions.
Decide what each utility is responsible for
Before writing classes, assign each visual concern to one group of utilities. This keeps the class list readable and makes later changes easy to locate.
| Concern | Utilities used | Notes |
|---|---|---|
| Surface | `bg-white`, `text-slate-900` | Light-mode base colors |
| Shape | `rounded-xl`, `overflow-hidden` | Radius, plus clipping of children |
| Edge | `border`, `border-slate-200` | Defines the card boundary |
| Depth | `shadow-md`, `shadow-slate-900/5` | Size plus a tinted, low-opacity color |
| Sections | `divide-y`, `divide-slate-200` | Lines between header, body, footer |
| Interaction | `hover:shadow-lg`, `focus-visible:ring-2` | Feedback on the card and its button |
| Dark mode | `dark:bg-slate-800`, `dark:border-slate-700` | Replaces each light color |
The diagram shows the layers from the outside in.
Step 1: The surface
Start with the container. It sets the background, text color, border, radius, and shadow, with a dark: replacement for each color.
<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>A useful rule when writing this: every color utility without a dark: partner is a decision. Border and background almost always need a partner. A shadow often does not, as covered below.
Step 2: Sections separated by dividers
Real cards have sections. Instead of adding a border-b to each child, put divide-y on the parent. It adds a top border to every child except the first, so you never get a stray line at the edges.
Two details matter here:
- Section backgrounds, such as a tinted footer, are rectangles. They do not follow the parent's rounded corners unless the parent has
overflow-hidden. divide-*color utilities need their own dark partner.divide-slate-200stays light in dark mode unless you adddark:divide-slate-700.
Step 3: Tinted shadows that behave in both themes
shadow-md uses a default black shadow at low opacity. You can recolor it with a slash opacity, for example shadow-slate-900/5, so the shadow matches the card's palette instead of looking gray and muddy.
Shadows have a limit in dark mode. On a dark page, a dark shadow is almost invisible, so the border carries the edge. For that reason the full component below sets dark:shadow-none and relies on dark:border-slate-700. On hover, an indigo-tinted larger shadow adds feedback. Pair it with transition-shadow so the change animates instead of snapping.
Step 4: Focus and hover on the button
The footer button needs three states: normal, hover, and keyboard focus. Use focus-visible: rather than focus: for buttons. It shows the ring for keyboard users and usually not for mouse clicks.
Because the footer has a tinted background, add a ring offset so the ring does not touch the button edge. The offset color must match the surface behind the button, otherwise a mismatched halo shows. That means a dark-mode partner: dark:ring-offset-slate-900.
The complete component
This version combines everything. The status badge uses a low-opacity ring and background so it works on both themes with only the color shade changed.
<article class="divide-y divide-slate-200 overflow-hidden rounded-xl border border-slate-200 bg-white text-slate-900 shadow-md shadow-slate-900/5 transition-shadow hover:shadow-lg hover:shadow-indigo-500/20 dark:divide-slate-700 dark:border-slate-700 dark:bg-slate-800 dark:text-slate-100 dark:shadow-none">
<header class="flex items-center justify-between p-6">
<h2 class="text-lg font-semibold">Project status</h2>
<span class="rounded-full bg-emerald-50 px-2 py-0.5 text-xs font-medium text-emerald-700 ring-1 ring-inset ring-emerald-600/20 dark:bg-emerald-400/10 dark:text-emerald-400 dark:ring-emerald-400/20">
Passing
</span>
</header>
<div class="p-6">
<p class="text-slate-600 dark:text-slate-300">
All 42 checks passed on the main branch.
</p>
</div>
<footer class="flex justify-end bg-slate-50 p-4 dark:bg-slate-900/50">
<button class="rounded-md bg-indigo-600 px-4 py-2 text-sm font-medium text-white hover:bg-indigo-500 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-indigo-500 focus-visible:ring-offset-2 focus-visible:ring-offset-slate-50 dark:focus-visible:ring-offset-slate-900">
View details
</button>
</footer>
</article>Enabling dark mode
The dark: variants only activate if dark mode is on. By default, they follow the operating system's prefers-color-scheme setting. To toggle with a class on <html> instead, configure it once.
In Tailwind v3, set darkMode in the config:
// tailwind.config.js (Tailwind CSS v3)
module.exports = {
darkMode: 'class',
content: ['./src/**/*.{html,js}'],
}In Tailwind v4, redefine the variant in CSS:
/* app.css (Tailwind CSS v4) */
@import "tailwindcss";
@custom-variant dark (&:where(.dark, .dark *));After that, adding class="dark" to <html> switches every dark: utility.
Moving brand colors into the theme
If this card appears across the product, indigo-600 should become a named color. Define brand as shown in the earlier theme chapter, then replace bg-indigo-600, hover:bg-indigo-500, and ring-indigo-500 with bg-brand-600, hover:bg-brand-500, and ring-brand-500. A rebrand then becomes a one-place change. Keep arbitrary values such as shadow-[0_4px_12px_rgba(0,0,0,0.15)] for genuine one-offs, not for values that repeat.
Checking contrast
Colors that look fine can still fail. Check these pairs with a contrast checker, using the 4.5:1 minimum for normal text and 3:1 for UI component boundaries and focus indicators:
text-slate-600onbg-whiteis about 7.6:1, which passes comfortably.text-slate-300onbg-slate-800is well above 4.5:1, so the dark-mode body text passes.text-whiteonbg-indigo-600is about 6.3:1. On hover,bg-indigo-500is lower, near the 4.5:1 limit. Check it before shipping, or hover to a darker shade instead.border-slate-200on white is about 1.2:1. That is acceptable for a decorative card edge, because the content is readable without it. It would fail for the boundary of a text input, which should use something likeborder-slate-500.
The focus ring is a UI indicator, so ring-indigo-500 against the footer background should reach 3:1. Never remove the outline without replacing it, as the focus:outline-none plus ring pairing in the earlier input example does.
Common mistakes
- Forgetting a dark partner for
divide-*,ring-offset-*, orbg-*on sections, which leaves light patches in dark mode. - Placing
overflow-hiddenon the card and then clipping a shadow on a child. Shadows on the card itself are fine, because they render outside its box. Only children's overflow is clipped. - Using
focus:on a button that is clicked often, which shows the ring on every mouse click. - Building class names dynamically, such as `
bg-${color}-500`. Tailwind scans for complete class strings, so these classes are never generated. Write the full names, or map each variant to a full string.
Exercise
The card's footer has bg-slate-50, but its bottom corners look square even though the card has rounded-xl. What is the fix, and what trade-off does it carry?
Show answer
Add overflow-hidden to the card container so child backgrounds are clipped to the rounded corners. The trade-off is that anything extending outside the card's box, such as an absolutely positioned dropdown inside it, is clipped too.
Exercise
In dark mode, the card shows a light line between its sections and a bright halo around the focused button. Which two utilities are missing dark-mode partners?
Show answer
divide-slate-200 needs dark:divide-slate-700, and the focus ring offset (focus-visible:ring-offset-slate-50) needs dark:focus-visible:ring-offset-slate-900, so the offset matches the dark footer background.