html

Using CSS for a Fade-in Effect on Page Load

CSS permits us to add various styling properties, such as color, border, font-size, and text-alignment to the HTML elements. These styling properties provide an attractive look to the application. In addition, there are several other CSS properties that help us to add some animation effects to the elements. Using animations can also increase user engagement on web pages.

This article will provide:

Method 1: Fade-in Effect Using CSS “animation” Property

To design a simple HTML page, add the following element on it:

  • Add the “<h2>” element along with the “style” attribute. The “style” attribute contains the styling properties of the element.
  • Apply the “color” property in the style attribute to define the element’s text color.
  • After that, use the “<p>” element to add some text or a simple paragraph.

Below is the HTML code:

<h2 style="color: rgb(84, 8, 191)">
 Linuxhint Tutorial Website
</h2>
<p>fade-in effect on page load</p>

The HTML page is created successfully:

In the CSS section, to apply the fade-in effect on the page, the “animation” CSS property will be used on the “<body>” element of the HTML page.

Style “body” Element

body {
 animation: fadeInPage ease 3s;
 animation-iteration-count: 1;
}

The “<body>” is applied with the following CSS properties:

  • animation” is the shorthand property that sets the animation by specifying multiple values. Here, the animation name, animation-timing-function, and animation-duration is defined.
  • animation-iteration-count” defines how many times the animation should iterate.

Apply “@keyframes” rules on “animation”

@keyframes fadeInPage {
 0% {
  opacity: 0;
  }
 100% {
  opacity: 1;
  }
}

To define the “@keyframes” rules for the animation, mention the animation name after the @keyframes keyword. Modify the animation behavior as follows:

  • At “0%” animation, the “opacity” property is assigned the value 0. It means when the animation starts, the image is transparent.
  • At “100%” animation, the opacity is set to “1”, which refers to a solid color.

Output

Let’s move ahead toward the second method for applying the Fade-in effect on page load.

Method 2: Fade-in Effect Using CSS “transition” Property

Add an “onload” attribute within the “<body>” element. This event is triggered on page load. On load, the opacity of the body element is set to “1”, which relates to a solid color:

<body onload="document.body.style.opacity='1'">

In this example, the CSS “transition” property is utilized to add a fade-in effect:

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

Following is the explanation of the above-stated properties:

  • opacity” property defines the transparency of the elements.
  • Using CSS “transition”, gradually change the values of properties over a specified time.

Output

We have taught you the methods for using CSS for a fade-in effect on page load.

Conclusion

Several CSS properties can be utilized to apply a fade-in effect on HTML elements. More specifically, the “animation”, “opacity”, and “transition” properties can be used to specify animated effects on pages or elements. The animations are adjusted by using the “@keyframe” rules. This article has explained the methods to add a fade-in effect on page load using CSS.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.