The @keyframes at-rule and why it is useful

The @keyframes at-rule defines a set of rules for an animation. By using it, you can change an element style over a specific period of time. It is helpful in cases where you want the animation to have multiple stages or be repeated in a loop.

You can use it to create an advanced, choreography-like result that brings your UI to life.

@keyframes at-rule syntax

To use @keyframes in your animations, you first assign a name to them. This name is then used whenever you call @keyframes (for example, @keyframes <name>), and you can reference it later with the animation-name property or through the shorthand animation property.

The @keyframes at-rule includes one or more @keyframe selectors. These keyword selectors help specify the various points during the animation where you want the styles to change.

You can include multiple @keyframe selectors within a single @keyframes at-rule, with each selector indicating a distinct moment in the animation.

A @keyframe selector includes one or more keyframe declarations that are enclosed in curly brackets. Each declaration consists of properties and their respective values.

Below is the representation of the @keyframes at-rule syntax:

Pseudocode example of the @keyframes at-rule syntax.

@keyframes at-rule examples

You can use the keywords from and to as keyframe selectors, or you can opt for percentage values, or even both. The from keyword is equivalent to 0%, which signifies the start of the animation. On the other hand, the to keyword is equal to 100%, which indicates the conclusion of the animation.

Code example of @keyframes from selectors.

You can also use percentages to denote different stages during the animation. For example, 20% represents 20% of the duration, and 50% indicates the halfway point and so on.

Code example of @keyframes using percentages to denote different stages during the animation.

Once you have defined the different stages you can use the name of your @keyframes in your animation.

Code example of an animation using @keyframes.

@keyframes generators

One easy way to experiment with or/and write animations with @keyframes is to use a keyframes generator like the one in cssgen or cssawwwards .

A @keyframes generator allows you to create animations visually, see them in real-time, and easily copy the CSS needed for production.

They are usually completely free, and don’t require any sign-up.

Although my blog doesn’t support comments, feel free to reply via email or X.