Time Units: s and ms
What you'll learn: How to use seconds (s) and milliseconds (ms) to control the duration and timing of CSS animations and transitions.
The Core Idea
When you want elements on your webpage to move, fade, or change smoothly over time, you need to tell CSS how long those changes should take. CSS provides two time units for this: seconds (s) and milliseconds (ms).
s= seconds (1s = one second)ms= milliseconds (1000ms = one second)
Think of time units like setting a cooking timer. If you want your element to fade in slowly, you might use 2s (two seconds). For a quick, snappy change, you might use 200ms (one-fifth of a second).
Practical Usage
Time units appear in properties related to animation and transition timing. Here's how you write them:
.box {
transition: background-color 0.5s;
/* Background color changes over half a second */
}
.circle {
transition: transform 300ms;
/* Shape transforms in 300 milliseconds */
}
Both units are interchangeable. 1s equals 1000ms, so 0.5s is the same as 500ms. Use whichever feels more readable—many developers prefer milliseconds for short durations and seconds for longer ones.
Important Notes
- Time values must include the unit—
transition: 2won't work, buttransition: 2swill - You can use decimals:
0.3sor1.5s - Zero values technically don't need a unit (
0sor0both work), but being explicit is clearer
Key Takeaway: Use s (seconds) and ms (milliseconds) to define how long CSS transitions and animations should take—they're your tools for controlling the speed of visual changes.