CSS Animations & Transitions - vdsology
- Chapter 9 — CSS Animations & Transitions
Chapter 9 — CSS Animations & Transitions
Part 1 — Transitions
A transition says: “When this property changes, move smoothly over time instead of jumping instantly.”
Without vs With Transition
/* Without — instant jump */
.button { background: blue; }
.button:hover { background: red; }
/* With — smooth change */
.button {
background: blue;
transition: background 300ms ease;
}
.button:hover { background: red; }
Transition Syntax
transition: property duration timing-function delay;
transition: background-color 300ms ease 0ms;
duration — how long: 200ms, 0.5s
timing-function:
| Value | Behavior |
|---|---|
ease |
Starts fast, slows at end — feels natural (default) |
ease-in |
Starts slow, speeds up |
ease-out |
Starts fast, slows down |
ease-in-out |
Slow start, fast middle, slow end |
linear |
Constant speed |
Multiple properties:
.card {
transition:
background-color 300ms ease,
transform 200ms ease-out,
box-shadow 300ms ease;
}
Common Transitions
Hover lift:
.card {
transform: translateY(0);
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
transition: transform 200ms ease-out, box-shadow 200ms ease-out;
}
.card:hover {
transform: translateY(-6px);
box-shadow: 0 12px 24px rgba(0,0,0,0.15);
}
Button press:
.button {
transform: scale(1);
transition: transform 100ms ease, opacity 100ms ease;
}
.button:hover { opacity: 0.9; }
.button:active { transform: scale(0.96); }
transform — Moving and Reshaping Elements
transform is GPU-accelerated — silky smooth and doesn’t affect layout.
transform: translateX(100px); /* move right */
transform: translateY(-20px); /* move up */
transform: translate(50px, -20px); /* move right AND up */
transform: scale(1.1); /* 10% bigger */
transform: scale(0.9); /* 10% smaller */
transform: rotate(45deg); /* rotate clockwise */
transform: rotate(-90deg); /* counter-clockwise */
transform: skewX(10deg); /* slant */
/* Combine multiple */
transform: translateY(-6px) scale(1.02) rotate(2deg);
💡 Always prefer animating
transformandopacityoverwidth,height,margin, ortop/left.
Part 2 — Keyframe Animations
Transitions are A→B. Animations are A→B→C→D→… They can loop and run automatically.
Defining with @keyframes
@keyframes fadeIn {
from { opacity: 0; transform: translateY(20px); }
to { opacity: 1; transform: translateY(0); }
}
/* Or with percentage steps */
@keyframes pulse {
0% { transform: scale(1); }
50% { transform: scale(1.05); }
100% { transform: scale(1); }
}
Applying an Animation
.card {
animation: fadeIn 500ms ease forwards;
}
All animation properties:
.element {
animation-name: fadeIn;
animation-duration: 500ms;
animation-timing-function: ease;
animation-delay: 200ms;
animation-iteration-count: infinite; /* or a number */
animation-direction: alternate; /* normal, reverse, alternate */
animation-fill-mode: forwards; /* none, forwards, backwards, both */
animation-play-state: running; /* running or paused */
}
/* Shorthand */
animation: fadeIn 500ms ease 200ms infinite alternate forwards;
Common Animations
Spinning loader:
@keyframes spin {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
.loader {
width: 40px;
height: 40px;
border: 4px solid #e5e7eb;
border-top-color: #2563eb;
border-radius: 50%;
animation: spin 700ms linear infinite;
}
Bounce:
@keyframes bounce {
0%, 100% { transform: translateY(0); }
50% { transform: translateY(-20px); }
}
.arrow { animation: bounce 1s infinite; }
Slide in:
@keyframes slideInLeft {
from { transform: translateX(-100%); opacity: 0; }
to { transform: translateX(0); opacity: 1; }
}
Staggered Animations
Cards appear one after another:
@keyframes fadeUp {
from { opacity: 0; transform: translateY(20px); }
to { opacity: 1; transform: translateY(0); }
}
.card {
opacity: 0;
animation: fadeUp 400ms ease-out forwards;
}
.card:nth-child(1) { animation-delay: 0ms; }
.card:nth-child(2) { animation-delay: 100ms; }
.card:nth-child(3) { animation-delay: 200ms; }
.card:nth-child(4) { animation-delay: 300ms; }
Performance — What to Animate
/* ✅ FAST — GPU accelerated */
transform: translate(), scale(), rotate()
opacity
/* ⚠️ SLOW — causes layout reflow */
width, height, margin, padding, top, left
Always Respect User Preferences
@media (prefers-reduced-motion: reduce) {
* {
animation-duration: 0.01ms !important;
transition-duration: 0.01ms !important;
}
}
Tags: #CSS #css #vdsology #webdevelopment #vdsologyCSS #CSS3 #CSSreference #CSSreference2026
Write a comment