Special Offer Diwali Special Bundle Offer: 3 courses for ₹8,999, save ₹9,001 (50% OFF)! Limited seats available. View Bundles →
0% Completed
Tutorials / CSS Tutorial / Chapter 5: Advanced Selectors, Effects & Variables
CSS ⏱ 12 min read

Transitions and Animations (@keyframes)

Subtle, purposeful motion makes websites feel responsive, alive, and polished.

What is it?

CSS transitions smoothly interpolate property values over a duration when states change. CSS animations (@keyframes) create complex, continuous multi-step animations without JavaScript.

Why is it used?

Provides smooth visual feedback on buttons, cards, modals, page transitions, and loading spinners.

Detailed Explanation

Transitions: • `transition: property duration timing-function delay;` • Example: `transition: all 0.25s cubic-bezier(0.16, 1, 0.3, 1);` • Performant properties to animate: `transform` (scale, translate, rotate) and `opacity`. These are GPU-accelerated and won't cause layout reflow. Keyframe Animations: • Defined via `@keyframes animationName { from { ... } to { ... } }` or percentages `0% { ... } 50% { ... } 100% { ... }`. • Applied using `animation: name duration timing-function iteration-count;`.

Syntax Structure

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

.spinner {
  animation: spin 1s linear infinite;
}

Code Example

CSS Code
/* GPU-Accelerated Button Micro-interaction */
.btn-animated {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  background: #1c1c1c;
  color: #fff;
  padding: 12px 24px;
  border-radius: 6px;
  text-decoration: none;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.btn-animated:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(28, 28, 28, 0.15);
}

.btn-animated:active {
  transform: translateY(0);
}

/* Subtle Pulsing Badge */
@keyframes pulseBadge {
  0%, 100% { transform: scale(1); opacity: 1; }
  50% { transform: scale(1.05); opacity: 0.85; }
}

.live-indicator {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #10b981;
  animation: pulseBadge 2s infinite ease-in-out;
}

Explanation of the Code

• Button hovers smoothly lift by 2px and cast a gentle shadow using GPU hardware acceleration. • The live indicator pulses continuously to show active session status.

Expected Output

Smooth button lift and depression on click, and an organic emerald pulsing badge indicator.

Real-World Application

Used across modern SaaS dashboards for loading skeleton screens, toast notification entrances, and button hover states.

Common Mistakes to Avoid

• Animating heavy layout properties like `width`, `height`, or `top`/`left` which cause costly browser reflows and stuttering. Always animate `transform` and `opacity`. • Excessive animations that distract users or cause motion sickness (respect `@media (prefers-reduced-motion)`).

Quick Practice Challenge

Create an infinite spinning loading spinner using `@keyframes` and `transform: rotate(360deg)`.

Key Takeaways

• Animate `transform` and `opacity` for 60fps smooth hardware acceleration. • Transitions handle state changes; `@keyframes` handle autonomous animations. • Keep interface animations snappy (under 300ms).