Sub topic 13
Putting it together: common patterns
This chapter combines the pieces from earlier chapters into patterns you will reuse. It assumes you know Tailwind utility classes, state variants such as hover: and focus-visible:, and the basics of CSS transitions and keyframes. It also assumes you have read the earlier chapters on transitions, transforms, group and peer variants, and reduced motion.
The shared recipe
Most interface motion follows one recipe:
- Pick a trigger: a state variant (
hover:,focus-visible:,active:), agroup-hover:orpeer-*:variant, or a class toggled by script. - Name the property that changes, with the narrowest
transition-*utility that covers it. - Set
duration-*andease-*. - Prefer
transformandopacitychanges, because they are cheap to render. - Guard continuous or large motion with
motion-safe:ormotion-reduce:.
Pattern 1: A button with hover, focus and press feedback
A button should respond to the pointer, the keyboard, and the press itself. Only the colors and a small scale change here, so transition plus a short duration is enough.
<button class="rounded bg-indigo-600 px-4 py-2 text-white transition duration-200 ease-out hover:bg-indigo-700 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600 active:scale-95">
Save
</button>The active:scale-95 class gives a small press effect. Because transition covers transform, the scale animates without any extra class. If you only wanted the color to animate, transition-colors would be the narrower choice.
Pattern 2: A lifting card
Cards often rise and gain a larger shadow on hover. The transition utility animates the default property set, which includes box-shadow and transform.
<div class="rounded-lg bg-white p-6 shadow transition duration-300 ease-out hover:-translate-y-1 hover:shadow-lg motion-reduce:transition-none motion-reduce:hover:translate-y-0">
Card content
</div>The two motion-reduce: classes remove the movement for users who ask for less motion. The shadow still changes instantly, so the hover feedback is not lost.
Pattern 3: A link with a moving arrow
Here the hovered element is the link, but the element that moves is a child. Mark the link with group, then use group-hover: on the child.
<a href="#" class="group inline-flex items-center gap-2 text-indigo-600">
<span>Read more</span>
<span class="transition-transform duration-200 group-hover:translate-x-1">→</span>
</a>The transition class sits on the arrow, not on the link. The element that changes is the one that needs transition-*.
Pattern 4: A loading spinner
The built-in animate-spin utility loops forever, so it is a good candidate for a reduced-motion guard.
<div class="size-6 rounded-full border-2 border-slate-300 border-t-indigo-600 motion-safe:animate-spin" role="status">
<span class="sr-only">Loading</span>
</div>With motion-safe:, the spinner rotates only when the user has not requested reduced motion. Those users see a static ring, so the sr-only text matters: it tells assistive technology that something is loading.
Pattern 5: A menu that fades in
In Tailwind v4, starting: sets the style an element has on the frame it first appears. Combined with transition-discrete, it lets a popover animate as it opens.
<div popover id="menu" class="opacity-0 transition-opacity transition-discrete duration-200 open:opacity-100 starting:open:opacity-0">
Menu content
</div>This pattern needs a browser that supports @starting-style. Where it is missing, the menu still opens, only without the fade.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| The change snaps with no animation | No `transition-*` class on the element that changes | Add `transition` or a narrower utility to that element |
| The child does not move when the parent is hovered | `hover:` used on the child instead of `group-hover:` | Add `group` to the parent and use `group-hover:` |
| The hover effect flickers at the element's edge | The element moves out from under the pointer | Animate a child, or move a wrapper instead |
| Page layout jumps during an effect | Animating width, height, margin or padding | Animate `transform` or `opacity` instead |
| A spinner still moves for users who disabled motion | `animate-spin` used without a guard | Use `motion-safe:animate-spin` |
Exercise
A card should lift 4 pixels on hover and show a larger shadow. Users who prefer reduced motion should still see the shadow change, but the card should not move. Write the class list.
Show answer
Use "shadow transition duration-300 ease-out hover:-translate-y-1 hover:shadow-lg motion-reduce:hover:translate-y-0". The transition utility animates both transform and box-shadow, and the motion-reduce variant cancels only the translation. Adding "motion-reduce:transition-none" is also acceptable if you want the shadow to change instantly.
Exercise
You add "group-hover:translate-x-1" to an arrow inside a link, but the arrow jumps to its new position without animating. What is missing, and where does it go?
Show answer
The arrow needs a transition utility such as "transition-transform duration-200". It goes on the arrow itself, because that is the element whose property changes.