Skip to main content

Advanced CSS3 Animation Effects

By Craig Buckler

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:

	-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:

	animation-name: colorchange, sizechange;
	animation-duration: 5s, 2s;
	animation-timing-function: linear, ease;
	animation-delay: 0s, 5s;
	animation-iteration-count: infinite;
	animation-direction: alternate;


  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!

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.

Integromat Tower Ad