Transitions, Transforms and Animation
💻 CSSAdd restrained motion with transitions, transforms and keyframe animations.
Motion can guide attention and make an interface feel alive. Used carelessly, it distracts and makes people dizzy.
- Animate hover states with transitions.
- Use transform for scale and movement.
- Write a keyframe animation.
Transitions
A transition animates a change between two states. Specify the property, duration and easing.
.button {
background: #5e256d;
color: white;
padding: 12px 20px;
border-radius: 8px;
transition: background 200ms ease, transform 200ms ease;
}
.button:hover {
background: #7b2d8f;
transform: translateY(-2px);
}Transforms
Use transform instead of changing width or top for movement. It is smoother because the browser can animate it without recalculating layout.
translateY(-2px)lifts an element.scale(1.05)enlarges it slightly.rotate(2deg)tilts it.transform-originsets the pivot point.
Keyframes
A keyframe animation runs a sequence automatically. Keep loops subtle and provide a reduced-motion option.
@keyframes pulse {
0%, 100% { opacity: 1; }
50% { opacity: 0.6; }
}
.badge {
animation: pulse 2s ease-in-out infinite;
}
@media (prefers-reduced-motion: reduce) {
.badge { animation: none; }
}Practice
- Add a hover lift to a button.
- Animate a loading dot with keyframes.
- Disable the animation when reduced motion is requested.
Hands-on exercise
Style a .button so that on hover it:
1. Moves up by 3px.
2. Grows to 1.03 scale.
3. Changes background colour.
Use a 180ms transition with ease easing.
Recap
Transitions animate state changes, transforms move without breaking layout, and keyframes run sequences. Respect reduced-motion settings.
Example
.button {
background: #5e256d;
color: white;
padding: 12px 20px;
border-radius: 8px;
transition: background 200ms ease, transform 200ms ease;
}
.button:hover {
background: #7b2d8f;
transform: translateY(-2px);
}
Exercises
1. Moves up by 3px.
2. Grows to 1.03 scale.
3. Changes background colour.
Use a 180ms transition with ease easing.
Expected Output
The button lifts smoothly on hover.
Lesson Nav
CSS Course
0/9 lessons completed
- 1 How to Add CSS to HTML Tutorial
- 2 Colors, Fonts, and Text Tutorial
- 3 The Box Model Tutorial
- 4 Flexbox Layout Tutorial
- 5 Build a Landing Page Project
- 6 CSS Grid Layout Tutorial
- 7 Responsive Design with Media Queries Tutorial
- 8 Transitions, Transforms and Animation Tutorial
- 9 Project: A Responsive Portfolio Project