Advanced CSS3 Animation Effects

Share this article

We’ve now covered the majority of CSS3 animation properties but there’s another of which few developers are aware…

Pausing and Restarting Animations

The animation-play-state property can pause or resume an animation. It accepts either:
  • running — the default; an animation plays as normal
  • paused — the animation is paused
Typically, you could apply animation-play-state: paused in reaction to user input, e.g. when hovering over or focusing on the animated element:
#myelement:hover, #myelement:focus
{
	animation-play-state: paused;
}
The animation continues from where it stopped once the mouse cursor/focus is moved elsewhere. Consider my Star Wars Scrolling Text in CSS3
. It was just a bit of fun but some users would find it difficult to read the text at the speed it scrolls. Shouting at the projectionist would get you thrown out of the movie theater, but at least we can pause the scrolling in the browser when the user hovers over the text:
#titlecontent:hover
{
	-webkit-animation-play-state: paused;
	animation-play-state: paused;
}
View the Star Wars titles featuring pausing and restarting… No JavaScript is necessary and no Ewoks were harmed in the creation of this code (although I did bullseye a womp rat and slap a Gungan).

Multiple Animations

Like transitions, you can apply multiple animations to the same element. These are played concurrently — they’re not queued like you would expect in jQuery or other JavaScript libraries. Different animation values are separated by a comma and remember that values will loop if you omit them. For example:
#myelement
{
	animation-name: colorchange, sizechange;
	animation-duration: 5s, 2s;
	animation-timing-function: linear, ease;
	animation-delay: 0s, 5s;
	animation-iteration-count: infinite;
	animation-direction: alternate;
}
This:
  1. runs an alternating ‘colorchange’ animation using the linear timing function for five seconds and starts immediately, and
  2. runs an alternating ‘sizechange’ animation using the ease timing function for two seconds but starts after five seconds.
View a multiple animations demonstration… You’ll notice that the color change occurs for five seconds before the resizing starts (I’ve also added animation-play-state: paused on hover). Yes, it’s ugly — I’m sure you can devise better examples!

Frequently Asked Questions on Advanced CSS3 Animations

What are the key differences between CSS3 transitions and animations?

CSS3 transitions and animations are both powerful tools for creating dynamic web content. Transitions are simpler and are used to create smooth, gradual changes from one CSS property value to another over a specified duration. They are triggered by a change in the state of an element, such as a hover effect. On the other hand, animations are more complex and versatile. They allow for multiple keyframes, which can define different styles at different points in the animation timeline. This allows for more intricate and controlled animations compared to transitions.

How can I use CSS3 animations to improve user experience on my website?

CSS3 animations can significantly enhance user experience on your website by making it more interactive and engaging. They can be used to draw attention to important elements, provide visual feedback, guide users through a process, or simply add a touch of fun and personality to your site. However, it’s important to use animations judiciously and ensure they don’t distract from the main content or slow down the site performance.

Can I control the speed and timing of CSS3 animations?

Yes, CSS3 animations provide a high level of control over the speed and timing of animations. You can specify the duration of the animation, the number of times it repeats, the direction it plays in, and even the timing function, which controls the acceleration and deceleration of the animation. This allows you to create animations that feel natural and intuitive to the user.

Are CSS3 animations supported by all browsers?

Most modern browsers support CSS3 animations, including Chrome, Firefox, Safari, and Edge. However, older versions of some browsers, particularly Internet Explorer, may not fully support CSS3 animations. It’s always a good idea to test your animations in multiple browsers to ensure they work as expected.

How can I use keyframes in CSS3 animations?

Keyframes are used in CSS3 animations to define the styles at various points along the animation timeline. You can specify as many keyframes as you like, and the browser will interpolate the styles between them. This allows you to create complex, multi-step animations with precise control over the appearance of the element at each stage.

Can I animate multiple properties at once with CSS3?

Yes, CSS3 animations allow you to animate multiple properties at once. You simply list all the properties you want to animate in the animation property, separated by commas. This allows you to create more complex and interesting animations.

What are some common uses of CSS3 animations?

CSS3 animations are commonly used to create hover effects, loading spinners, slide-in elements, background animations, and much more. They can also be used to create more complex interactive elements, such as games or interactive infographics.

How can I make my CSS3 animations responsive?

To make your CSS3 animations responsive, you can use media queries to adjust the animation properties based on the screen size. You can also use relative units, such as percentages or viewport units, to ensure the animations scale with the size of the element or the viewport.

Can I pause or stop a CSS3 animation?

Yes, you can control the play state of a CSS3 animation using the animation-play-state property. Setting this property to “paused” will pause the animation, while setting it to “running” will resume it.

How can I learn more about advanced CSS3 animations?

There are many resources available online to learn more about advanced CSS3 animations. Websites like SitePoint, Smashing Magazine, and Shay Howe offer in-depth tutorials and examples. You can also find video tutorials on YouTube, or take a course on a platform like LinkedIn Learning or Creative Bloq.

Craig BucklerCraig Buckler
View Author

Craig is a freelance UK web consultant who built his first page for IE2.0 in 1995. Since that time he's been advocating standards, accessibility, and best-practice HTML5 techniques. He's created enterprise specifications, websites and online applications for companies and organisations including the UK Parliament, the European Parliament, the Department of Energy & Climate Change, Microsoft, and more. He's written more than 1,000 articles for SitePoint and you can find him @craigbuckler.

AdvancedCSSanimationCSS3HTML5 Dev CenterHTML5 Tutorials & Articles
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week