Skip to main content

CSS3 Animation Cheatsheet

CSS3 provides powerful features to create animations, including transition, transform, and animation.


Transitions

CSS transitions allow smooth changes between property values over a specified duration.

Transition Properties

PropertyDescriptionExample
transitionShorthand for all transition properties.transition: all 0.5s ease;
transition-propertySpecifies the property to transition.transition-property: background-color;
transition-durationDefines the duration of the transition.transition-duration: 0.5s;
transition-timing-functionDefines the speed curve.transition-timing-function: ease-in-out;
transition-delaySpecifies a delay before the transition starts.transition-delay: 0.3s;

Example: Transition

.button {
background-color: blue;
transition: background-color 0.5s ease;
}

.button:hover {
background-color: green;
}

CSS Transforms

CSS transforms allow manipulation of elements in 2D or 3D space using translation, rotation, scaling, skewing, or a combination of these.

Transform Functions

FunctionDescriptionExample
translate(x, y)Moves an element along the x and y axes.transform: translate(50px, 100px);
translateX(x)Moves an element along the x-axis.transform: translateX(100px);
translateY(y)Moves an element along the y-axis.transform: translateY(50px);
rotate(angle)Rotates an element by a specified angle (in degrees).transform: rotate(45deg);
scale(x, y)Scales an element by x and y factors.transform: scale(1.5, 2);
scaleX(x)Scales an element horizontally by x factor.transform: scaleX(1.2);
scaleY(y)Scales an element vertically by y factor.transform: scaleY(0.8);
skew(x-angle, y-angle)Skews an element along the x and y axes.transform: skew(30deg, 10deg);
skewX(x-angle)Skews an element along the x-axis.transform: skewX(20deg);
skewY(y-angle)Skews an element along the y-axis.transform: skewY(15deg);
matrix(a, b, c, d, e, f)Combines all transformations in a single function.transform: matrix(1, 0, 0, 1, 50, 50);

Transform Origin

The transform-origin property sets the point of origin for the transformation. By default, this is the center of the element.

PropertyDescriptionExample
transform-originSpecifies the origin of transformations.transform-origin: 50% 50%;
ValuesKeywords (left, top, center, etc.) or percentage/length values.transform-origin: bottom right;

2D vs 3D Transforms

CSS also supports 3D transforms for more advanced effects.

FunctionDescriptionExample
perspective(n)Specifies perspective for 3D transforms.transform: perspective(500px);
rotateX(angle)Rotates an element around the x-axis.transform: rotateX(45deg);
rotateY(angle)Rotates an element around the y-axis.transform: rotateY(45deg);
rotateZ(angle)Rotates an element around the z-axis (same as rotate).transform: rotateZ(45deg);
scaleZ(z)Scales an element along the z-axis.transform: scaleZ(2);
translateZ(z)Moves an element along the z-axis.transform: translateZ(50px);

Example: Transform

.card {
width: 200px;
height: 200px;
background-color: coral;
transform: scale(1.2) rotate(15deg) translate(20px, 30px);
transition: transform 0.3s ease-in-out;
}

.card:hover {
transform: scale(1) rotate(0deg) translate(0px, 0px);
}

CSS Keyframe Animations

Keyframe animations allow for creating complex animations by defining intermediate steps between the start and end states.

Animation Properties

PropertyDescriptionExample
animationShorthand for all animation properties.animation: fadeIn 2s ease-in-out 1;
animation-nameSpecifies the name of the keyframes to use.animation-name: fadeIn;
animation-durationSpecifies the duration of the animation.animation-duration: 2s;
animation-timing-functionDefines the speed curve of the animation.animation-timing-function: ease-in;
animation-delaySpecifies the delay before the animation starts.animation-delay: 1s;
animation-iteration-countNumber of times the animation repeats.animation-iteration-count: infinite;
animation-directionSpecifies whether the animation reverses on alternate cycles.animation-direction: alternate;
animation-fill-modeDefines how styles are applied before/after the animation.animation-fill-mode: forwards;
animation-play-stateSpecifies whether the animation is running or paused.animation-play-state: paused;

Keyframes Syntax

Keyframes define the intermediate steps for an animation using percentages or keywords like from and to.

@keyframes fadeIn {
0% {
opacity: 0;
transform: translateY(20px);
}
100% {
opacity: 1;
transform: translateY(0);
}
}

Key Animation Properties

animation-timing-function Values

The animation-timing-function property controls the pace of an animation.

ValueDescription
easeStarts slow, speeds up in the middle, and slows down at the end.
linearMaintains a constant speed throughout.
ease-inStarts slowly and accelerates.
ease-outStarts quickly and decelerates.
ease-in-outStarts and ends slowly, with acceleration in the middle.
steps(n, start/end)Divides the animation into n discrete steps.

animation-direction Values

The animation-direction property specifies the direction in which the animation runs.

ValueDescription
normalAnimation plays forwards, from start to finish.
reverseAnimation plays backwards, from end to start.
alternateAnimation alternates between forwards and backwards on each iteration.
alternate-reverseAnimation alternates, starting in reverse on the first iteration.

Other Key Animation Properties

PropertyDescriptionExample
animation-fill-modeSpecifies how the animation applies styles before and after execution.animation-fill-mode: forwards;
animation-delaySpecifies a delay before the animation starts.animation-delay: 1s;
animation-iteration-countDefines how many times the animation should repeat.animation-iteration-count: infinite;
animation-play-stateSpecifies if the animation is running or paused.animation-play-state: paused;

Example: Combining Key Animation Properties

.box {
width: 100px;
height: 100px;
background-color: coral;
animation: bounce 3s ease-in-out infinite alternate;
animation-delay: 1s;
animation-fill-mode: forwards;
}

@keyframes bounce {
0% {
transform: translateY(0);
}
50% {
transform: translateY(-30px);
}
100% {
transform: translateY(0);
}
}