Animation duration and delay control how long an animation takes and when it starts. This helps make web pages feel smooth and lively.
Animation duration and delay in CSS
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
CSS
animation-duration: <time>; animation-delay: <time>;
animation-duration sets how long the animation runs.
animation-delay sets how long to wait before starting the animation.
Examples
CSS
animation-duration: 2s;
CSS
animation-delay: 1s;
CSS
animation-duration: 500ms; animation-delay: 200ms;
Sample Program
This example shows a square box that waits 1 second, then slides to the right over 3 seconds.
CSS
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Animation Duration and Delay Example</title> <style> .box { width: 100px; height: 100px; background-color: coral; margin: 2rem auto; animation-name: slideRight; animation-duration: 3s; animation-delay: 1s; animation-fill-mode: forwards; } @keyframes slideRight { from { transform: translateX(0); } to { transform: translateX(200px); } } </style> </head> <body> <div class="box" aria-label="Animated box sliding right"></div> </body> </html>
Important Notes
You can use seconds (s) or milliseconds (ms) for time values.
Use animation-fill-mode: forwards; to keep the animation's end state visible.
Delays help you create nice effects by starting animations at different times.
Summary
animation-duration controls how long an animation runs.
animation-delay controls when the animation starts.
Using both lets you create smooth, timed animations on your web page.
Practice
1. What does the CSS property
animation-duration control?easy
Solution
Step 1: Understand the property purpose
animation-durationsets the length of time an animation takes to complete one cycle.Step 2: Compare with other properties
animation-delaycontrols start time, not duration. Color and size are unrelated.Final Answer:
How long the animation runs -> Option AQuick Check:
animation-duration = length of animation [OK]
Hint: Duration means how long something lasts [OK]
Common Mistakes:
- Confusing duration with delay
- Thinking it changes color or size
- Mixing up start time and length
2. Which of the following is the correct syntax to set an animation delay of 2 seconds in CSS?
easy
Solution
Step 1: Recall CSS property syntax
CSS uses colon:to assign values, not equals=.Step 2: Check value format
Time values require units likesfor seconds without quotes.Final Answer:
animation-delay: 2s; -> Option AQuick Check:
Correct CSS syntax uses colon and units [OK]
Hint: Use colon and units without quotes for CSS times [OK]
Common Mistakes:
- Using equals sign instead of colon
- Omitting time units
- Adding quotes around time values
3. Given this CSS animation:
When will the div start moving and how long will the movement last?
div {
animation-name: slide;
animation-duration: 3s;
animation-delay: 1s;
animation-fill-mode: forwards;
}
@keyframes slide {
from { transform: translateX(0); }
to { transform: translateX(100px); }
}When will the div start moving and how long will the movement last?
medium
Solution
Step 1: Identify animation-delay effect
The animation waits 1 second before starting because ofanimation-delay: 1s;.Step 2: Identify animation-duration effect
The animation runs for 3 seconds as set byanimation-duration: 3s;.Final Answer:
Starts after 1 second delay and moves for 3 seconds -> Option CQuick Check:
Delay = 1s, Duration = 3s [OK]
Hint: Delay waits, duration runs animation [OK]
Common Mistakes:
- Mixing delay and duration times
- Assuming animation starts immediately
- Confusing delay length with duration length
4. Identify the error in this CSS snippet:
.box {
animation-duration: 2s;
animation-delay: 500ms;
animation-name: fadeIn;
animation-delay: 1s;
}
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}medium
Solution
Step 1: Check for duplicate properties
The CSS has twoanimation-delaylines; the second one (1s) overrides the first (500ms).Step 2: Understand CSS property overriding
In CSS, later declarations override earlier ones if they target the same property.Final Answer:
Two animation-delay properties; the last one overrides the first -> Option BQuick Check:
Duplicate properties override earlier ones [OK]
Hint: Last property wins if repeated in CSS [OK]
Common Mistakes:
- Thinking duration must be longer than delay
- Believing property order matters for validity
- Assuming delay must be seconds only
5. You want an animation to start 2 seconds after page load and run for 4 seconds. Which CSS snippet achieves this correctly?
hard
Solution
Step 1: Set the delay to 2 seconds
The animation should wait 2 seconds before starting, soanimation-delay: 2s;is correct.Step 2: Set the duration to 4 seconds
The animation should run for 4 seconds, soanimation-duration: 4s;is correct.Step 3: Verify order and values
Order does not affect behavior, but values must match requirements exactly.Final Answer:
animation-delay: 2s; animation-duration: 4s; -> Option DQuick Check:
Delay 2s, duration 4s matches requirement [OK]
Hint: Delay first, then duration for timing [OK]
Common Mistakes:
- Swapping delay and duration values
- Setting delay longer than needed
- Assuming order changes effect
