Sub topic 1
Triggering Changes with State Variants
Prerequisites
This chapter assumes basic HTML and CSS, familiarity with Tailwind utility classes, and a basic understanding of CSS transitions and keyframe animations.
Triggering Changes with State Variants
A state variant applies utilities when an element reaches a state. Common states include hover, focus, focus-visible, and active. Put the variant before the utility with a colon.
<button
class="rounded bg-indigo-600 px-4 py-2 text-white
transition-colors duration-200 ease-out
hover:bg-indigo-700 focus-visible:outline-2
focus-visible:outline-indigo-600 active:bg-indigo-800"
>
Save
</button>The button changes color when the pointer hovers over it, receives keyboard focus, or is pressed. transition-colors makes the color change gradual instead of immediate.
Choosing What to Transition
Choose a transition utility that matches the property you change:
transition-colorsfor text, background, border, and outline colorstransition-opacityfor fadingtransition-transformfor movement, rotation, or scalingtransition-shadowfor changing shadowstransitionfor several common properties
Avoid transition-all unless you need it. It can animate properties you did not intend to change and can make debugging harder.
<a
href="#"
class="rounded-lg bg-white p-4 shadow
transition-transform transition-shadow
hover:-translate-y-1 hover:shadow-lg"
>
Read the article
</a>Setting Duration and Delay
duration-* sets how long the transition runs. For example, duration-200 lasts 200 milliseconds. Use delay-* to wait before starting the transition.
Short durations often work well for buttons and links. Longer durations suit larger movements.
Easing: How a Transition Feels
An easing function controls the transition's speed over time:
ease-linearmoves at a constant speed.ease-instarts slowly.ease-outends slowly and usually feels responsive.ease-in-outstarts and ends slowly.
Exercise
Which classes make a background color change animate for 300 milliseconds and slow down at the end?
Show answer
Use transition-colors duration-300 ease-out.