The Wayback Machine - https://web.archive.org/web/20240609040820/https://www.geeksforgeeks.org/css-rain-and-lightning-effect/
Open In App

CSS Rain and lightning effect

Last Updated : 09 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

To generate the rain and lightning effect, we will make use of CSS animations that allow animation of HTML elements.

We will use @keyframes that allow the animation to gradually change from the current style to the new style at certain times then we will use the filter:hue-rotate() to give the lightning effect.

  • Add a background image in the section.
  • To generate the rain effect add a background image of rain to the same section using section:before property of CSS.
  • We will create an animation for a lightning effect named color-change for 10s and infinite time.
  • To create a color-change animation we will use @keyframes and give the effect using property filter:hue-rotate(deg).
  • To create a rain effect again we will use the @keyframes property and change the position of the background image for some interval.

Example: 

html




<!DOCTYPE html>
<html>
    <head>
        <style>
            body {
                margin: 0;
                padding: 0;
            }
            /*adding background to section
          and animation named color-change*/
            section {
                position: relative;
                width: 100%;
                height: 100vh;
                background-image: url(
                background-size: cover;
                background-position: center;
                animation: color-change 10s linear infinite;
                animation-delay: 1s;
            }
            /*adding rain img and making opacity 0*/
            section:before {
                content: "";
                position: absolute;
                top: 0;
                left: 0;
                width: 100%;
                height: 100%;
                background-image: url(
                animation: rain 0.4s linear infinite;
                opacity: 0;
            }
            /* just changing the position of image
          of rain using keyframes*/
            @keyframes rain {
                0% {
                    background-position: 0 0;
                    opacity: 1;
                }
 
                100% {
                    background-position: 8% 80%;
                    opacity: 1;
                }
            }
            /* applying filter at different angle
          on different interval using keyframe*/
            @keyframes color-change {
                0% {
                    filter: hue-rotate(0deg);
                }
                50% {
                    filter: hue-rotate(0deg);
                }
                100% {
                    filter: hue-rotate(360deg);
                }
            }
        </style>
    </head>
    <body>
        <section></section>
    </body>
</html>


Output:



Similar Reads

How to create Glowing Star effect using HTML and CSS?
The glowing star effect is one of the coolest effects that is used for dark themed websites. It is known as star effect because it has small glowing balls animated in different ways which looks like stars. This effect can be used for image slider, loader, and maybe as an showcase UI element. Approach: The approach is to create small balls using uno
3 min read
How to Create Liquid Filling Effect on Text using HTML and CSS ?
The liquid fill text animation can be done using CSS | ::before selector. We will use key frames to set height for each frame of animation. Please make sure you know about both CSS | ::before selector and CSS | @keyframes Rule before try this code.The basic styling of the text can be done differently that totally depends on you how you want your te
3 min read
How to create shock wave or explosion effect using HTML and CSS ?
The shock wave effect is also known as the explosion effect. It is one of the simple CSS effects. For a beginner, it is one of the best examples to learn the concept of pseudo-elements. The pseudo-element that we are using is hover. I recommend you to go through it before jumping into the code to understand it better. For the briefing, hover is use
3 min read
How to Create Loading Blur Text Animation Effect using HTML and CSS ?
The blur text animation is known as the Smoky effect and it is used to give our text a blurry animation. The text blurs linearly in one direction and then reappears. Approach: The approach of this article is quite simple. We are animating the blur effect using the blur() function. Then we are using n-th child property to apply the animation delay.
3 min read
How to create Skewed Background with hover effect using HTML and CSS?
The skewed background or you can say an angel color shade background can be created by using HTML and CSS. This background can be used as a cover pic of your website that will be attractive. In this article, we will create a simple skewed background. We will divide the article into two sections in the first section we will create the structure and
2 min read
How to create paper corner fold effect on hover by using HTML and CSS?
The folding effect is quite attractive on the website, may you have seen on some websites when you hover on the paging layout it gets folded on the corner. The folding corner effect can be achieved by using HTML and CSS only. The below sections will guide you on how to create the animation. In this article, we will divide the article into two secti
3 min read
How to make a glass/blur effect overlay using HTML and CSS ?
To give a background blur effect on an overlay, the CSS's backdrop-filter: blur() property is used with ::before pseudo-element. The “backdrop-filter: blur()” property gives the blur effect on the box or any element where it is desired and “before” is used to add the blurred background without applying any extra markup. HTML Code: In this section,
3 min read
How to Create an Effect to Change Button Color using HTML and CSS ?
The color-changing effect of a button is very common but what we are going to do a slightly advanced version of this effect. The background will change like a clip effect rotating at some particular angle. Approach: The approach is to provide two or three background and then rotating them on different angles. HTML Code: In this section, we will use
3 min read
How to Design Stitched Glowing Effect for Button using HTML and CSS ?
The stitched glowing effect is generally used in the design of classical or retro-themed websites. You can also call it a Denim effect if you use blue color for the effect as it looks similar to the classic original denim. It is one of the best effect to understand the concept of radial-gradient and dashed property of border. Approach: The approach
1 min read
How to Create Animated Navigation Bar with Hover Effect using HTML and CSS ?
The Navigation bar or navbar or menu-bar is the most important component of any web or mobile application. The user can only navigate from one page to another page through this menu. It is usually provided at the top of the website to provide a better UX (user experience). Approach: The approach is to create a navbar first and then animate a line b
3 min read