CSS Animations & Transitions

Keyframes, transitions, transforms, and animation properties

Transitions

transition

Shorthand for all transition properties

transition: all 0.3s ease;

transition-property

Specify which properties to animate

transition-property: transform, opacity;

transition-duration

How long the transition takes

transition-duration: 0.3s;

transition-timing-function

How the transition progresses over time

transition-timing-function: ease-in-out;

transition-delay

Delay before transition starts

transition-delay: 0.1s;

Transforms

transform

Apply 2D or 3D transformations

transform: translateX(10px) rotate(45deg);

translate

Move element in 2D space

transform: translate(10px, 20px);

translateX/Y/Z

Move element along specific axis

transform: translateX(10px);

scale

Scale element size

transform: scale(1.2);

rotate

Rotate element

transform: rotate(45deg);

skew

Skew element

transform: skew(10deg, 5deg);

Keyframes

@keyframes

Define custom animation sequence

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

animation

Shorthand for all animation properties

animation: slideIn 0.5s ease-out;

animation-name

Name of the keyframe animation

animation-name: slideIn;

animation-duration

How long the animation takes

animation-duration: 0.5s;

animation-timing-function

How the animation progresses

animation-timing-function: ease-out;

animation-delay

Delay before animation starts

animation-delay: 0.2s;

animation-iteration-count

How many times to repeat

animation-iteration-count: infinite;

animation-direction

Direction of animation

animation-direction: alternate;

animation-fill-mode

Styles before/after animation

animation-fill-mode: forwards;

Timing Functions

ease

Slow start, fast middle, slow end

transition-timing-function: ease;

linear

Constant speed

transition-timing-function: linear;

ease-in

Slow start, fast end

transition-timing-function: ease-in;

ease-out

Fast start, slow end

transition-timing-function: ease-out;

ease-in-out

Slow start and end, fast middle

transition-timing-function: ease-in-out;

cubic-bezier()

Custom timing function

transition-timing-function: cubic-bezier(0.25, 0.46, 0.45, 0.94);

Common Patterns

Hover Effects

Common hover animations

.button {
  background: #007bff;
  color: white;
  padding: 10px 20px;
  border: none;
  border-radius: 5px;
  transition: all 0.3s ease;
}

.button:hover {
  background: #0056b3;
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(0,0,0,0.2);
}

Fade In Animation

Simple fade in effect

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.fade-in {
  animation: fadeIn 0.6s ease-out;
}

Loading Spinner

Rotating loading animation

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

.spinner {
  width: 40px;
  height: 40px;
  border: 4px solid #f3f3f3;
  border-top: 4px solid #3498db;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

Slide In from Left

Slide animation for content

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

.slide-in {
  animation: slideInLeft 0.5s ease-out;
}

Pulse Effect

Pulsing animation for attention

@keyframes pulse {
  0% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.05);
  }
  100% {
    transform: scale(1);
  }
}

.pulse {
  animation: pulse 2s infinite;
}

Tips & Best Practices

Use transform instead of changing layout properties for better performance

Keep animations under 300ms for smooth user experience

Use will-change property for elements that will animate

Respect prefers-reduced-motion for accessibility

Use animation-fill-mode: forwards to maintain final state