Bring your websites to life with smooth, engaging animations and transitions.
CSS animations allow you to animate the transition of CSS properties over time. They can be used to create smooth, engaging user experiences without requiring JavaScript.
Transitions: Animate between two states (e.g., hover effects)
Animations: Complex multi-step animations with keyframes
Use transitions for simple state changes, animations for complex sequences!
CSS transitions provide a way to control animation speed when changing CSS properties. Instead of having property changes take effect immediately, you can cause the changes in a property to take place over a period of time.
/* Basic transition */
.element {
background: blue;
transition: background 0.3s ease;
}
.element:hover {
background: red;
}
/* Multiple properties */
.element {
transition: background 0.3s ease, transform 0.5s ease-out;
}
/* All properties */
.element {
transition: all 0.3s ease;
}
/* Shorthand syntax */
.element {
transition: property duration timing-function delay;
}
Hover over the box to see the transition effect
/* Transition property */
.element {
transition-property: background, transform;
}
/* Transition duration */
.element {
transition-duration: 0.3s;
}
/* Transition timing function */
.element {
transition-timing-function: ease;
/* Values: ease, ease-in, ease-out, ease-in-out, linear, cubic-bezier() */
}
/* Transition delay */
.element {
transition-delay: 0.1s;
}
/* Shorthand */
.element {
transition: background 0.3s ease 0.1s;
}
Property | Values | Description |
---|---|---|
transition-property |
all, none, property-name | Which properties to animate |
transition-duration |
time (s, ms) | How long the transition takes |
transition-timing-function |
ease, ease-in, ease-out, ease-in-out, linear | How the transition progresses over time |
transition-delay |
time (s, ms) | Delay before transition starts |
transition |
property duration timing delay | Shorthand for all transition properties |
CSS transforms allow you to modify the coordinate space of the CSS visual formatting model. Using transform, elements can be translated, rotated, scaled, and skewed.
/* Translate (move) */
.element {
transform: translateX(50px);
transform: translateY(-20px);
transform: translate(50px, -20px);
}
/* Scale */
.element {
transform: scale(1.5);
transform: scaleX(2);
transform: scaleY(0.5);
}
/* Rotate */
.element {
transform: rotate(45deg);
transform: rotateX(45deg);
transform: rotateY(45deg);
}
/* Skew */
.element {
transform: skew(10deg, 5deg);
transform: skewX(10deg);
transform: skewY(5deg);
}
Hover over each box to see transform effects
/* Combining transforms */
.element {
transform: translate(50px, 50px) rotate(45deg) scale(1.2);
}
/* Transform origin */
.element {
transform-origin: center;
transform-origin: top left;
transform-origin: 50% 50%;
}
/* 3D transforms */
.element {
transform: rotateX(45deg) rotateY(45deg);
transform-style: preserve-3d;
perspective: 1000px;
}
Function | Values | Description |
---|---|---|
translate() |
x, y | Move element horizontally and vertically |
translateX() |
distance | Move element horizontally |
translateY() |
distance | Move element vertically |
scale() |
factor | Scale element uniformly |
scaleX() |
factor | Scale element horizontally |
scaleY() |
factor | Scale element vertically |
rotate() |
angle | Rotate element around Z-axis |
rotateX() |
angle | Rotate element around X-axis |
rotateY() |
angle | Rotate element around Y-axis |
skew() |
x-angle, y-angle | Skew element |
Keyframe animations allow you to create complex animations by defining multiple steps (keyframes) in the animation sequence.
/* Define keyframes */
@keyframes slideIn {
from {
transform: translateX(-100%);
opacity: 0;
}
to {
transform: translateX(0);
opacity: 1;
}
}
/* Apply animation */
.element {
animation: slideIn 1s ease-out;
}
/* Multiple keyframes */
@keyframes bounce {
0% {
transform: translateY(0);
}
50% {
transform: translateY(-30px);
}
100% {
transform: translateY(0);
}
}
/* Animation name */
.element {
animation-name: slideIn;
}
/* Animation duration */
.element {
animation-duration: 2s;
}
/* Animation timing function */
.element {
animation-timing-function: ease-in-out;
}
/* Animation delay */
.element {
animation-delay: 0.5s;
}
/* Animation iteration count */
.element {
animation-iteration-count: 3;
animation-iteration-count: infinite;
}
/* Animation direction */
.element {
animation-direction: alternate;
/* Values: normal, reverse, alternate, alternate-reverse */
}
/* Animation fill mode */
.element {
animation-fill-mode: forwards;
/* Values: none, forwards, backwards, both */
}
/* Animation play state */
.element {
animation-play-state: paused;
/* Values: running, paused */
}
/* Shorthand */
.element {
animation: slideIn 2s ease-in-out 0.5s infinite alternate;
}
Property | Values | Description |
---|---|---|
animation-name |
keyframe-name, none | Name of the keyframe animation |
animation-duration |
time (s, ms) | How long the animation takes |
animation-timing-function |
ease, ease-in, ease-out, ease-in-out, linear | How the animation progresses over time |
animation-delay |
time (s, ms) | Delay before animation starts |
animation-iteration-count |
number, infinite | How many times to repeat the animation |
animation-direction |
normal, reverse, alternate, alternate-reverse | Direction of the animation |
animation-fill-mode |
none, forwards, backwards, both | How to apply styles before/after animation |
animation-play-state |
running, paused | Whether animation is running or paused |
Not all CSS properties animate equally well. Some properties trigger layout recalculations, while others can be optimized by the GPU.
transform
- translate, scale, rotateopacity
filter
will-change
width
, height
margin
, padding
top
, left
, right
, bottom
display
/* Good: GPU-accelerated */
.element {
transform: translateX(100px);
opacity: 0.5;
}
/* Bad: Triggers layout */
.element {
left: 100px;
width: 200px;
}
/* Use will-change for optimization */
.element {
will-change: transform, opacity;
}
/* Use transform3d for hardware acceleration */
.element {
transform: translate3d(100px, 0, 0);
}
/* Avoid animating layout properties */
.element {
/* Don't animate these */
/* width, height, margin, padding, top, left, etc. */
}
Modern browsers can use the GPU to accelerate certain CSS properties. Properties like transform
and opacity
are typically GPU-accelerated, making them much more performant than layout properties.
Some users prefer reduced motion due to vestibular disorders or personal preference. Always respect these preferences.
/* Respect reduced motion preference */
@media (prefers-reduced-motion: reduce) {
* {
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
}
}
/* Alternative: Provide reduced motion version */
.element {
animation: slideIn 0.5s ease-out;
}
@media (prefers-reduced-motion: reduce) {
.element {
animation: none;
opacity: 1;
}
}
/* Pause animations on hover for users who prefer it */
@media (prefers-reduced-motion: reduce) {
.element:hover {
animation-play-state: paused;
}
}
prefers-reduced-motion
/* Fade in */
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
/* Slide in from left */
@keyframes slideInLeft {
from {
transform: translateX(-100%);
opacity: 0;
}
to {
transform: translateX(0);
opacity: 1;
}
}
/* Scale in */
@keyframes scaleIn {
from {
transform: scale(0);
opacity: 0;
}
to {
transform: scale(1);
opacity: 1;
}
}
/* Bounce */
@keyframes bounce {
0%, 20%, 53%, 80%, 100% {
transform: translate3d(0, 0, 0);
}
40%, 43% {
transform: translate3d(0, -30px, 0);
}
70% {
transform: translate3d(0, -15px, 0);
}
90% {
transform: translate3d(0, -4px, 0);
}
}