CSS Fade Animations With Code Examples | FadeIn, FadeOut, Directional Fade
Contents
CSS Fade animations are a popular choice to add smooth appearance and disappearance effects to elements. This guide showcases different types of fade animations using pure CSS, including directional fade-in and fade-out. Each section provides a live preview and the corresponding source code for quick use.
The simplest fade animations involve transitioning the opacity of an element. The fadeIn
effect makes an element appear smoothly, while fadeOut
gradually hides it.
Demo FadeIn
.demo-text {
font-size: 18px;
font-weight: bold;
font-family: Arial, Helvetica, sans-serif;
}
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
.fadeIn {
animation: fadeIn 1s ease-in infinite; /* remove infinite*/
}
Demo FadeOut
.demo-text {
font-size: 18px;
font-weight: bold;
font-family: Arial, Helvetica, sans-serif;
}
@keyframes fadeOut {
from { opacity: 1; }
to { opacity: 0; }
}
.fadeOut {
animation: fadeOut 1s ease-in infinite; /* remove infinite*/
}
These animations combine fading with directional movement. They are ideal for animating elements as they enter from different sides of the screen.
Demo FadeInUp
.demo-text {
font-size: 18px;
font-weight: bold;
font-family: Arial, Helvetica, sans-serif;
}
@keyframes fadeInUp {
from {
opacity: 0;
transform: translateY(20px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.fadeInUp {
animation: fadeInUp 1s ease-out infinite;
}
Demo FadeInDown Animation
.demo-text {
font-size: 18px;
font-weight: bold;
font-family: Arial, Helvetica, sans-serif;
}
@keyframes fadeInDown {
from {
opacity: 0;
transform: translateY(-20px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.fadeInDown {
animation: fadeInDown 1s ease-out infinite;
}
Demo FadeInLeft Animation
.demo-text {
font-size: 18px;
font-weight: bold;
font-family: Arial, Helvetica, sans-serif;
}
@keyframes fadeInLeft {
from {
opacity: 0;
transform: translateX(-20px);
}
to {
opacity: 1;
transform: translateX(0);
}
}
.fadeInLeft {
animation: fadeInLeft 1s ease-out infinite;
}
Demo FadeInRight Animation
.demo-text {
font-size: 18px;
font-weight: bold;
font-family: Arial, Helvetica, sans-serif;
}
@keyframes fadeInRight {
from {
opacity: 0;
transform: translateX(20px);
}
to {
opacity: 1;
transform: translateX(0);
}
}
.fadeInRight {
animation: fadeInRight 1s ease-out infinite;
}
Similar to directional fade-ins, these animations fade the element out while moving it in a particular direction.
Demo fadeOutUp Animation
.demo-text {
font-size: 18px;
font-weight: bold;
font-family: Arial, Helvetica, sans-serif;
}
@keyframes fadeOutUp {
from {
opacity: 1;
transform: translateY(0);
}
to {
opacity: 0;
transform: translateY(-20px);
}
}
.fadeOutUp {
animation: fadeOutUp 1s ease-in infinite;
}
Demo FadeOutDown Animation
.demo-text {
font-size: 18px;
font-weight: bold;
font-family: Arial, Helvetica, sans-serif;
}
@keyframes fadeOutDown {
from {
opacity: 1;
transform: translateY(0);
}
to {
opacity: 0;
transform: translateY(20px);
}
}
.fadeOutDown {
animation: fadeOutDown 1s ease-in infinite;
}
Demo FadeOutLeft Animation
.demo-text {
font-size: 18px;
font-weight: bold;
font-family: Arial, Helvetica, sans-serif;
}
@keyframes fadeOutLeft {
from {
opacity: 1;
transform: translateX(0);
}
to {
opacity: 0;
transform: translateX(-20px);
}
}
.fadeOutLeft {
animation: fadeOutLeft 1s ease-in infinite;
}
Demo fadeOutRight Animation
.demo-text {
font-size: 18px;
font-weight: bold;
font-family: Arial, Helvetica, sans-serif;
}
@keyframes fadeOutRight {
from {
opacity: 1;
transform: translateX(0);
}
to {
opacity: 0;
transform: translateX(20px);
}
}
.fadeOutRight {
animation: fadeOutRight 1s ease-in infinite;
}