CSS Animations
Example 1: Bouncing Ball Animation:
<!DOCTYPE html>
<html>
<head>
  <style>
    @keyframes bounce {
      0%, 100% { transform: translateY(0); }
      50% { transform: translateY(-100px); }
    }
    .ball {
    	border-radius: 30px;
      width: 50px;
      height: 50px;
      background-color: red;
      position: relative;
      animation: bounce 2s ease-in-out infinite;
    }
  </style>
</head>
<body>
  <div class="ball"></div>
</body>
</html>
The ball moves up and down in a continuous loop. The position: relative is used to keep the ball within its container. Example 2: Sequential Animation:
<!DOCTYPE html>
<html>
<head>
  <style>
    @keyframes slide-in {
      0% { transform: translateX(-100%); }
      100% { transform: translateX(0); }
    }
    @keyframes fade-in {
      0% { opacity: 0; }
      100% { opacity: 1; }
    }
    .card {
      width: 300px;
      height: 200px;
      background-color: #007bff;
      color: white;
      animation: slide-in 1s ease-in-out forwards, fade-in 1s ease-in-out forwards 1s;
    }
  </style>
</head>
<body>
  <div class="card">Sequential Animation</div>
</body>
</html>
This example combines two animations (slide-in and fade-in) to create a sequential animation effect on a card. The forwards keyword ensures that the final state of the animations is maintained after they finish. Example 3: Animated Typing Effect:
<!DOCTYPE html>
<html>
<head>
  <style>
    .typing {
      font-family: monospace;
      overflow: hidden;
      white-space: nowrap;
      border-right: 3px solid;
      animation: typing 3s steps(40) infinite, blink 0.75s step-end infinite;
    }
    @keyframes typing {
      from { width: 0; }
      to { width: 100%; }
    }
    @keyframes blink {
      50% { border-color: transparent; }
    }
  </style>
</head>
<body>
  <div class="typing">Hello, I'm a typing effect.</div>
</body>
</html>
The typing animation gradually reveals the text by increasing the width of the border, and the blink animation simulates the blinking cursor. Example 4: Animated Gradient Background:
<!DOCTYPE html>
<html>
<head>
  <style>
    body {
      margin: 0;
      height: 100vh;
      background: linear-gradient(-45deg, #007bff, #00ff00, #ff0000, #ffc107);
      background-size: 400% 400%;
      animation: gradient 10s ease infinite;
    }
    @keyframes gradient {
      0%, 100% { background-position: 0 50%; }
      25% { background-position: 100% 50%; }
      50% { background-position: 50% 100%; }
      75% { background-position: 0 50%; }
    }
  </style>
</head>
<body>
</body>
</html>
This example animates a gradient background by changing the background-position at different keyframes. The result is a visually striking and dynamic background. Example 5: Image Gallery with Hover Effect:
<!DOCTYPE html>
<html>
<head>
  <style>
    .image-container {
      display: flex;
      flex-wrap: wrap;
      gap: 10px;
    }
    .image {
      width: 200px;
      height: 150px;
      overflow: hidden;
      position: relative;
      transition: transform 0.3s ease-in-out;
    }
    .image img {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }
    .image:hover {
      transform: scale(1.1);
    }
    .caption {
      position: absolute;
      bottom: 0;
      left: 0;
      width: 100%;
      background-color: rgba(0, 0, 0, 0.5);
      color: white;
      padding: 5px;
      opacity: 0;
      transition: opacity 0.3s ease-in-out;
    }
    .image:hover .caption {
      opacity: 1;
    }
  </style>
</head>
<body>
  <div class="image-container">
    <div class="image">
      <img src="https://shorturl.at/fjsOQ" alt="Image 1">
      <div class="caption">Beautiful Landscape</div>
    </div>
    <div class="image">
      <img src="https://shorturl.at/muvW9" alt="Image 2">
      <div class="caption">City Skyline</div>
    </div>
    <div class="image">
      <img src="https://shorturl.at/bkwBE" alt="Image 3">
      <div class="caption">Sunny Beach</div>
    </div>
  </div>
</body>
</html>
This example creates an image gallery with a hover effect. When you hover over an image, it scales up and reveals a caption. The transition property is used to smoothly animate the scaling and opacity changes.
by Luka Tatarishvili
1 year ago
CSS
css
0
Pro tip: use ```triple backticks around text``` to write in code fences