Free CSS course. Sign Up for tracking progress →

CSS: Animation

Animation is an extensive module in CSS. Unfortunately, it's impossible to describe all the features in one lesson. In this lesson, we will show you the basic principle of animation in CSS based on @keyframes. For example, let's make a text animation, where the color of the text changes from blue to red.

The @keyframes rule and the name of the animation will be used to set the animation:

@keyframes color-change {
  // Inside will be our animation
}

The name of the animation can be anything. You can choose it yourself. After, the animation property will know which animation you want to play by its name.

Inside @keyframes you need to specify different time intervals in which the changes will take place. For example, at the start (0% animation completion), there should be blue text, in the middle of the animation (50%), it should become red, and at the end (100%) it'll go back to blue.

@keyframes color-change {
  0% {
    color: blue;
  }

  50% {
    color: red;
  }

  100% {
    color: blue;
  }
}

Now we need to apply the animation we created to the text on the page. To accomplish this, use the animation rule with three basic values:

  • Animation name
  • Length of the animation. It's given in seconds (1s, 2s, 3s etc.)
  • You also need to decide if the animation will be infinite (if it is infinite, then you need to specify the infinite parameter)
.animation-text {
  animation: color-change 3s infinite;
}

All that's left is to create a paragraph with the necessary class, and the animation will begin to work.

Text with color-change animation. The color of the text changes from blue to red and back again

Instructions

Create an animation that changes the color of the text from black to white. Apply the animation to the black-white class. Write the styles in the tag <style>

The exercise doesn't pass checking. What to do? 😶

If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:

  • Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
In my environment the code works, but not here 🤨

Tests are designed so that they test the solution in different ways and against different data. Often the solution works with one kind of input data but doesn't work with others. Check the «Tests» tab to figure this out, you can find hints at the error output.

My code is different from the teacher's one 🤔

It's fine. 🙆 One task in programming can be solved in many different ways. If your code passed all tests, it complies with the task conditions.

In some rare cases, the solution may be adjusted to the tests, but this can be seen immediately.

I've read the lessons but nothing is clear 🙄

It's hard to make educational materials that will suit everyone. We do our best but there is always something to improve. If you see a material that is not clear to you, describe the problem in “Discussions”. It will be great if you'll write unclear points in the question form. Usually, we need a few days for corrections.

By the way, you can participate in courses improvement. There is a link below to the lessons course code which you can edit right in your browser.


If you got stuck and don't know what to do, you can ask a question in our community