
A CSS transition interpolates a supported property when its computed value changes. It suits a change between states, such as a button changing color when focused.
1. Transition syntax
Set transition-property, transition-duration, transition-timing-function, and transition-delay separately or use the transition shorthand. The first time is the duration and the second is the delay. List the properties you actually want to animate.
.button { transition: background-color 180ms ease, transform 180ms ease-out; }2. Hover, focus, and pressed states
Put the transition on the base rule so both directions animate. Define target values for pointer hover and keyboard focus. Use :active for a pressed interaction, not a persistent toggle state.
.button:is(:hover, :focus-visible) { background-color: #204a85; transform: translateY(-2px); }
.button:active { transform: translateY(0); }3. Timing and practical examples
linear maintains a constant rate; ease, ease-in, ease-out, and ease-in-out change how the transition accelerates. Test short durations with actual interaction. Keep a static alternative for reduced motion.
@media (prefers-reduced-motion: reduce) { .button { transition: none; } }4. When to use keyframes
For several intermediate steps or repetition, use CSS keyframe animations. To move, rotate, or scale the element itself, use transforms.