D DevBrainBox

CSS Animations

CSS

What are CSS Animations?

CSS Animations allow you to move, transform, or change elements over time using keyframes — no JavaScript needed.

  • They are more advanced than transition because:
  • They can run automatically
  • They can have multiple steps
  • They can loop infinitely

Basic Structure

1. Define the animation

Using @keyframes:

@keyframes slideIn {
  from {
    transform: translateX(-100%);
  }
  to {
    transform: translateX(0);
  }
}

2. Apply the animation to an element

.box {
  animation-name: slideIn;
  animation-duration: 1s;
  animation-timing-function: ease;
  animation-delay: 0s;
  animation-iteration-count: 1;
  animation-fill-mode: forwards;
}

Shorthand Syntax

animation: slideIn 1s ease 0s 1 forwards;

PropertyWhat it does
animation-nameName of the keyframes
animation-durationHow long it takes (2s, 500ms)
animation-delayWait time before it starts
animation-timing-functionSpeed pattern (ease, linear, ease-in-out)
animation-iteration-countNumber of repeats (1, infinite)
animation-directionnormal, reverse, alternate
animation-fill-modeKeeps final state with forwards or backwards

Example: Fade In Animation

@keyframes fadeIn {
  from { opacity: 0; }
  to   { opacity: 1; }
}

.fade-box {
  animation: fadeIn 2s ease-in-out forwards;
}

Example: Bouncing Box

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

.bounce {
  animation: bounce 1s ease infinite;
}
StepDescription
@keyframesDefine how the animation behaves
animationApply it to the element
FlexibleSupports delays, repeats, directions

On this page