Sub topic 13
Putting it together: common patterns
This chapter combines the pieces from earlier chapters into patterns you will use in real interfaces. It assumes you know Tailwind utility classes, the basics of CSS transitions and keyframes, and the earlier chapters on state variants, transition utilities, duration and easing, transforms, group and peer, built-in animations, and reduced motion. The examples work in Tailwind v3 and v4 unless a note says otherwise.
How to choose a pattern
Most interface motion falls into a few families. Pick the family first, then pick the utilities.
Pattern 1: A button with complete feedback
A good button gives feedback for hover, press, and keyboard focus. Each state uses a different variant, and one transition utility covers them all.
<button class="rounded bg-indigo-600 px-4 py-2 text-white transition duration-150 ease-out hover:bg-indigo-700 active:scale-95 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600 motion-reduce:transition-none">
Save
</button>hover:bg-indigo-700changes the color when the pointer is over the button.active:scale-95shrinks the button slightly while it is pressed.focus-visible:shows the outline for keyboard users and usually not for mouse clicks.motion-reduce:transition-noneremoves the animation for users who asked for less motion. The color still changes, just instantly.
Use the default transition here instead of transition-colors. The press effect scales the button, and transition-colors would not animate that.
Pattern 2: A card that lifts, with a nudging arrow
Combine transforms with a shadow change on the card. Then use group so a child reacts to hovering the whole card.
<a href="#" class="group block rounded-lg bg-white p-6 shadow transition duration-300 ease-out hover:-translate-y-1 hover:shadow-lg motion-reduce:transform-none motion-reduce:transition-none">
<h3 class="font-semibold text-slate-900">Release notes</h3>
<p class="mt-1 text-slate-600">See what changed this month.</p>
<span class="mt-4 inline-flex items-center gap-2 text-indigo-600">
Read more
<span class="transition-transform duration-200 group-hover:translate-x-1">→</span>
</span>
</a>The arrow has no hover rule of its own. group on the link and group-hover: on the arrow connect them. Keep the scale small. A card that grows by 5% pushes against its neighbors and looks jumpy in a grid, so a lift of -translate-y-1 with a larger shadow is usually enough.
motion-reduce:transform-none is a v3 idiom. In v4, translate, scale, and rotate are separate CSS properties, so transform-none no longer cancels them. Use motion-reduce:hover:translate-y-0 or wrap the lift in motion-safe:hover:-translate-y-1. The motion-safe: form works in both versions and is the safer habit:
<a href="#" class="group block rounded-lg bg-white p-6 shadow transition duration-300 ease-out hover:shadow-lg motion-safe:hover:-translate-y-1">
Card content
</a>Pattern 3: Loading indicators
Two built-in animations cover most loading states. A spinner signals work in progress. A pulsing placeholder (a skeleton) reserves space for content that has not arrived.
<div class="flex items-center gap-3">
<div class="size-6 rounded-full border-2 border-slate-300 border-t-indigo-600 motion-safe:animate-spin"></div>
<span class="text-slate-600">Loading…</span>
</div>
<div class="mt-6 space-y-3" aria-hidden="true">
<div class="h-4 w-3/4 rounded bg-slate-200 motion-safe:animate-pulse"></div>
<div class="h-4 w-1/2 rounded bg-slate-200 motion-safe:animate-pulse"></div>
</div>The spinner keeps its text label, so a user who sees no rotation still knows something is loading. The skeleton is hidden from screen readers because it carries no content. For a user who prefers reduced motion, the skeleton becomes a static gray block. That is acceptable, because the shape alone communicates "content goes here."
size-6 needs Tailwind v3.4 or later. On older v3 versions, use h-6 w-6.
Pattern 4: A floating label with peer
A floating label starts inside the input and moves up when the field has focus or contains text. The peer variant lets the label react to the input's state. The label must come after the input in the HTML, because peer-* only looks at earlier siblings.
<div class="relative">
<input id="email" type="email" placeholder=" " class="peer w-full rounded border border-slate-300 px-3 pb-2 pt-6 outline-none transition-colors duration-150 focus:border-indigo-600" />
<label for="email" class="pointer-events-none absolute left-3 top-4 origin-left text-slate-500 transition-transform duration-150 ease-out peer-focus:-translate-y-3 peer-focus:scale-75 peer-[:not(:placeholder-shown)]:-translate-y-3 peer-[:not(:placeholder-shown)]:scale-75">
Email
</label>
</div>The placeholder is a single space. This lets :placeholder-shown tell an empty field from a filled one without JavaScript. origin-left makes the label shrink toward its left edge instead of its center, so it does not drift sideways. pointer-events-none lets clicks on the label reach the input.
Pattern 5: A dropdown that fades and slides
This pattern combines group, opacity, a small translate, and visibility. Visibility matters because an invisible menu with opacity-0 still takes clicks and keyboard focus.
<div class="group relative inline-block">
<button class="rounded bg-slate-900 px-4 py-2 text-white">Menu</button>
<ul class="invisible absolute left-0 mt-2 w-40 translate-y-1 rounded bg-white py-1 opacity-0 shadow-lg transition-all duration-150 ease-out group-focus-within:visible group-focus-within:translate-y-0 group-focus-within:opacity-100 group-hover:visible group-hover:translate-y-0 group-hover:opacity-100">
<li><a href="#" class="block px-4 py-2 hover:bg-slate-100">Profile</a></li>
<li><a href="#" class="block px-4 py-2 hover:bg-slate-100">Sign out</a></li>
</ul>
</div>This uses transition-all on purpose. The default transition does not include visibility, so the menu would vanish instantly on close and cut the fade-out. With transition-all, visibility stays visible until the fade finishes. The group-focus-within: variants keep the menu reachable by keyboard.
Pattern 6: Elements that appear (Tailwind v4)
For a popover or dialog that is shown with display, v4 provides starting: and transition-discrete.
<div popover id="menu" class="opacity-0 transition-opacity transition-discrete duration-200 open:opacity-100 starting:open:opacity-0">
Menu content
</div>Without starting:open:opacity-0, the element would appear at full opacity because it has no earlier style to transition from. transition-discrete lets display flip at the right moment instead of at the start. This pattern is v4 only, and it relies on browser support for @starting-style. Check your target browsers.
Pitfalls to check before shipping
| Symptom | Likely cause | Fix |
|---|---|---|
| Hover snaps with no animation | No `transition` utility on the element | Add `transition` (or a specific `transition-*`) |
| Only the color animates, not the scale | `transition-colors` used with a transform | Use `transition` or `transition-transform` |
| Hover effect "flickers" at the edge | Scale moves the element out from under the pointer | Put the hover on a wrapper, scale the child |
| Menu fades in but cuts out on close | `visibility` is not in the transition list | Use `transition-all` or list `visibility` |
| `peer-*` does nothing | Peer element comes after the target | Move the peer first in the HTML |
| Reduced-motion users still see movement | Only transitions were disabled, not transforms | Wrap movement in `motion-safe:` |
Two more habits help. Keep durations between 100 and 300 ms for interface feedback, because longer values feel sluggish. Animate opacity, translate, scale, and rotate where you can, since the browser can draw them without recalculating layout. Animating width, height, top, or margin makes the browser lay out the page on every frame.
Exercise
A card uses hover:scale-105 but has no transition utility. It also lifts on hover for users who prefer reduced motion. Which two changes fix both problems?
Show answer
Add transition (with a duration such as duration-200) to the base class list so the scale animates. Change the effect to motion-safe:hover:scale-105 so users who prefer reduced motion get no movement.
Exercise
You build a floating label with peer, but the label never moves when the input is focused. The label is written before the input in the HTML. What is wrong, and what is the fix?
Show answer
The peer-* variants only match siblings that come after the element marked peer. Move the label after the input (and keep peer on the input). Position the label with absolute so it still appears inside the field.
Exercise
Why does the dropdown in Pattern 5 use transition-all and not transition-opacity?
Show answer
The menu toggles visibility as well as opacity and translate. transition-opacity does not include visibility, so the menu would turn invisible immediately on close and the fade-out would not show. transition-all keeps it visible until the transition ends.