Open In App

Create Effect of Particle Animation using CSS

At least once, you must have seen a website with particles appearing and disappearing in its background. This cool particle animation may seem complicated but it is actually quite easy to make. In this article, we will set an image as the background of our body, and on it, we will have particles moving in zig-zag motion appearing and disappearing regularly. The main concepts of CSS that will be helpful are position properties, keyframe, and transform properties.

Prerequisites:



Approach:

Implementation: Below is the implementation of the above approach:






<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible"
          content="IE=edge">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0">
    <title>GeeksforGeeks Pure CSS Particle Animation</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="container">
        <div class="text-content">
            <h1>Particle Animation</h1>
            <p>
                Welcome to GeeksforGeeks, in this article you
                  will learn to create particle animation purely
                  from CSS. We will not be using any CSS library.
            </p>
        </div>
        <div class="particle-container">
            <div class="particle"></div>
            <div class="particle"></div>
            <div class="particle"></div>
            <div class="particle"></div>
            <div class="particle"></div>
            <div class="particle"></div>
            <div class="particle"></div>
            <div class="particle"></div>
            <div class="particle"></div>
            <div class="particle"></div>
            <div class="particle"></div>
            <div class="particle"></div>
            <div class="particle"></div>
            <div class="particle"></div>
            <div class="particle"></div>
            <div class="particle"></div>
            <div class="particle"></div>
            <div class="particle"></div>
            <div class="particle"></div>
            <div class="particle"></div>
        </div>
    </div>
</body>
</html>




*{
    padding: 0%;
    margin: 0%;
    box-sizing: border-box;
}
 
body{
    width: 100vw;
    height: 100vh;
    overflow-x: hidden;
}
 
.container{
    position: relative;
    width: 100%;
    height: 100%;
    overflow: hidden;
    background-color: rgb(41, 37, 37);
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;
}
 
.text-content{
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 90%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    color: rgb(255, 193, 59);
    text-shadow: 2px 1px 5px crimson;
    font-family: Verdana, Geneva, Tahoma, sans-serif;
    letter-spacing: 2px;
}
 
.text-content h1{
    font-size: 4rem;
}
 
.text-content p{
    font-size: 1.2rem;
    font-style: italic;
    font-weight: lighter;
    width: 80%;
}
 
.particle-container .particle{
    position: absolute;
    height: 10px;
    width: 10px;
    border-radius: 50%;
    background: radial-gradient(rgb(255, 245, 153) 40%, rgb(247, 211, 51), orange, rgb(255, 81, 0));
    box-shadow: 3px 5px 10px rgba(255, 255, 255, 0.514);
 
}
 
.particle-container .particle:nth-child(1){
    top: 12%;
    left: 42%;
    animation: particle-motion 1s linear infinite;
}
 
.particle-container .particle:nth-child(2){
    top: 70%;
    left: 50%;
    animation: particle-motion 7s linear infinite;
}
.particle-container .particle:nth-child(3){
    top: 17%;
    left: 6%;
    animation: particle-motion 9s linear infinite;
}
.particle-container .particle:nth-child(4){
    top: 20%;
    left: 60%;
    animation: particle-motion 10s linear infinite;
}
.particle-container .particle:nth-child(5){
    top: 67%;
    left: 10%;
    animation: particle-motion 6s linear infinite;
}
.particle-container .particle:nth-child(6){
    top: 80%;
    left: 70%;
    animation: particle-motion 11s linear infinite;
}
.particle-container .particle:nth-child(7){
    top: 60%;
    left: 80%;
    animation: particle-motion 6s linear infinite;
}
.particle-container .particle:nth-child(8){
    top: 32%;
    left: 25%;
    animation: particle-motion 6s linear infinite;
}
.particle-container .particle:nth-child(9){
    top: 90%;
    left: 25%;
    animation: particle-motion 9s linear infinite;
}
.particle-container .particle:nth-child(10){
    top: 10%;
    left: 80%;
    animation: particle-motion 5s linear infinite;
}
 
.particle-container .particle:nth-child(11){
    top: 24%;
    left: 82%;
    animation: particle-motion 10s linear infinite;
}
 
.particle-container .particle:nth-child(12){
    top: 97%;
    left: 5%;
    animation: particle-motion 7s linear infinite;
}
.particle-container .particle:nth-child(13){
    top: 97%;
    left: 60%;
    animation: particle-motion 9s linear infinite;
}
.particle-container .particle:nth-child(14){
    top: 30%;
    left: 40%;
    animation: particle-motion 5s linear infinite;
}
.particle-container .particle:nth-child(15){
    top: 47%;
    left: 55%;
    animation: particle-motion 7s linear infinite;
}
.particle-container .particle:nth-child(16){
    top: 70%;
    left: 80%;
    animation: particle-motion 8s linear infinite;
}
.particle-container .particle:nth-child(17){
    top: 40%;
    left: 40%;
    animation: particle-motion 10s linear infinite;
}
.particle-container .particle:nth-child(18){
    top: 23%;
    left: 15%;
    animation: particle-motion 4s linear infinite;
}
.particle-container .particle:nth-child(19){
    top: 90%;
    left: 90%;
    animation: particle-motion 3s linear infinite;
}
 
.particle-container .particle:nth-child(20){
    top: 85%;
    left: 95%;
    animation: particle-motion 7s linear infinite;
}
 
@keyframes particle-motion {
    0%{
        transform: translate(0);
        opacity: 1;
    }
 
    20%{
        transform: translate(5px -20px);
        opacity: 0.8;
    }
 
    40%{
        transform: translate(-10px, -30px);
        opacity: 0.7;
    }
 
    60%{
        transform: translate(15px, -40px);
        opacity: 0.5;
    }
 
    80%{
        transform: translate(-20px, -60px);
        opacity: 0.2;
    }
 
    100%{
        transform: translate(40px, -100px);
        opacity: 0;
    }
}

Output:

 


Article Tags :