CSS animations and transitions for smooth interactions

Alex Chang Feb 2026
1 tab
/* Basic transitions */
.button {
  background-color: blue;
  color: white;
  padding: 1rem 2rem;
  transition: background-color 0.3s ease;
}

.button:hover {
  background-color: darkblue;
}

/* Multiple properties */
.card {
  transform: scale(1);
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
  transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.card:hover {
  transform: scale(1.05);
  box-shadow: 0 8px 16px rgba(0,0,0,0.2);
}

/* Transition all properties */
.link {
  transition: all 0.2s ease;
}

/* Timing functions */
.ease { transition-timing-function: ease; }
.ease-in { transition-timing-function: ease-in; }
.ease-out { transition-timing-function: ease-out; }
.ease-in-out { transition-timing-function: ease-in-out; }
.linear { transition-timing-function: linear; }

/* Custom cubic-bezier */
.bounce {
  transition: transform 0.5s cubic-bezier(0.68, -0.55, 0.265, 1.55);
}

.bounce:hover {
  transform: translateY(-10px);
}

/* Staggered transitions */
.menu-item {
  opacity: 0;
  transform: translateX(-20px);
  transition: opacity 0.3s ease, transform 0.3s ease;
}

.menu-item:nth-child(1) { transition-delay: 0.1s; }
.menu-item:nth-child(2) { transition-delay: 0.2s; }
.menu-item:nth-child(3) { transition-delay: 0.3s; }
.menu-item:nth-child(4) { transition-delay: 0.4s; }

.menu.open .menu-item {
  opacity: 1;
  transform: translateX(0);
}

/* Simple keyframe animations */
@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

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

/* Slide in from left */
@keyframes slideInLeft {
  from {
    transform: translateX(-100%);
    opacity: 0;
  }
  to {
    transform: translateX(0);
    opacity: 1;
  }
}

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

/* Bounce animation */
@keyframes bounce {
  0%, 20%, 50%, 80%, 100% {
    transform: translateY(0);
  }
  40% {
    transform: translateY(-30px);
  }
  60% {
    transform: translateY(-15px);
  }
}

.bounce-animation {
  animation: bounce 2s infinite;
}

/* Pulse animation */
@keyframes pulse {
  0% {
    transform: scale(1);
    opacity: 1;
  }
  50% {
    transform: scale(1.1);
    opacity: 0.8;
  }
  100% {
    transform: scale(1);
    opacity: 1;
  }
}

.pulse {
  animation: pulse 2s ease-in-out infinite;
}

/* Rotate spinner */
@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;
}

/* Shake animation */
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  10%, 30%, 50%, 70%, 90% { transform: translateX(-10px); }
  20%, 40%, 60%, 80% { transform: translateX(10px); }
}

.shake {
  animation: shake 0.5s;
}

/* Complex percentage-based keyframes */
@keyframes complexAnimation {
  0% {
    transform: scale(1) rotate(0deg);
    border-radius: 0%;
  }
  25% {
    transform: scale(1.5) rotate(90deg);
    border-radius: 25%;
  }
  50% {
    transform: scale(1) rotate(180deg);
    border-radius: 50%;
  }
  75% {
    transform: scale(1.5) rotate(270deg);
    border-radius: 25%;
  }
  100% {
    transform: scale(1) rotate(360deg);
    border-radius: 0%;
  }
}

.complex-anim {
  animation: complexAnimation 4s ease-in-out infinite;
}

/* Animation properties */
.animated-element {
  animation-name: slideInLeft;
  animation-duration: 1s;
  animation-timing-function: ease-out;
  animation-delay: 0.5s;
  animation-iteration-count: 1;
  animation-direction: normal;
  animation-fill-mode: forwards;
  animation-play-state: running;
}

/* Shorthand */
.animated-short {
  animation: slideInLeft 1s ease-out 0.5s 1 normal forwards running;
}

/* Infinite animations */
.infinite {
  animation: pulse 2s infinite;
}

/* Alternate direction */
@keyframes swing {
  0%, 100% { transform: rotate(0deg); }
  50% { transform: rotate(10deg); }
}

.swing {
  transform-origin: top center;
  animation: swing 2s ease-in-out infinite alternate;
}

/* Pausing animations */
.animated-box {
  animation: spin 2s linear infinite;
}

.animated-box:hover {
  animation-play-state: paused;
}

/* Fill modes */
.fill-forwards {
  animation: fadeIn 1s ease-in forwards; /* Keeps final state */
}

.fill-backwards {
  animation: fadeIn 1s ease-in backwards; /* Applies 0% styles during delay */
}

.fill-both {
  animation: fadeIn 1s ease-in both; /* Both forwards and backwards */
}

/* Transform animations (performant) */
@keyframes slideUp {
  from {
    transform: translateY(100%);
  }
  to {
    transform: translateY(0);
  }
}

/* Scale animations */
@keyframes scaleIn {
  from {
    transform: scale(0);
  }
  to {
    transform: scale(1);
  }
}

/* Gradient animation */
@keyframes gradientShift {
  0% {
    background-position: 0% 50%;
  }
  50% {
    background-position: 100% 50%;
  }
  100% {
    background-position: 0% 50%;
  }
}

.gradient-animation {
  background: linear-gradient(270deg, #ff6ec4, #7873f5, #4facfe);
  background-size: 600% 600%;
  animation: gradientShift 8s ease infinite;
}

/* Loading dots animation */
@keyframes dotPulse {
  0%, 80%, 100% {
    opacity: 0;
  }
  40% {
    opacity: 1;
  }
}

.loading-dots span {
  animation: dotPulse 1.4s infinite;
}

.loading-dots span:nth-child(1) { animation-delay: 0s; }
.loading-dots span:nth-child(2) { animation-delay: 0.2s; }
.loading-dots span:nth-child(3) { animation-delay: 0.4s; }

/* Performance optimization */
.will-animate {
  will-change: transform, opacity; /* Use sparingly */
}

/* Hardware acceleration hint */
.hardware-accelerated {
  transform: translateZ(0);
  backface-visibility: hidden;
}

/* Accessibility - respect reduced motion preference */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
1 file · css Explain with highlit

CSS transitions animate property changes with transition: property duration timing-function delay. I use transition-property to specify which properties animate. The timing-function controls acceleration with values like ease, ease-in, ease-out, cubic-bezier(). CSS animations use @keyframes with percentage-based waypoints. The animation shorthand sets name, duration, timing-function, delay, iteration-count, direction. Using animation-fill-mode controls styles before and after animation. The transform property animates efficiently with hardware acceleration. Performance-friendly properties include transform, opacity, and filter. I use will-change sparingly to hint at animations. The prefers-reduced-motion media query respects accessibility preferences.