CSS3 Animation Tutorials

Mastering the art of dynamic web design with CSS.

Introduction to CSS Animations

CSS Animations allow you to create complex animation sequences without relying on JavaScript or Flash. They are defined using the @keyframes rule and applied to elements using the animation property.

Core Concepts

@keyframes Example

This defines an animation named "pulse" that scales an element up and down.

@keyframes pulse {
  0% {
    transform: scale(1);
    box-shadow: 0 0 0 0 rgba(0, 168, 168, 0.7);
  }
  70% {
    transform: scale(1.05);
    box-shadow: 0 0 0 15px rgba(0, 168, 168, 0);
  }
  100% {
    transform: scale(1);
    box-shadow: 0 0 0 0 rgba(0, 168, 168, 0);
  }
}

Applying the Animation

Here's how to apply the "pulse" animation to an element:

.animated-box {
  width: 150px;
  height: 150px;
  background-color: #00a8a8; /* accent-color */
  border-radius: 10px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 1.5rem;
  font-weight: bold;
  color: #f0f0f0; /* light-text */
  animation: pulse 2s infinite ease-in-out;
}
Animate

CSS Transitions vs. Animations

While similar, transitions and animations serve different purposes:

Transition Example (Hover Effect)

This box smoothly changes color and shape when you hover over it.

.transition-example {
  width: 100px;
  height: 100px;
  background-color: #0078d4; /* primary-color */
  margin: 20px auto;
  transition: background-color 0.5s ease-in-out, transform 0.5s ease-in-out;
}

.transition-example:hover {
  background-color: #00a8a8; /* accent-color */
  transform: rotate(45deg) scale(1.2);
}

Key Animation Properties

Understanding the various properties that control animations:

animation-name and animation-fill-mode Example

This box animates in from the right and stays visible.

.animation-name-example {
  width: 100px;
  height: 100px;
  background-color: #00a8a8; /* accent-color */
  margin: 20px auto;
  animation: slideInRight 1.5s forwards;
}

@keyframes slideInRight {
  from { transform: translateX(100%); opacity: 0; }
  to { transform: translateX(0); opacity: 1; }
}

animation-iteration-count and animation-direction Example

This circular element smoothly changes its gradient colors and alternates direction.

.keyframe-example {
  width: 120px;
  height: 120px;
  background: linear-gradient(45deg, #0078d4, #00a8a8);
  border-radius: 50%;
  margin: 20px auto;
  animation: colorChange 4s infinite alternate;
}

@keyframes colorChange {
  0% { background: linear-gradient(45deg, #0078d4, #00a8a8); }
  50% { background: linear-gradient(45deg, #00a8a8, #0078d4); }
  100% { background: linear-gradient(45deg, #0078d4, #00a8a8); }
}

Advanced Techniques & Best Practices

Explore further resources on MDN Web Docs for comprehensive details on CSS animations and transitions.

MDN CSS Animations Guide