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.
- Basics of HTML.
- Basics of CSS.
- A nice background image.
- Create a <div> tag that will be the container of the entire animation, thus we give it the class of container. It will have two more containers inside it, one for holding the text using one <h1> tag and one <p> tag, and the other for holding the particles using 20 <div> tags each with the class of particle.
- Give the animation container a background image, that covers all of it and is placed in the center using the background, background-repeat, background-size, and background-position properties set to url(“path_to_file”), no-repeat, cover, and center respectively.
- To keep the container from being scrollable, we set overflow to hidden.
- Make the image look a little dull by adding a layer of black transparent element over it using the before pseudo selector on the container and giving it a blank content, taking 100% width and height.
- To make this container take full-screen size, we first reset CSS by putting the margin and padding to 0%, and box-sizing to border-box. Make the body take full screen by setting the height and the width property to 100vh and 100vw respectively. Now, set the height and width properties of the animation container to 100%.
- To place elements freely on the screen ignoring the DOM hierarchy, we set the position of the text container and each particle to absolute.
- To place the text contained in the center of the screen, set the top and the left properties to 50%, then use the transform property with the translate() method to move it up and left by 50% of its own dimensions since the top and the left properties push the container based on its edges and not its center point.
- Now, we just increase the font size, give the required padding and margin, and apply some text-shadow.
- To make our 20 <div>s look like particles, we give them the same dimensions for height and width and set border-radius to 50% to make them round. We give them a background color that matches the background image, and some box shadow to make it look like it is glowing.
- We place the particles randomly on the screen, evenly distributed, selecting each one separately using the nth-child selector and setting their top and left properties. To keep it responsive, we define the units in percentages. We also define the animate property to add the particle-motion animation to each particle, each one performing infinitely, linearly, but with different durations.
- Now, define the particle-motion animation using the @keyframes keyword. We use to change values based on stages of completion. We simply make them move zig-zag while moving upward, by giving positive and negative values of x and only the negative value of y in the translate() method of transform property. As the animation progresses, we also decrease the opacity.
Implementation: Below is the implementation of the above approach:
My Personal Notes arrow_drop_up
Please Login to comment...