CSS Animation Example: Floating Particles
"Floating Particles" (or digital fireflies) is a moody background effect best suited for dark themes, SaaS products, or gaming websites to provide a sense of atmospheric depth.
This animation is constructed using absolute positioning and overflow: hidden on the parent container. We start all particles below the visible fold by setting bottom: -10%.
We then apply two simultaneous @keyframes animations to each particle:
float-up: Drives the particle upward utilizingtransform: translateY(). A slighttranslateXadds a natural angled drift mimicking wind.fade-out: Controls the opacity, starting at0, blooming to1as it rises into screen, and dropping back to0before it hits the roof to ensure it doesn't just abruptly vanish off-screen.
Providing unique animation-duration and animation-delay variables to each particle class is required to make the swarm look random and organic. In a production environment, you might write a tiny Javascript loop to generate 50+ of these with randomized classes.
Here is the code to create the Floating Particles animation.
Accessibility Considerations
Endless, unprompted background movement can become deeply distracting, making the foreground text hard to read. prefers-reduced-motion is a CSS media feature used to detect if a user has enabled a setting on their device to minimize the amount of non-essential animation or motion.
Because particles exist solely as decorative motion flair, if a user has indicated they prefer reduced motion, it is best to simply disable the particles entirely and render a clean, still background.
Here's what we used in our example:
@media (prefers-reduced-motion: reduce) {
.particle {
/* Nuke 'em */
display: none;
}
}