WDD 231: Web Frontend Development I

CSS Animations and Transitions

Overview

The animation property in CSS enables developers to implement animations without the need for JavaScript. These animations are performance-efficient and are well-suited for creating subtle transitions between defined CSS style states over a specified duration.

The transition property in CSS allows for the smooth interpolation of style changes over a specified duration. By defining which properties should transition and the timing of the transition, developers can enhance user experience through subtle, visually appealing effects triggered by changes in an element's state.

Prepare

The following explains the CSS baseline properties to implement visual effects on web pages.

Keyframes

The @keyframes rule in CSS defines the steps of an animation, specifying how an element's styles should change over time.

You use @keyframes to create an animation by naming it and defining what should happen at different points (called keyframes) during the animation's timeline, such as at 0%, 50%, or 100%.

The following is an example of an animation for a fade in effect:

@keyframes fadeIn {
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
} 

And, the following example produces a fade color effect that fades from red to yellow:

@keyframes fadeColor {
  from {
    background-color: red;
  }
  to {
    background-color: yellow;
  }
}

Once defined, the new keyframes animation can be referenced in an animation rule as shown below.

Animations

The animation can be defined directly through the animation shorcut property, or by specifying any of its sub-properties directly:

🍂 🍁 🍃 🍁 🍂 🍃 🍂 🍁 🍃 🍁