Sub topic 5
Scale, rotate and translate on hover
This chapter assumes you know basic HTML and CSS, can read Tailwind utility classes, and have used the hover: variant and transition-* utilities from earlier chapters. Here you will add movement to a transition: growing, turning and shifting an element when the user points at it.
How transforms work in Tailwind
A CSS transform changes how an element is drawn without changing where it sits in the page layout. Neighbouring elements do not move. Tailwind exposes three transforms as separate utilities:
| Effect | Utilities | Example |
|---|---|---|
| Scale (resize) | `scale-*`, `scale-x-*`, `scale-y-*` | `scale-105` is 105% size |
| Rotate (turn) | `rotate-*` | `rotate-3` turns 3 degrees clockwise |
| Translate (shift) | `translate-x-*`, `translate-y-*` | `-translate-y-1` moves up 0.25rem |
Negative values use a leading dash, as in -rotate-6 or -translate-y-1. Values are on a spacing scale for translate, a percentage scale for scale, and degrees for rotate.
Transforms do nothing visible until something changes them. Pair them with a variant such as hover: and a transition utility.
A transform alone jumps
Without a transition, the browser applies the new transform instantly. This card snaps to its larger size:
<div class="rounded-lg bg-white p-6 shadow hover:scale-105">
Card content
</div>Add transition and a duration and the change is animated.
Combining transform, shadow and transition
The transition utility animates a default set of properties. That set includes transform, translate, scale, rotate and box-shadow, so one class covers all the effects below.
<div class="rounded-lg bg-white p-6 shadow transition duration-300 ease-out hover:-translate-y-1 hover:scale-105 hover:shadow-lg">
Card content
</div>On hover, this card moves up 0.25rem, grows by 5% and gains a larger shadow. All three changes run over 300 milliseconds with the same easing.
If you only want to animate transforms, use transition-transform. It limits the transition to transform-related properties, so a color change on the same element would still jump.
Moving a child with group-hover
Often you want a small part of a component to move when the whole component is hovered. Put group on the parent and 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 arrow slides 0.25rem to the right when the pointer is anywhere over the link, not only over the arrow. Notice that the transition classes sit on the arrow, because the arrow is the element that changes.
Rotation and transform origin
Rotation and scaling happen around a point called the transform origin. By default it is the center of the element. Change it with origin-* utilities.
<button class="origin-top-left rounded bg-slate-800 px-4 py-2 text-white transition-transform duration-200 hover:-rotate-3">
Tilt
</button>With origin-top-left, the button pivots from its top-left corner instead of its middle. Use origin-bottom for something that should grow upward from its base, such as a menu.
Common mistakes
- Putting the transition on the hover variant only. Writing
hover:transitionanimates the entering change but not the return. Puttransitionon the base element so both directions animate. - Scaling too far. Values above
scale-110on text or images often look blurry or push into neighbouring content. Small changes such asscale-105feel more refined. - Expecting layout to adjust. A scaled element can overlap its neighbours because its layout box does not change. Add spacing, or use
z-10on hover to keep it above siblings. - Using margin or
topinstead of translate. Margin and position changes force the browser to recalculate layout. Translate does not, and it animates more smoothly.
Exercises
Exercise
A card has the classes rounded p-4 hover:scale-105 hover:shadow-lg. It snaps to the new size on hover. Which classes would you add so it animates smoothly in both directions, and where do they go?
Show answer
Add transition duration-300 ease-out (or similar values) to the base element, not behind hover:. The transition utility includes transform and box-shadow in its default set. Putting it on the base state makes the return animation run too.
Exercise
You want an icon inside a button to rotate 90 degrees when the user hovers anywhere over the button. Which classes go on the button and which on the icon?
Show answer
Button: group. Icon: transition-transform duration-200 group-hover:rotate-90. The icon rotates around its center by default.