CSS Transitions and Animations

Introduction to CSS Transitions and Animations

CSS Transitions and Animations allow us to make web pages more interactive and visually appealing by adding motion effects to HTML elements. They enable smooth changes between different states, making websites feel more dynamic and engaging.


CSS Transitions

Definition:

CSS Transitions allow changes to CSS property values to occur over a specified duration, creating smooth, gradual transitions between the starting and ending values.

Key Properties of Transitions:

  1. transition-property:

    • Defines the CSS property (like color, background-color, width, etc.) you want to animate.
    • Example: transition-property: background-color;
  2. transition-duration:

    • Specifies the duration of the transition effect (how long the change will take).
    • Example: transition-duration: 0.5s;
  3. transition-timing-function:

    • Controls the speed of the transition effect. It defines the acceleration curve of the transition.
    • Common values:
      • ease: starts slow, speeds up, then slows down.
      • linear: constant speed.
      • ease-in: starts slow and speeds up.
      • ease-out: starts fast and slows down.
      • ease-in-out: starts slow, speeds up in the middle, and slows down at the end.
    • Example: transition-timing-function: ease;
  4. transition-delay:

    • Specifies a delay before the transition starts.
    • Example: transition-delay: 0.2s;

Shorthand Syntax for Transitions:

All transition properties can be combined in a shorthand notation:

Example of a Transition:

button {
  background-color: #4CAF50;
  transition: background-color 0.3s ease;
}

button:hover {
  background-color: #45a049;
}

In this example:


CSS Animations

Definition:

CSS Animations allow you to create complex sequences of changes to CSS properties over time. Unlike transitions, which occur between two states, animations allow for multiple keyframes, making it possible to create looping and continuous animations.

Key Components of Animations:

  1. @keyframes Rule:

    • Defines the stages of the animation using “keyframes” that represent specific points in the animation sequence.

    • Example:

      @keyframes slide {
        0% { transform: translateX(0); }
        50% { transform: translateX(50px); }
        100% { transform: translateX(0); }
      }
      
  2. animation-name:

    • Specifies the name of the @keyframes animation to apply.
    • Example: animation-name: slide;
  3. animation-duration:

    • Defines how long the animation should take to complete one cycle.
    • Example: animation-duration: 2s;
  4. animation-timing-function:

    • Sets the speed curve for each cycle of the animation (similar to the transition timing function).
    • Example: animation-timing-function: ease-in-out;
  5. animation-delay:

    • Delays the start of the animation.
    • Example: animation-delay: 1s;
  6. animation-iteration-count:

    • Specifies the number of times the animation should repeat.
    • Possible values:
      • 1, 2, 3 (etc.): defines a specific number of repeats.
      • infinite: repeats the animation indefinitely.
    • Example: animation-iteration-count: infinite;
  7. animation-direction:

    • Controls the direction of the animation on each cycle.
    • Values:
      • normal: each cycle plays forward.
      • reverse: each cycle plays backward.
      • alternate: each cycle alternates between forward and backward.
      • alternate-reverse: each cycle alternates in reverse.
    • Example: animation-direction: alternate;
  8. animation-fill-mode:

    • Specifies what styles should apply before and after the animation.
    • Values:
      • none: default; styles do not apply outside animation.
      • forwards: applies the final keyframe’s styles after animation.
      • backwards: applies the initial keyframe’s styles before animation starts.
      • both: applies both forwards and backwards effects.
    • Example: animation-fill-mode: forwards;

Shorthand Syntax for Animations:

Example of an Animation:

@keyframes bounce {
  0% { transform: translateY(0); }
  50% { transform: translateY(-20px); }
  100% { transform: translateY(0); }
}

button {
  animation: bounce 1s ease infinite;
}

In this example:


Key Differences Between Transitions and Animations

Property CSS Transitions CSS Animations
Trigger Starts on an event (e.g., hover, click) Runs automatically once defined
Control Limited to two states Multiple keyframes allow for complex sequences
Repetition Runs once per event Can loop infinitely or a set number of times
Definition Defined by transition properties Defined using @keyframes and animation properties

Practical Use Cases


Tips for Using Transitions and Animations