CSS Fade-In Animations
This example demonstrates how to create fade-in animations using CSS @keyframes, opacity, transforms, filters, and animation delays. Explore different fade-in directions, speeds, and effects that can be used to introduce content smoothly on a webpage.
Basic Fade-In
The element gradually changes from fully transparent to fully visible.
The animation takes 1.5 seconds to complete.
Fast Fade-In
A shorter animation duration creates a faster appearance.
This animation completes in 0.5 seconds.
Slow Fade-In
A longer animation duration creates a gradual fade-in effect.
This animation completes in 3 seconds.
Delayed Fade-In
The animation-delay property postpones the start of the animation.
The animation begins after a one-second delay.
Fade-In Up
The element fades into view while moving upward.
The translateY() function controls vertical movement.
Fade-In Down
The element fades into view while moving downward.
The element begins 30 pixels above its final position.
Fade-In Left
The element fades into view while moving from left to right.
The translateX() function controls horizontal movement.
Fade-In Right
The element fades into view while moving from right to left.
The element begins 40 pixels to the right of its final position.
Fade-In Scale
The element gradually appears while increasing in size.
The scale() function enlarges the element to its normal size.
Fade-In Rotate
The element fades into view while rotating slightly.
The animation combines opacity, rotation, and scaling.
Fade-In Blur
The element changes from blurred and transparent to sharp and visible.
The blur() filter creates the soft appearance.
Repeating Fade
The element continuously fades in and out.
The infinite keyword repeats the animation continuously.
Staggered Fade-In Animation
Staggered animations introduce multiple elements one after another using different animation delays.
Each item uses the same fade-in animation with a progressively longer delay.
This technique is useful for introducing lists, cards, navigation items, and other grouped content.
Interactive Fade-In Animation
Select the button below to replay a fade-in animation. The demonstration uses JavaScript to restart the CSS animation without reloading the page.
Replay Animation
Select Replay Animation to begin the demonstration.
Try It: Reload the page to watch the initial fade-in animations. Compare the different directions, durations, and effects. Select Replay Animation to restart the interactive example. Change the animation durations, delays, and transform values to create your own effects.
How CSS Fade-In Animations Work
CSS fade-in animations gradually change an element's opacity from transparent to visible.
The opacity property controls how transparent an element appears.
An opacity value of 0 makes the element fully transparent, while a value of 1 makes it fully opaque.
The following CSS creates a basic fade-in animation:
@keyframes fade-in {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
.fade-basic {
animation: fade-in 1.5s ease both;
}
The @keyframes rule defines the starting and ending states.
The animation property applies the keyframes to an element.
CSS Animation Properties
Several CSS properties control how fade-in animations behave.
The animation-name property specifies which keyframes to use.
The animation-duration property determines how long the animation takes.
The animation-timing-function property controls the rate of change during the animation.
The animation-delay property specifies how long to wait before the animation starts.
The animation-iteration-count property controls how many times the animation repeats.
The animation-fill-mode property controls which keyframe styles apply before and after the animation.
The animation shorthand property combines these settings into a single declaration.
animation: fade-in 1.5s ease 0.5s both;
This declaration applies a 1.5-second fade-in animation with a 0.5-second delay and retains the appropriate keyframe styles before and after the animation.
Combining Fade-In and Movement Effects
Fade-in animations can be combined with CSS transforms to create movement.
The translateX() function moves an element horizontally.
The translateY() function moves an element vertically.
The following CSS creates a fade-in effect that moves upward:
@keyframes fade-in-up {
from {
opacity: 0;
transform: translateY(30px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
The element begins 30 pixels below its normal position and moves upward as it becomes visible.
Using transforms allows the movement to occur without changing the surrounding document layout.
Using Animation Delays
The animation-delay property specifies how long the browser waits before beginning an animation.
Animation delays can create a sequence in which multiple elements appear at different times.
The following CSS demonstrates staggered animation delays:
.stagger-item:nth-child(1) {
animation-delay: 0.2s;
}
.stagger-item:nth-child(2) {
animation-delay: 0.5s;
}
.stagger-item:nth-child(3) {
animation-delay: 0.8s;
}
Each element uses the same animation but begins at a different time.
Staggered animations can help introduce groups of related content in a visually organized sequence.
Understanding Animation Fill Mode
The animation-fill-mode property determines how keyframe styles apply outside the animation's active duration.
The forwards value retains the styles defined by the final keyframe after the animation finishes.
The backwards value applies the relevant starting keyframe styles during the animation delay.
The both value combines the behaviors of forwards and backwards.
Using both can be useful for delayed fade-in animations because it prevents the element from appearing fully visible before the animation begins.
.fade-delayed {
animation: fade-in 1s ease 1s both;
}
This example waits one second before beginning the fade-in and retains the final visible state afterward.
Creating Repeating Fade Animations
The animation-iteration-count property controls how many times an animation repeats.
Using the infinite keyword makes an animation repeat continuously.
The following keyframes create a repeating fade-in and fade-out effect:
@keyframes fade-in-out {
0%, 100% {
opacity: 0;
}
50% {
opacity: 1;
}
}
.fade-repeat {
animation: fade-in-out 2s ease-in-out infinite;
}
This technique can be used for decorative effects or loading indicators.
Avoid using continuous animation when it could distract visitors from important content.
Fade-In Animation Accessibility
Animations should enhance the presentation of content without making information difficult to access.
Use the prefers-reduced-motion media query to respect visitors who prefer less animation.
The following CSS disables animations when reduced motion is requested:
@media (prefers-reduced-motion: reduce) {
.animation-box,
.stagger-item {
animation: none;
}
}
Content should remain visible and understandable when animations are disabled.
Avoid using animations that flash rapidly or create excessive movement.
Do not use animation as the only method of communicating important information.
Provide controls when users need to pause, stop, or replay animations that could interfere with reading or interaction.
Customizing Fade-In Animations
You can customize fade-in animations by changing their duration, timing function, delay, and keyframe values.
Shorter durations create faster effects, while longer durations create more gradual appearances.
Modify the translateX() and translateY() values to control the distance and direction of movement.
Use scale() to create zoom effects or rotate() to introduce rotation.
Combine opacity with transforms or filters to create additional animation styles.
For a consistent website design, use similar animation durations and timing functions throughout your interface.