๐จ Mastering CSS Transitions & Animations โ Beginner to Advanced ๐

๐ง 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
Property | Description |
transition-property | What property to animate |
transition-duration | How long it lasts (0.5s , 200ms ) |
transition-timing-function | Speed pattern (ease , linear , etc.) |
transition-delay | How 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
Property | Description |
animation-name | Links to @keyframes |
animation-duration | Duration of animation |
animation-timing-function | Speed curve |
animation-delay | Delay before animation starts |
animation-iteration-count | 1 , infinite , etc. |
animation-direction | normal , alternate , reverse |
animation-fill-mode | Keeps 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
๐ Useful Links
๐ LinkedIn: https://linkedin.com/in/krishan86
๐ฆ Twitter (X): https://x.com/KrishanCodes
๐ GitHub Repo: Frontend-Elevate
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!