🎞️ CSS Animations — Full Tutorial

Last Updated: November 2025


CSS Animations allow HTML elements to move, fade, grow, shrink, rotate, or change colors — without using JavaScript 🎨.
They make your webpage more interactive and lively.

🗣 Hinglish Tip: Animation ka matlab — ek property ka value time ke saath badalna. Jaise ek box left se right move kar raha ho.

Syntax

selector {
  animation-name: name;
  animation-duration: time;
  animation-timing-function: ease;
  animation-delay: time;
  animation-iteration-count: number | infinite;
  animation-direction: normal | reverse | alternate;
  animation-fill-mode: none | forwards | backwards | both;
  animation-play-state: running | paused;
}

Shorthand Property

animation: name duration timing-function delay iteration-count direction
  fill-mode play-state;

Keyframes Define Animation

@keyframes name {
  from {
    property: value;
  }
  to {
    property: value;
  }
}

🧩 Animation Properties Explained

PropertyDescriptionExample
animation-nameName of the keyframes to useanimation-name: fadeIn;
animation-durationHow long one cycle takesanimation-duration: 3s;
animation-timing-functionSpeed curve of animationanimation-timing-function: ease-in;
animation-delayWait time before animation startsanimation-delay: 1s;
animation-iteration-countHow many times it repeatsanimation-iteration-count: infinite;
animation-directionPlayback directionanimation-direction: alternate;
animation-fill-modeState of element after animationanimation-fill-mode: forwards;
animation-play-statePause or play animationanimation-play-state: paused;

🎞️ Timing Functions

FunctionDescription
linearConstant speed
easeDefault → slow-fast-slow
ease-inStarts slow
ease-outEnds slow
ease-in-outSlow start & end
cubic-bezier()Custom speed curve

🗣 Hinglish Tip:Cubic-bezier ek graph hota hai jisme tum speed ka pattern custom set karte ho.


Example: Simple Animation

@keyframes moveRight {
  from {
    left: 0;
  }
  to {
    left: 200px;
  }
}

.box {
  position: relative;
  width: 100px;
  height: 100px;
  background: coral;
  animation-name: moveRight;
  animation-duration: 2s;
}

Example: Fade In & Out

@keyframes fade {
  0% {
    opacity: 0;
  }
  50% {
    opacity: 1;
  }
  100% {
    opacity: 0;
  }
}

.fade-box {
  width: 100px;
  height: 100px;
  background: pink;
  animation: fade 4s ease-in-out infinite;
}

Example: Rotate Loader

@keyframes spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

.loader {
  width: 80px;
  height: 80px;
  border: 8px solid #ccc;
  border-top: 8px solid #333;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

Example: Bounce Effect

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

.ball {
  width: 50px;
  height: 50px;
  background: red;
  border-radius: 50%;
  animation: bounce 1s ease-in-out infinite;
}

Example: Multiple Animations

@keyframes move {
  from {
    left: 0;
  }
  to {
    left: 200px;
  }
}

@keyframes colorChange {
  0% {
    background: red;
  }
  100% {
    background: yellow;
  }
}

.multi {
  position: relative;
  width: 100px;
  height: 100px;
  animation: move 2s linear infinite, colorChange 2s alternate infinite;
}

Control Animation with Hover

.box {
  animation: move 3s linear infinite;
  animation-play-state: paused;
}

.box:hover {
  animation-play-state: running;
}