The Front-End
CSS
Transitions & Animations

Transitions and Animations

CSS transitions and animations allow you to add dynamic and interactive elements to your web pages. Here's an in-depth guide with code snippets and examples:

  1. CSS Transitions
  2. CSS Animations
  3. Example

CSS Transitions

Basic Transition:

Smoothly transition the color property over a specified duration.

.button {
  background-color: #3498db;
  color: white;
  transition: background-color 0.3s ease;
}
 
.button:hover {
  background-color: #2980b9;
}

Transition Multiple Properties:

Apply a transition to multiple properties simultaneously.

.box {
  width: 100px;
  height: 100px;
  background-color: #2ecc71;
  transition: width 0.3s ease, height 0.3s ease, background-color 0.3s ease;
}
 
.box:hover {
  width: 150px;
  height: 150px;
  background-color: #27ae60;
}

Delay and Timing Function:

Introduce a delay and change the timing function for a more customized effect.

.circle {
  width: 100px;
  height: 100px;
  background-color: #e74c3c;
  transition: width 0.5s ease-in-out 0.2s, height 0.5s ease-in-out 0.2s, background-color 0.5s ease-in-out;
}
 
.circle:hover {
  width: 150px;
  height: 150px;
  background-color: #c0392b;
}

CSS Animations

Keyframe Animation:

Define keyframes for a custom animation.

@keyframes moveRight {
  0% {
    transform: translateX(0);
  }
  100% {
    transform: translateX(200px);
  }
}
 
.box {
  width: 50px;
  height: 50px;
  background-color: #f39c12;
  animation: moveRight 2s ease-in-out infinite alternate;
}

Infinite Rotation

Create an infinitely rotating element.

@keyframes rotate {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}
 
.spinner {
  width: 50px;
  height: 50px;
  border: 5px solid #3498db;
  border-top: 5px solid #c0392b;
  border-radius: 50%;
  animation: rotate 2s linear infinite;
}

Bouning Ball

Create a bouncing ball animation.

@keyframes bounce {
  0%, 20%, 50%, 80%, 100% {
    transform: translateY(0);
  }
  40% {
    transform: translateY(-30px);
  }
  60% {
    transform: translateY(-15px);
  }
}
 
.ball {
  width: 50px;
  height: 50px;
  background-color: #9b59b6;
  border-radius: 50%;
  animation: bounce 2s ease-in-out infinite;
}

Fade In

Create a fade in animation.

@keyframes fadeIn {
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}
 
.box {
  width: 100px;
  height: 100px;
  background-color: #3498db;
  animation: fadeIn 2s ease-in-out;
}

Fade Out

Create a fade out animation.

@keyframes fadeOut {
  0% {
    opacity: 1;
  }
  100% {
    opacity: 0;
  }
}
 
.box {
  width: 100px;
  height: 100px;
  background-color: #3498db;
  animation: fadeOut 2s ease-in-out;
}

Example

Animated Button:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Animated Button Example</title>
  <style>
    .button {
      display: inline-block;
      padding: 10px 20px;
      background-color: #3498db;
      color: white;
      text-decoration: none;
      border-radius: 5px;
      transition: background-color 0.3s ease;
    }
 
    .button:hover {
      background-color: #2980b9;
    }
  </style>
</head>
<body>
  <a href="#" class="button">Click Me</a>
</body>
</html>