Sub topic 2
Scale, Rotate, and Translate on Hover
Assume you know basic HTML and CSS, Tailwind utility classes, and the difference between transitions and keyframe animations. This chapter focuses on transform effects and coordinated animations.
Scale, rotate, and translate on hover
A transform changes an element’s visual position, size, or angle without changing the layout around it. Tailwind provides utilities such as scale-105, rotate-2, and translate-y-1.
Use transition-transform so the transform changes smoothly. Add a duration and timing function to control the motion.
<div class="rounded-lg bg-white p-6 shadow transition-transform duration-300 ease-out hover:-translate-y-1 hover:scale-105 hover:rotate-1">
Hover over this card
</div>The card moves upward, grows to 105% of its size, and rotates slightly. These changes happen together because each hover utility modifies the element’s transform.
Transforms usually animate smoothly because they do not require the browser to recalculate the page layout.
Coordinating elements with group and peer
The group variant lets a child respond when its parent is hovered or focused. Add group to the parent and group-hover:* to 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>A peer variant lets one element respond to the state of a previous sibling. This is useful for custom checkboxes, hints, and validation messages.
<label>
<input type="checkbox" class="peer sr-only">
<span class="rounded border px-3 py-2 peer-checked:bg-indigo-600 peer-checked:text-white">
Select option
</span>
</label>Built-in animations
Tailwind includes animate-spin, animate-ping, animate-pulse, and animate-bounce. These apply predefined keyframe animations.
<div class="size-6 rounded-full border-2 border-slate-300 border-t-indigo-600 motion-safe:animate-spin"></div>motion-safe:animate-spin runs the animation only when the user has not requested reduced motion. Use motion-reduce:animate-none when you need to explicitly disable an animation.
Exercise
Which utility should you use to smoothly animate scale, rotation, and translation together?
Show answer
Use transition-transform, then add the desired hover transform utilities, such as hover:scale-105, hover:rotate-1, and hover:-translate-y-1.