CSS fade-in animation
A fade-in makes an element go from invisible to visible by animating its opacity from 0 to 1. In CSS it takes two parts: an @keyframes block that describes the start and the end, and an animation declaration that applies it to a class with a duration, an easing and a fill mode. The generator writes both parts and the media query that switches the animation off for people who ask their system for less motion. This page opens it on the "fade-in" preset.
A real result
@keyframes fade-in {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
.animated {
animation: fade-in 600ms ease-out 0ms 1 normal both;
}
@media (prefers-reduced-motion: reduce) {
.animated {
animation: none;
}
}The shorthand reads, in order: name, duration (600 ms), easing (ease-out), delay (0), iteration count (1), direction (normal) and fill mode (both).
Why the fill mode matters
- both: the element has the first keyframe (opacity 0) during the delay and keeps the last one (opacity 1) at the end. Without it, a delayed element flashes visible before the animation begins.
- forwards: keeps the end state only; backwards: applies the first keyframe during the delay only; none: neither.
Respect reduced motion
The generator adds a prefers-reduced-motion block by default, which removes the animation for users who set that preference (for example because motion makes them dizzy). Without it the CSS is just this:
@keyframes fade-in {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
.animated {
animation: fade-in 600ms ease-out 0ms 1 normal both;
}A fade is gentle, but a slide or a zoom is not: keep the rule for those.
The 13 presets
The first and the last keyframe of every preset the generator offers, as it computes them (24 px of travel for the moving ones):
| Preset | Keyframes | First | Last |
|---|---|---|---|
| fade-in | 2 | opacity: 0 | opacity: 1 |
| fade-out | 2 | opacity: 1 | opacity: 0 |
| slide-up | 2 | opacity: 0; transform: translateY(24px) | opacity: 1; transform: translateY(0) |
| slide-down | 2 | opacity: 0; transform: translateY(-24px) | opacity: 1; transform: translateY(0) |
| slide-left | 2 | opacity: 0; transform: translateX(24px) | opacity: 1; transform: translateX(0) |
| slide-right | 2 | opacity: 0; transform: translateX(-24px) | opacity: 1; transform: translateX(0) |
| zoom-in | 2 | opacity: 0; transform: scale(0.5) | opacity: 1; transform: scale(1) |
| bounce | 3 | transform: translateY(0) | transform: translateY(0) |
| pulse | 3 | transform: scale(1) | transform: scale(1) |
| shake | 11 | transform: translateX(0) | transform: translateX(0) |
| wobble | 7 | transform: translateX(0) rotate(0) | transform: translateX(0) rotate(0) |
| spin | 2 | transform: rotate(0deg) | transform: rotate(360deg) |
| flip | 2 | transform: perspective(400px) rotateY(0deg) | transform: perspective(400px) rotateY(360deg) |
Playing it when the element scrolls into view
const io = new IntersectionObserver((entries) => {
for (const entry of entries) {
if (entry.isIntersecting) { entry.target.classList.add('animated'); io.unobserve(entry.target); }
}
});
document.querySelectorAll('.reveal').forEach(el => io.observe(el));Give .reveal an opacity of 0 and let the .animated class run the animation; with fill mode "both" it ends visible.
Common mistakes
- Hiding the content until a script adds the class: if the script fails the content stays invisible. Add the hiding rule only under a class that the script sets itself, such as .js on the html element.
- A very long fade: over 500 ms the page feels slow. Fades of 200 to 400 ms read as smooth.
- Animating properties that force layout (width, top, margin): opacity and transform are handled by the compositor and stay smooth.
How to use the generator
- The fade-in preset is loaded and plays in the preview (press Play to see it again).
- Set the duration, delay, easing, iterations, direction and fill mode.
- Copy the CSS, with or without the reduced-motion rule.
Frequently asked questions
How do I fade in an element with CSS only?
Define @keyframes fade-in from opacity 0 to 1 and apply animation: fade-in 600ms ease-out both to the element. It runs when the element is first rendered.
Animation or transition for a fade?
A transition fits a change caused by a state (a class toggled, a hover). A keyframe animation fits something that must play on its own when the element appears, without a trigger.
Why does my fade-in flash before it starts?
The element is visible during the delay. Use animation-fill-mode: both (or backwards), or set opacity: 0 as the starting style.