CSS Animations

CSS Animations
udemy placeholder image

CSS animations allow you to animate the transition between CSS properties. The awesome thing about CSS animations is that they don't require any JavaScript and it allows the browser to control the animation, meaning their performance can be improved over time as opposed to if the animation was written in JavaScript.

An animation.
An animation.

Animation Keyframes

The first step in creating a new animation is to first define your animation keyframes. A keyframe is just the state of the animation at a certain point in time. After you define a few keyframes, you can then let the browser animate the states between them.

A full keyframes definition is composed of three distinct parts:

  1. Name: The name of the animation.
  2. Keyframe: The moment in time as a percentage of the entire animation.
  3. CSS Properties: The CSS properties and values to apply.

Let's say you wanted an animation that turns a page's background color from white to pink then to lightsalmon, and we called this animation fire:

	
    @keyframes fire {
        0% {
            background-color: white;
        }
        50% {
            background-color: pink;
        }
        100% {
            background-color: lightsalmon;
        }
    }
	

At 0% since the animation has not yet started, we have the initial state to a background color of white. Halfway through the animation, at 50%, we set the background color to pink. Finally, at the end of the animation, at 100%, we set the background color to lightsalmon.

In between those states, however, the browser is doing the heavy lifting and calculating the color values in between to ensure the animation is silky smooth.

Animation Name and Duration

Now that we've defined our keyframes and given our animation a name, now we must simply apply it to an element. We do this by telling an element what animation to use via animation-name. In our example, it would be fire.

After specifying which animation the element should use, you will also need to define a duration for the animation. After all, we told our browser that we want a background color of lightred at 50%, but 50% of what? We define the duration of the animation using animation-duration.

Let's say we want to apply fire to body and have the entire animation last for 3 seconds:

	
    .animation {
        animation-name: fire;
        animation-duration: 3s;
    }
	

Try this out yourself with this fully-working file:

	
    <!DOCTYPE html>
    <html>
        <head>
            <title>Animation</title>
			<style>
				.animation {
					animation-name: fire;
					animation-duration: 3s;
				}
				@keyframes fire {
					0% {
						background-color: white;
					}
					50% {
						background-color: pink;
					}
					100% {
						background-color: lightsalmon;
					}
				}
			</style>
        </head>
        <body class="animation"></body>
    </html>
	
Our fire animation ๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅ.
Our fire animation ๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅ.

While an animation name and duration is required, there are multiple additional optional animation properties that you can use to customize your animation:

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

Let's go over each one.

1and1 placeholder image

Animation Delay

The animation-delay property defines how long to wait after the element has loaded to begin the animation. Naturally, it takes a time value, seconds (s) or milliseconds (ms).

To start an animation 2 seconds later:

	
    .animation {
        animation-name: fire;
        animation-duration: 3s;
        animation-delay: 2s;
    }
	

Or to start an animation 1,500 milliseconds into the animation, use a negative value:

	
    .animation {
        animation-name: fire;
        animation-duration: 3s;
        animation-delay: -1500ms;
    }
	

Fully-working example:

	
    <!DOCTYPE html>
    <html>
        <head>
            <title>Animation Delay</title>
            <style>
                body {
                    display: flex;
                    justify-content: space-between;
                }
                .box {
                    width: 5rem;
                    height: 5rem;
                    animation-name: pulsate;
                    animation-duration: 3s;
                }
                .delayed {
                    animation-delay: 2s;
                    background-color: blue;
                }
                .sped-up {
                    animation-delay: -1500ms;
                    background-color: orange;
                }
                @keyframes pulsate {
                    0% {
                        width: 5rem;
                        height: 5rem;
                    }
                    100% {
                        width: 10rem;
                        height: 10rem;
                    }
                }
            </style>
        </head>
        <body>
            <div class="box delayed"></div>
            <div class="box sped-up"></div>
        </body>
    </html>
	

Animation Iteration Count

The animation-iteration-count property defines how many times the animation should run before stopping. The default is 1 but you can have it run any number of times you want. To run it forever, simply set the value to infinite.

An animation set to run 3 times:

	
    .three-times {
        animation-name: fire;
        animation-duration: 3s;
        animation-iteration-count: 3;
    }
	

An animation set to run forever:

	
    .forever {
        animation-name: fire;
        animation-duration: 3s;
        animation-iteration-count: infinite;
    }
	

Fully-working example:

	
    <!DOCTYPE html>
    <html>
        <head>
            <title>Animation Iteration Count</title>
            <style>
                body {
                    display: flex;
                    justify-content: space-between;
                }
                .box {
                    width: 5rem;
                    height: 5rem;
                    animation-name: pulsate;
                    animation-duration: 3s;
                }
                .three-times {
                    animation-iteration-count: 3;
                    background-color: blue;
                }
                .forever {
                    animation-iteration-count: infinite;
                    background-color: orange;
                }
                @keyframes pulsate {
                    0% {
                        width: 5rem;
                        height: 5rem;
                    }
                    100% {
                        width: 10rem;
                        height: 10rem;
                    }
                }
            </style>
        </head>
        <body>
            <div class="box three-times"></div>
            <div class="box forever"></div>
        </body>
    </html>
	

Animation Direction

The animation-direction property tells the browser in what direction to play the animation. Here are the valid values:

  • normal: Plays the animation forwards (0% to 100%)
  • reverse: Plays the animation backwards (100% to 0%)
  • alternate: Plays the animation forwards (0% to 100%) then backwards (100% to 0%)
  • alternate-reverse: Plays the animation backwards (100% to 0%) then forwards (0% to 100%)

The default is normal so for most cases, you don't even need to specify a animation-direction.

Fully-working example:

	
    <!DOCTYPE html>
    <html>
        <head>
            <title>Animation Direction</title>
            <style>
                body {
                    display: flex;
                    justify-content: space-between;
                }
                .box {
                    width: 5rem;
                    height: 5rem;
                    animation-name: pulsate;
                    animation-duration: 3s;
                    animation-iteration-count: infinite;
                }
                .normal {
                    animation-direction: normal;
                    background-color: blue;
                }
                .reverse {
                    animation-direction: reverse;
                    background-color: orange;
                }
                .alternate {
                    animation-direction: alternate;
                    background-color: red;
                }
                .alternate-reverse {
                    animation-direction: alternate-reverse;
                    background-color: purple;
                }
                @keyframes pulsate {
                    0% {
                        width: 5rem;
                        height: 5rem;
                    }
                    100% {
                        width: 10rem;
                        height: 10rem;
                    }
                }
            </style>
        </head>
        <body>
            <div class="box normal"></div>
            <div class="box reverse"></div>
            <div class="box alternate"></div>
            <div class="box alternate-reverse"></div>
        </body>
    </html>
	
bluehost placeholder image

Animation Timing Function

The animation-timing-function property is hugely important in determining how the animation is actually rendered. So you know how animations are percentage-based (0% to 100%)? Well, this property defines the acceleration curve, or how fast the browser goes from 0% to 100% over time.

Here are the valid values:

  • ease: Starts off slow, speeds up sharply, slows down at the end. (default)
  • linear: Goes from 0% to 100% linearly.
  • ease-in: Starts off slow and continues to speed up until the end.
  • ease-out: Starts off fast but slows down at the end.
  • ease-in-out: Starts off slow, speeds up smoothly, slows down at the end.

Fully-working example:

	
    <!DOCTYPE html>
    <html>
        <head>
            <title>Animation Timing Function</title>
            <style>
                body {
                    display: flex;
                    justify-content: space-between;
                }
                .box {
                    width: 5rem;
                    height: 5rem;
                    animation-name: pulsate;
                    animation-duration: 3s;
                    animation-iteration-count: infinite;
                }
                .ease {
                    animation-timing-function: ease;
                    background-color: blue;
                }
                .linear {
                    animation-timing-function: linear;
                    background-color: orange;
                }
                .ease-in {
                    animation-timing-function: ease-in;
                    background-color: red;
                }
                .ease-out {
                    animation-timing-function: ease-out;
                    background-color: purple;
                }
                .ease-in-out {
                    animation-timing-function: ease-in-out;
                    background-color: pink;
                }
                @keyframes pulsate {
                    0% {
                        width: 5rem;
                        height: 5rem;
                    }
                    100% {
                        width: 10rem;
                        height: 10rem;
                    }
                }
            </style>
        </head>
        <body>
            <div class="box ease"></div>
            <div class="box linear"></div>
            <div class="box ease-in"></div>
            <div class="box ease-out"></div>
            <div class="box ease-in-out"></div>
        </body>
    </html>
	

Animation Fill Mode

The animation-fill-mode property determines if an element with an animation will have styles applied before the animation begins, or after it ends. A little confusing at first, but it can prove really useful once you understand it. By default, styles from animations are not applied before the animation begins (like during an animation delay) nor after an animation ends. Using animation-fill-mode, you can change this behavior with the following values:

  • normal: No animation styles are applied before nor after (default behavior).
  • backwards: Animation styles of the initial keyframe (0%) are applied before the animation is played.
  • forwards: Animation styles of the last keyframe (100%) are applied after the animation ends.
  • both: Combining both backwards and forwards, the initial keyframe (0%) is applied before the animation begins, and the last keyframe (100%) is applied after it ends.
	
    <!DOCTYPE html>
    <html>
        <head>
            <title>Animation Fill Mode</title>
            <style>
                body {
                    display: flex;
                    justify-content: space-between;
                }
                .box {
                    width: 5rem;
                    height: 5rem;
                    animation-name: pulsate;
                    animation-duration: 3s;
                    animation-delay: 1s;
                }
                .box:hover {
                    animation-play-state: paused;
                }
                @keyframes pulsate {
                    0% {
                        width: 5rem;
                        height: 5rem;
                    }
                    100% {
                        width: 10rem;
                        height: 10rem;
                    }
                }
            </style>
        </head>
        <body>
            <div class="box"></div>
        </body>
    </html>
	

Animation Play State

The animation-play-state sets whether or not the animation is playing. This is useful for when you want to pause the animation for any reason, like a mouse hover. Here are the values:

  • playing: This allows the animation to continue playing (default behavior).
  • paused: This pauses the animation.

Fully-working example:

	
    <!DOCTYPE html>
    <html>
        <head>
            <title>Animation Play State</title>
            <style>
                .box {
                    width: 5rem;
                    height: 5rem;
                    animation-name: pulsate;
                    animation-duration: 3s;
					animation-iteration-count: infinite;
					background-color: red;
                }
                .box:hover {
                    animation-play-state: paused;
                }
                @keyframes pulsate {
                    0% {
                        width: 5rem;
                        height: 5rem;
                    }
                    100% {
                        width: 10rem;
                        height: 10rem;
                    }
                }
            </style>
        </head>
        <body>
            <div class="box"></div>
        </body>
    </html>
	

As you've hopefully seen, animations are a powerful way to add sweet animations to your elements to help make your page feel a little more alive. They are extremely customizable allowing you plenty of freedom to create interesting new experiences. The best way to learn is to just jump right in and start animating!