Adding animation with CSS
CSS is fantastic at styling a web page, and it can bring a page to life with movement too. A little animation can draw attention to a button, welcome a visitor with a heading that slides into view, or give helpful feedback, like a form field that shakes when something's wrong.
Writing animations from scratch takes practice, but you don't have to. Animate.css, a free library created by Daniel Eden, gives you dozens of ready-made animations that you add to an element with a couple of class names.
We've also set up a page where you can preview every animation at the click of a button: check out our CSS animations page.
Try a few animations
Here are six popular effects, similar to ones you'll find in the library. Press a button to play each animation:
Animate.css groups its animations into categories, so it's easy to find the right kind of movement:
| Category | Good for | Examples |
|---|---|---|
| Attention seekers | Drawing the eye to something already on the page | pulse, shakeX, heartBeat |
| Entrances | Making elements appear: fading, bouncing, sliding, zooming or rotating in | fadeInUp, bounceIn, zoomIn |
| Exits | Making elements disappear in style | fadeOut, bounceOut, slideOutRight |
| Flippers and specials | Playful, eye-catching effects | flip, hinge, rollIn |
Using Animate.css in your project
Step 1: add the stylesheet
The quickest way is to link to the library from a CDN (content delivery network), a service that hosts popular files so you don't have to. Add this line inside your page's <head>:
<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css">
You can also download the file and host it yourself, or install it with npm (npm install animate.css) if you use a build tool.
Step 2: add the classes
Give an element two classes: animate__animated, which switches animation on, and the name of the animation you want, with the animate__ prefix:
<h2 class="animate__animated animate__bounceIn">Welcome!</h2>
That's it. When the page loads, the heading bounces into view.
Correction: the original version of this post used class names like animated pulse. Animate.css version 4 added the animate__ prefix to every class, so they don't clash with other styles on your site. Code using the old names won't work with the current library. A few animation names changed too: shake is now shakeX (side to side), with a new shakeY (up and down).
Playing an animation on demand
Animations run as soon as the classes are added. To play one later, for example when a button is clicked, add the classes with a little JavaScript:
<script>
const box = document.querySelector('.box');
box.classList.add('animate__animated', 'animate__shakeX');
// Remove the classes when it finishes, so it can play again next time
box.addEventListener('animationend', function () {
box.classList.remove('animate__animated', 'animate__shakeX');
}, { once: true });
</script>
Changing speed, delay and repeats
Animate.css includes utility classes, extra classes that adjust an animation without writing any CSS:
| Class | What it does |
|---|---|
animate__delay-2s | Waits 2 seconds before starting (there are also -3s, -4s and -5s versions) |
animate__slow, animate__slower | Plays over 2 or 3 seconds |
animate__fast, animate__faster | Plays in 800 or 500 milliseconds |
animate__repeat-2, animate__repeat-3 | Plays two or three times |
animate__infinite | Repeats forever (use with care) |
<p class="animate__animated animate__fadeInUp animate__delay-2s animate__slow">
I fade in slowly, two seconds after the page loads.
</p>
For exact timing, set the --animate-duration custom property (a CSS variable). Set it on :root to change every animation, or on one element to change just that one:
:root {
--animate-duration: 600ms;
}
.hero-title {
--animate-duration: 1.5s;
}
Using animation with care
A little movement goes a long way. Too much, and a page becomes distracting or even uncomfortable to use. Keep these points in mind:
- Animate with purpose. Use movement to guide attention or give feedback, not just because you can.
- Avoid endless loops. The original version of this post had four headings animating forever. Constant movement makes it hard to read anything else on the page, and accessibility guidelines ask that anything moving for more than five seconds can be paused or stopped.
- Respect reduced motion. Some people get dizzy or nauseous from on-screen movement, and turn on a "reduce motion" setting on their device. Animate.css honours this setting automatically through the
prefers-reduced-motionmedia query. Its documentation recommends never turning that off. - Never hide content until it animates. If an entrance animation fails to run, the content should still be visible and usable.
Good to know: the demos above were written specially for this post rather than loaded from the library, so they don't add a stylesheet to the rest of the page. They also respect the reduce motion setting: if it's turned on, each one jumps straight to its final state.
Animate.css is free to use, but check its licence before adding it to a commercial project. Animate.css uses the Hippocratic License, which comes with some conditions on how software may be used. You'll find it, along with the full documentation, at animate.style.
Wrapping up
Animate.css makes adding movement as easy as adding a class. Link the stylesheet, add animate__animated plus an animation name, and tweak it with the utility classes. Explore every effect on our CSS animations page, and keep an eye out for our more in-depth post on creating your own CSS animations.