CSS Animations

CSS animations bring elements on your web pages to life, creating visual motion and captivating user experiences. They allow you to seamlessly transition between different styles and states, adding dynamic effects without relying on JavaScript or external libraries.

  • @keyframes: This rule defines the animation sequence, specifying keyframes (waypoints) that mark changes in styles over time.
  • animation-name: Assigns a name to the animation, linking it to the defined keyframes.
  • animation-duration: Sets the duration of the animation in seconds or milliseconds.
  • animation-timing-function: Controls the pace of the animation (e.g., ease, linear, ease-in-out).
  • animation-delay: Specifies a delay before the animation starts.
  • animation-iteration-count: Determines how many times the animation repeats (e.g., infinite, 2).
  • animation-direction: Controls whether the animation plays forwards, backwards, or alternates.
  • animation-fill-mode: Specifies the style of the element when the animation isn’t running (e.g., forwards, backwards, both).

1. Simple Fade-In Animation:

HTML:

<div class="fade-in">This text fades in.</div>

CSS:

.fade-in {
  opacity: 0; /* Initially invisible */
  animation: fade-in 2s ease-in-out; /* Animation name, duration, timing function */
}

@keyframes fade-in {
  from { opacity: 0; }
  to { opacity: 1; } /* Transition to full opacity */
}

2. Moving Element Animation:

HTML:

<div class="move-right">This box moves to the right.</div>

CSS:

.move-right {
  animation: move-right 3s linear infinite; /* Infinite loop */
}

@keyframes move-right {
  from { transform: translateX(0); }
  to { transform: translateX(200px); } /* Move 200px to the right */
}

3. Rotating Element Animation:

HTML:

<img src="image.jpg" class="rotate">

CSS:

.rotate {
  animation: rotate 2s linear infinite alternate; /* Rotate back and forth */
}

@keyframes rotate {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); } /* Rotate 360 degrees */
}

4. Pulsing Animation:

HTML:

<button class="pulse">Click me</button>

CSS:

.pulse {
  animation: pulse 1s infinite; /* Repeat indefinitely */
}

@keyframes pulse {
  0% { transform: scale(1); }
  50% { transform: scale(1.1); } /* Expand slightly */
  100% { transform: scale(1); } /* Return to original size */
}

Key Points:

  • Use the animation property to apply animations to elements.
  • Define animation keyframes using the @keyframes rule.
  • Control animation duration, timing function, iteration count, and direction.
  • Animate properties like opacity, position, transform, and more.
  • Create visually engaging and interactive experiences without JavaScript.

Best Practices:

  • Consider performance impact, especially for complex animations or those running on less powerful devices.
  • Optimize animation properties for smooth transitions and visual harmony.
  • Ensure accessibility for users with motion sensitivity or visual impairments by providing alternative ways to access content or disabling animations if necessary.