CSS Transitions, Transforms and Animations: Hover Effects, Keyframes, Performance and Reduced Motion
Transitions: Smooth Changes
A transition animates a property change, for example on hover.
css
.btn {
background: #0b5cff;
transition: background-color .2s ease, transform .2s ease;
}
.btn:hover,
.btn:focus-visible {
background: #0846c2;
transform: translateY(-2px);
}
| Part | Meaning |
|---|---|
| Property | What changes (background-color, transform) |
| Duration | How long (.2s). 150 to 300 ms feels natural for small UI |
| Timing function | ease, ease-in-out, linear, or a custom curve |
| Delay | Wait before starting |
Transforms
css
.card:hover { transform: scale(1.03); }
.icon { transform: rotate(45deg); }
.slide { transform: translateX(100%); }
.flip { transform: rotateY(180deg); }
Keyframe Animations
css
@keyframes fade-up {
from { opacity: 0; transform: translateY(16px); }
to { opacity: 1; transform: translateY(0); }
}
.hero h1 {
animation: fade-up .6s ease-out both;
}
@keyframes spin { to { transform: rotate(360deg); } }
.spinner {
width: 32px; height: 32px;
border: 4px solid #ddd;
border-top-color: #0b5cff;
border-radius: 50%;
animation: spin 1s linear infinite;
}
Performance Rule
Animate **transform and opacity**. Browsers can handle them smoothly on the graphics processor. Animating width, height, top or left forces layout work and can feel janky.
Respect Reduced Motion
Some people feel dizzy or sick from motion. Operating systems let them ask for less. Honor that setting.
css
@media (prefers-reduced-motion: reduce) {
*, *::before, *::after {
animation-duration: .01ms !important;
animation-iteration-count: 1 !important;
transition-duration: .01ms !important;
scroll-behavior: auto !important;
}
}
Do not overdo it Motion should explain or delight, not distract. Avoid flashing content (more than three flashes per second can trigger seizures) and long auto-playing animations.
Practice
- Add a hover and focus transition to buttons
- Create a fade-up entrance with keyframes
- Add the reduced motion block and test it using DevTools rendering options
Conclusion
Use transitions for state changes and keyframes for sequences. Animate transform and opacity, keep it subtle and honor reduced motion.
Quick Summary: Transitions; transforms; keyframes; performance; reduced motion.

