β±οΈ CSS Time Units β s & ms Explained
π What Are Time Units in CSS?
Time units in CSS are used to control the timing of animations, transitions, delays, and keyframe durations. The two main units are:
- s β seconds
- ms β milliseconds
>>βCSS time units shape how smooth, fast, or dramatic your animations feel.β
π¦ The Two Time Units
| Unit | Meaning | Equivalent |
|---|---|---|
| s | Seconds | 1s = 1000ms |
| ms | Milliseconds | 1000ms = 1s |
Note
β Both units work in transitions, animations, delays, and timing functions.
β Choose based on how precise your timing needs to be.
β Choose based on how precise your timing needs to be.
π§ͺ Where Are Time Units Used?
- transition-duration
- transition-delay
- animation-duration
- animation-delay
- animation-iteration-count (indirectly)
- scroll-behavior timing in JS (not CSS)
β±οΈ 1. Using Seconds (s)
The s unit is best for readable values in longer animations or for transitions that need clarity.
Using seconds
.fade {
transition: opacity 1s;
}This fades the element over 1 second.
β‘ 2. Using Milliseconds (ms)
ms is great for precision and micro-interactions (like button hovers).
Using milliseconds
button {
transition: background-color 150ms;
}150ms is perfect for snappy UI behavior.
π§ s vs ms β When to Use Which?
| Use Case | Recommended Unit |
|---|---|
| Micro animations (hover, click) | ms (e.g., 100msβ300ms) |
| Long transitions (fade, move) | s (e.g., 1sβ3s) |
| Keyframe animations | s |
| Precise timing | ms |
π§ͺ Practical Examples
1οΈβ£ Button Hover Animation
Hover using ms
button:hover {
transform: scale(1.05);
transition: transform 120ms ease;
}2οΈβ£ Smooth Page Fade-In
Fade-in using seconds
.page {
animation: fadeIn 1.4s ease-in-out;
}
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}3οΈβ£ Animation Delay
Animation delay
.box {
animation: slide 2s ease-in-out 500ms;
}Delay of 500ms before animation begins.
π¨ Combining s + ms
Combining Units
.combo {
transition: opacity 0.5s, transform 250ms;
}You can mix seconds and milliseconds in one declaration.
β οΈ Common Mistakes
- β Writing 0s instead of simply 0
- β Using long durations (over 2s) for simple UI interactions
- β Forgetting that delay and duration both accept s/ms
- β Making animations too slow β bad UX
Note
π― Ideal micro-interaction durations: 150msβ300ms
π― Ideal fade durations: 0.5sβ1.5s
π― Ideal fade durations: 0.5sβ1.5s
π₯ Summary
CSS time units s and ms control animation and transition timing. Use seconds for readability and longer effects, and millisecondsfor precision and micro-interactions.
>>βGreat UI feels smooth not because of graphics β but because of timing.β
Learn more β MDN Docs π