Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

How to create fade-in effect on page load using CSS ?

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Use animation and transition property to create a fade-in effect on page load using CSS. 

Method 1: Using CSS animation property: 
A CSS animation is defined with 2 keyframes. One with the opacity set to 0, the other with the opacity set to 1. When the animation type is set to ease, the animation smoothly fades in the page. This property is applied to the body tag. Whenever the page loads, this animation would play and the page will appear to fade in. The time of the fade in can be set in the animation property. 

Syntax: 

html




body {
    animation: fadeInAnimation ease 3s;
    animation-iteration-count: 1;
    animation-fill-mode: forwards;
}
 
@keyframes fadeInAnimation {
    0% {
        opacity: 0;
    }
    100% {
        opacity: 1;
     }
}


Example: 

html




<!DOCTYPE html>
<html>
  
<head>
    <title>
        How to create fade-in effect
        on page load using CSS
    </title>
  
    <style>
        body {
            animation: fadeInAnimation ease 3s;
            animation-iteration-count: 1;
            animation-fill-mode: forwards;
        }
        @keyframes fadeInAnimation {
            0% {
                opacity: 0;
            }
            100% {
                opacity: 1;
            }
        }
    </style>
</head>
  
<body>
    <h1 style="color: green">
        GeeksForGeeks
    </h1>
     
    <b>
        How to create fade-in effect
        on page load using CSS
    </b>
     
    <p>
        This page will fade in
        after loading
    </p>
</body>
  
</html>


Output:

 fadein-example 

Method 2: Using the transition property and setting the opacity to 1 when the body is loaded: 
In this method, the body can be set to the opacity 0 initially and the transition property is used to animate this property whenever it is changed. When the page is loaded, the opacity is set to 1 using the onload event. Due to the transition property, changing the opacity now will appear to fade in the page. The time of the fade in can be set in the transition property. 

Syntax: 

html




body {
    opacity: 0;
    transition: opacity 5s;
}


Example: 

html




<!DOCTYPE html>
<html>
 
<head>
    <title>
        How to create fade-in effect
        on page load using CSS
    </title>
 
    <style>
        body {
            opacity: 0;
            transition: opacity 3s;
        }
    </style>
</head>
 
<body onload="document.body.style.opacity='1'">
     
    <h1 style="color: green">
        GeeksForGeeks
    </h1>
     
    <b>
        How to create fade-in effect
        on page load using CSS
    </b>
     
    <p>
        This page will fade in
        after loading
    </p>
</body>
 
</html>                   


Output:

 fadein-example2

CSS is the foundation of webpages, is used for webpage development by styling websites and web apps.You can learn CSS from the ground up by following this CSS Tutorial and CSS Examples.


My Personal Notes arrow_drop_up
Last Updated : 02 Feb, 2023
Like Article
Save Article
Similar Reads
Related Tutorials