๐ŸŽจ Mastering CSS Transitions & Animations โ€” Beginner to Advanced ๐Ÿš€

krishankrishan
3 min read

๐Ÿง  Part 1: CSS Transitions โ€” Smooth State Changes

๐Ÿ”น What is a Transition?

A transition allows CSS properties to change over time, creating smooth motion instead of an instant switch.

โœ… Basic Syntax:

selector {
  transition: property duration timing-function delay;
}

โœ… Example:

<div class="box"></div>

<style>
.box {
  width: 100px;
  height: 100px;
  background: coral;
  transition: background 0.3s ease, transform 0.5s ease-in-out;
}

.box:hover {
  background: teal;
  transform: scale(1.2);
}
</style>

Hover over the .box and see it smoothly scale and change color.

๐Ÿ”น Properties You Can Transition

  • color, background, opacity, transform, box-shadow, height, width, etc.

๐Ÿง  Part 2: Deep Dive into Transition Properties

PropertyDescription
transition-propertyWhat property to animate
transition-durationHow long it lasts (0.5s, 200ms)
transition-timing-functionSpeed pattern (ease, linear, etc.)
transition-delayHow long to wait before it starts

โฑ Common Timing Functions

  • ease (default)

  • linear

  • ease-in

  • ease-out

  • ease-in-out

  • cubic-bezier() โ€” custom speed curves


๐Ÿง  Part 3: CSS Animations โ€” Multi-step Motion ๐ŸŽฌ

๐Ÿ”น What are Animations?

Animations let you change styles over time using @keyframes. Unlike transitions, animations are not triggered by events like :hover.

โœ… Syntax:

@keyframes animName {
  0%   { transform: scale(1); }
  100% { transform: scale(1.5); }
}

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

โœ… Example:

<div class="pulse"></div>

<style>
.pulse {
  width: 100px;
  height: 100px;
  background: purple;
  animation: pulseAnim 1s infinite ease-in-out;
}

@keyframes pulseAnim {
  0%, 100% { transform: scale(1); }
  50%      { transform: scale(1.2); }
}
</style>

This .pulse div will grow and shrink in a loop.


๐Ÿง  Part 4: Animation Properties Breakdown

PropertyDescription
animation-nameLinks to @keyframes
animation-durationDuration of animation
animation-timing-functionSpeed curve
animation-delayDelay before animation starts
animation-iteration-count1, infinite, etc.
animation-directionnormal, alternate, reverse
animation-fill-modeKeeps styles before/after animation ends

โœ… Bouncing Ball Example:

<div class="ball"></div>

<style>
.ball {
  width: 50px;
  height: 50px;
  background: tomato;
  border-radius: 50%;
  position: relative;
  animation: bounce 1s infinite alternate ease-in-out;
}

@keyframes bounce {
  0%   { top: 0; }
  100% { top: 200px; }
}
</style>

๐Ÿง  Part 5: Mixing Transitions & Animations

.box {
  transition: transform 0.3s ease;
  animation: spin 5s linear infinite;
}

.box:hover {
  transform: scale(1.2);
}

@keyframes spin {
  0%   { rotate: 0deg; }
  100% { rotate: 360deg; }
}

๐Ÿ’ผ Interview Questions (3 Years of Experience)

1. What is the difference between CSS transitions and animations?

Answer: Transitions are event-driven (e.g., hover), and animations are timeline-driven using @keyframes.

2. Can you animate display: none to block?

Answer: No. You need to animate opacity, transform, or use height/width as a workaround.

3. What does animation-fill-mode: forwards do?

Answer: It keeps the final state of the animation after it finishes.

4. How can you pause/resume a CSS animation?

Answer:

animation-play-state: paused; // or 'running'

Used with JavaScript or pseudo-classes like :hover.


๐Ÿงช Pro Tips

โœ… Use will-change: transform for performance boosts
โœ… Combine multiple animations: animation: spin 1s ease, pulse 2s ease-in-out
โœ… Use animation-play-state for interactive control


๐Ÿ“š External Resources


๐Ÿ”— LinkedIn: https://linkedin.com/in/krishan86

๐Ÿฆ Twitter (X): https://x.com/KrishanCodes
๐Ÿ”— GitHub Repo: Frontend-Elevate

10
Subscribe to my newsletter

Read articles from krishan directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

krishan
krishan

๐Ÿ‘‹ Hey, I'm a Frontend Developer passionate about building clean, user-friendly interfaces. ๐Ÿš€ Learning and sharing everything from React, JavaScript, HTML/CSS to advanced topics like Data Structures, Algorithms & System Design. ๐Ÿ’ป Documenting my journey from fundamentals to becoming a top-tier developer โ€” one blog at a time. ๐Ÿ“š Join me as I break down complex topics into easy, practical lessons โ€” with GitHub repos, code snippets, and real-world examples. ๐Ÿ” Consistent growth, community learning, and aiming high!