What You'll Learn
You'll learn how to control exactly how long a CSS transition takes to complete by setting a time duration.
Understanding Transition Duration
When you create a CSS transition, the browser needs to know how much time to take moving from the starting value to the ending value. That's where transition-duration comes in. This property accepts time values that tell the browser, "Take this long to complete the animation."
Think of it like driving from one city to another. The transition-property decides what is changing (your location), but transition-duration decides how fast you drive—whether you take 2 seconds or 2 minutes to get there.
How to Use It
The transition-duration property accepts time values in two units:
- Seconds:
s(e.g.,1s,0.5s,2.3s) - Milliseconds:
ms(e.g.,500ms,1000ms,250ms)
.box {
background-color: blue;
transition-property: background-color;
transition-duration: 2s; /* Takes 2 full seconds */
}
.box:hover {
background-color: red;
}
In this example, when you hover over the box, the background color smoothly transitions from blue to red over exactly 2 seconds.
Common Duration Ranges
- Quick feedback:
150msto300ms(feels snappy and responsive) - Noticeable animations:
500msto1s(clear but not sluggish) - Dramatic effects:
1sto3s(deliberate and attention-grabbing)
Shorter durations feel more immediate; longer ones feel more deliberate. For most UI interactions, keeping transitions under 500ms creates a responsive feel.
Key Takeaway: transition-duration controls how many seconds or milliseconds a transition takes to complete, letting you fine-tune the speed of your animations for the perfect user experience.