CSS Transition Not Smooth Fix
In this tutorial, you'll learn about CSS Transition Not Smooth Fix. We cover key concepts, practical examples, and best practices.
The Problem
You add transition: all 0.3s to an element but the change is instant and jarring. The transition either does not fire, fires on the wrong property, or animates choppily.
Quick Fix
Step 1: Specify the property and duration
.element {
/* Wrong — no transition defined */
background-color: blue;
}
.element:hover {
background-color: red;
}
/* Right */
.element {
background-color: blue;
transition: background-color 0.3s ease;
}
.element:hover {
background-color: red;
}
Expected output: Background color changes smoothly over 300ms on hover.
Step 2: Do not transition display
CSS cannot animate display. Use opacity and visibility instead:
/* Wrong — display does not animate */
.modal {
display: none;
transition: display 0.3s;
}
.modal.open {
display: block;
}
/* Right */
.modal {
opacity: 0;
visibility: hidden;
transition: opacity 0.3s, visibility 0.3s;
}
.modal.open {
opacity: 1;
visibility: visible;
}
Expected output: The modal fades in smoothly.
Step 3: Use transform instead of positional properties
Animating left, top, width, or height triggers layout recalculations:
/* Wrong — layout-triggering properties */
.element {
left: 0;
transition: left 0.3s;
}
.element.move {
left: 100px;
}
/* Right — GPU-accelerated properties */
.element {
transform: translateX(0);
transition: transform 0.3s;
}
.element.move {
transform: translateX(100px);
}
Expected output: The element slides without jank.
Step 4: Check for initial state mismatch
The initial state must match the transitioned property:
/* Wrong — no initial width set */
.element {
transition: width 0.3s;
}
.element.expand {
width: 200px;
}
/* Right */
.element {
width: 100px;
transition: width 0.3s;
}
Expected output: Width transitions from 100px to 200px.
Step 5: Avoid transition: all for performance
/* Wrong — transitions everything */
.element {
transition: all 0.3s;
}
/* Right — target specific properties */
.element {
transition: opacity 0.3s, transform 0.3s;
}
Expected output: Only targeted properties animate, reducing GPU load.
Prevention
- Always set an initial value for the property you want to transition
- Use
transformandopacityfor smooth 60fps animations - Avoid
transition: all— be explicit about which properties animate - Test transitions with Chrome DevTools Performance tab
Common Mistakes with transition not smooth
- Using
foldlinstead offoldl'causing stack overflow on large lists - Forgetting
deriving (Show, Eq)on custom data types needed for debugging - Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
These mistakes appear frequently in real-world CSS code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro