A quick introduction to CSS animations

Per Harald Borgen
Jul 3, 2017 · 5 min read

In this article you’ll learn the basics CSS animations, which allows you to gradually change an element’s style from one state to another.

It’s ideal if you just need a simple animation, and want to stay away from JavaScript.

As you read through the article and get familiar with the concepts, I’d recommend you to check out this interactive Scrimba screencast below, where you’ll be able to play around with the code yourself. Try experimenting with the animation yourself, so that you’ll properly grasp how the technology works.

Now let’s get started!

Thanks to Neil Rowe for the awesome screencast.

Creating the keyframes

The first thing we need to do is to create a @keyframes rule. This is where you specify how your animation will play out from beginning to end.

Let’s create an animation called test:

Inside of test, we’re using percentages to represent the steps in our animation. 0% represents the beginning of the animation and 100% represents the end.

We also need specify what kind of state we want our element to be in at in the animation. In this case, we’re simply flipping background-color. It will be changed to green, purple, orange and blue, and CSS will take care of creating smooth transitions between the colors.

Applying it to an element

This animation can used by any element on the page. We’re going to use it on the div tag with the id of myDiv, which we’ve styled as a small red square.

The div we’re going to animate.

To make myDiv use our animation, we set its animation-name property to test, like this:

As you can see we also give it animation-duration of 4 seconds. This will result in the following happening:

It’s a gif, so it’s looping, which makes it a bit hard to identify the beginning and end.

Customizing it

There are eight different animation properties in CSS, all of which can customize your animation in various ways:

  • animation-name
  • animation-duration
  • animation-timing-function
  • animation-delay
  • animation-iteration-count
  • animation-direction
  • animation-fill-mode
  • animation-play-state

If we for example want our animation to repeat twice and also delay two seconds before kicking off, we’ll do the following:


The final concept we’re going to learn is easing. Easing is used to make an animation look more natural by adjusting the speed throughout the animation. You’ll immediately notice if an animation lacks easing, and rather moves linearly from start to end, as your brain is used to seeing things move with easing in nature.

To specify the easing we want, we’ll use the animation-timing-function property. Here are some of the values it can take:

  • linear
  • ease (default)
  • ease-in
  • ease-out
  • ease-in-out

To make is easier to see how easing works, we’ll animate the width instead of the color of #myDiv. So we’ll change the test keyframe to the following:

And we’ll set #myDiv to:

As we’re not specifying the easing, it’ll use the ease value by default, which makes the animation look natural.

If you look closely, you’ll notices that it eases in and out in both steps of the animation, both from 0% to 50% and from 50% to 100%.

In each step, it starts off slow, then speeds up, before it slows down again towards the end.

Now let’s set theanimation-timing-function to linear to make it apparent how important easing is:

As you can see, this feels a bit less natural. The two steps have been merged into one, as the div is growing with the same speed throughout the entire animation.

Now that we’ve gone through the basic theory, I’d recommend you to play around with the code in the Scrimba screencast that was embedded to the top of this article (alternatively, check it out here).

Try to edit and run the code, so that you get an understanding of how your changes affects the animation.

God luck, and happy coding!

Thanks for reading! I’m Per, co-founder of Scrimba, a tool to create interactive coding screencasts. Be sure to check it out if you’re interested in learning more about web development.

Learning New Stuff

A publication about improving your technical skills.

Per Harald Borgen

Written by

Co-founder of Scrimba and GitSpeak. Trying to make it easier for devs to convey code. https://gitspeak.com/

Learning New Stuff

A publication about improving your technical skills.

More From Medium

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade