Bird
Raised Fist0
CSSmarkup~15 mins

Animation duration and delay in CSS - Deep Dive

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Overview - Animation duration and delay
What is it?
Animation duration and delay are CSS properties that control how long an animation takes to complete and when it starts. Duration sets the total time an animation runs from start to finish. Delay sets the waiting time before the animation begins after being triggered. Together, they help create smooth and timed visual effects on web pages.
Why it matters
Without controlling animation duration and delay, animations would either run too fast or start immediately without any pause, making user interfaces feel abrupt or confusing. These properties let designers create natural, engaging experiences by pacing animations to match user expectations and content flow. Without them, animations could distract or frustrate users instead of enhancing the design.
Where it fits
Before learning animation duration and delay, you should understand basic CSS syntax and how animations work with @keyframes. After mastering these properties, you can explore advanced animation timing functions, chaining animations, and JavaScript-driven animation control.
Mental Model
Core Idea
Animation duration controls how long an animation takes, and delay controls when it starts, together timing the animation's flow.
Think of it like...
Imagine a race where duration is how long the runner takes to finish, and delay is the time the runner waits at the start line before beginning to run.
Animation Timeline:
┌───────────────┬───────────────┐
│   Delay      │   Duration    │
│ (wait time)  │ (run time)    │
└───────────────┴───────────────┘
Start → (delay) → Animation runs for (duration) → End
Build-Up - 7 Steps
1
FoundationUnderstanding animation duration basics
🤔
Concept: Animation duration sets how long an animation takes to complete one cycle.
In CSS, the property 'animation-duration' defines the length of time an animation runs. It accepts time values like '2s' for two seconds or '500ms' for half a second. For example: .my-box { animation-name: slide; animation-duration: 3s; } This means the 'slide' animation will take 3 seconds to finish one cycle.
Result
The animation runs smoothly over the specified time, neither too fast nor too slow.
Understanding duration lets you control the speed of animations, making them feel natural and readable.
2
FoundationGrasping animation delay basics
🤔
Concept: Animation delay sets how long to wait before starting the animation after it is triggered.
The 'animation-delay' property in CSS specifies a pause before the animation begins. It also accepts time values like '1s' or '200ms'. For example: .my-box { animation-name: fadeIn; animation-duration: 2s; animation-delay: 1s; } Here, the animation waits 1 second before starting the 2-second fade-in effect.
Result
The animation does not start immediately but waits for the delay time, creating a timed effect.
Knowing delay helps you sequence animations or create anticipation before an effect starts.
3
IntermediateCombining duration and delay for timing
🤔Before reading on: Do you think delay affects the total animation time or just the start? Commit to your answer.
Concept: Duration and delay work together to control when an animation starts and how long it lasts, but delay does not add to the animation's active time.
When you set both 'animation-duration' and 'animation-delay', the animation waits for the delay time, then runs for the duration time. For example: .my-box { animation-name: bounce; animation-duration: 4s; animation-delay: 2s; } The animation will start 2 seconds after being triggered and then run for 4 seconds. The total time from trigger to finish is 6 seconds, but the animation itself only lasts 4 seconds.
Result
Animations can be precisely timed to start later and run for a specific length, improving user experience.
Understanding that delay only postpones the start, not the animation length, helps avoid timing mistakes.
4
IntermediateUsing negative delay for animation control
🤔Before reading on: Can animation delay be negative? What would that do? Commit to your answer.
Concept: CSS allows negative values for animation-delay to start animations partway through their cycle immediately.
A negative 'animation-delay' means the animation starts as if it had already been running for that amount of time. For example: .my-box { animation-name: spin; animation-duration: 5s; animation-delay: -2s; } This causes the animation to begin 2 seconds into the 5-second cycle right away, skipping the first 2 seconds visually.
Result
Animations can appear mid-way through, useful for syncing or looping effects.
Knowing negative delay lets you control animation states precisely, especially for repeated or continuous animations.
5
IntermediateApplying duration and delay with multiple animations
🤔Before reading on: Do you think each animation in a list can have its own duration and delay? Commit to your answer.
Concept: When multiple animations are applied, each can have separate duration and delay values using comma-separated lists.
CSS allows multiple animations on one element, each with its own timing. For example: .my-box { animation-name: fadeIn, slideRight; animation-duration: 2s, 4s; animation-delay: 0s, 1s; } Here, 'fadeIn' runs for 2 seconds starting immediately, while 'slideRight' runs for 4 seconds starting after 1 second delay.
Result
Complex animation sequences can be created with precise timing control for each effect.
Understanding how to manage multiple animations' timing unlocks advanced visual storytelling.
6
AdvancedImpact of duration and delay on animation iteration
🤔Before reading on: Does delay repeat with each animation cycle or only before the first? Commit to your answer.
Concept: Animation delay only applies before the first iteration; duration controls each cycle's length during repeats.
When an animation repeats (using 'animation-iteration-count'), the delay happens only once before the first cycle. For example: .my-box { animation-name: pulse; animation-duration: 1s; animation-delay: 2s; animation-iteration-count: infinite; } The animation waits 2 seconds, then pulses every 1 second continuously without delay between cycles.
Result
Animations start after delay once, then repeat smoothly without extra pauses.
Knowing delay applies only once prevents confusion about pauses in looping animations.
7
ExpertBrowser rendering and animation timing precision
🤔Before reading on: Do you think animation duration and delay are always exact to the millisecond? Commit to your answer.
Concept: Browsers approximate animation timing based on frame rates and system performance, so duration and delay may not be perfectly precise.
CSS animations run on the browser's rendering engine, which updates frames typically every 16ms (60fps). If duration or delay values don't align with frame timing, the browser rounds to the nearest frame. Also, heavy CPU load or throttling can affect timing accuracy. This means animations might start slightly earlier or later and run a bit faster or slower than specified.
Result
Animations feel smooth but may have tiny timing variations depending on device and browser.
Understanding browser rendering limits helps set realistic expectations and debug timing issues in production.
Under the Hood
When a CSS animation is triggered, the browser schedules the animation timeline based on the specified duration and delay. The delay sets a timer before the animation frames start updating. Once the delay passes, the browser calculates intermediate animation states for each frame over the duration period, updating the element's styles accordingly. The animation runs on the compositor thread for smoothness, syncing with the display's refresh rate. Negative delays offset the animation's progress to a later point immediately.
Why designed this way?
CSS animation timing properties were designed to separate concerns: duration controls speed, delay controls start time, allowing flexible sequencing without complex scripting. This design keeps animations declarative and easy to manage. Negative delays were added later to support advanced use cases like syncing animations or starting mid-cycle without extra code. The browser's frame-based rendering balances smooth visuals with performance constraints.
Animation Timing Flow:

Trigger Event
   │
   ▼
[Delay Timer] ──> Waits specified delay time
   │
   ▼
[Animation Start] ──> Begins animation frames
   │
   ▼
[Frame Updates] ──> Updates styles each frame over duration
   │
   ▼
[Animation End] ──> Completes animation cycle

Negative Delay:

Trigger Event
   │
   ▼
[Start at offset time] ──> Skips initial frames, starts mid-animation
Myth Busters - 4 Common Misconceptions
Quick: Does animation delay add to the animation's active running time? Commit yes or no.
Common Belief:Animation delay increases the total time the animation is running and visible.
Tap to reveal reality
Reality:Animation delay only postpones the start; it does not extend the animation's active running time or visible effect.
Why it matters:Misunderstanding this causes timing errors, making animations appear out of sync or longer than intended.
Quick: Can negative animation delay cause the animation to start immediately? Commit yes or no.
Common Belief:Animation delay cannot be negative; it must always be zero or positive.
Tap to reveal reality
Reality:CSS allows negative animation delays, which start the animation partway through immediately.
Why it matters:Ignoring negative delays limits animation control and prevents advanced timing effects.
Quick: Does animation delay repeat before every cycle in a looping animation? Commit yes or no.
Common Belief:Animation delay happens before every iteration in a repeating animation.
Tap to reveal reality
Reality:Animation delay only applies once before the first iteration, not before each repeat.
Why it matters:Believing delay repeats causes confusion about unexpected pauses in looping animations.
Quick: Are animation duration and delay always perfectly precise to the millisecond? Commit yes or no.
Common Belief:Animation timing is exact and consistent across all browsers and devices.
Tap to reveal reality
Reality:Animation timing is approximate due to frame rates, rendering engine rounding, and device performance.
Why it matters:Expecting perfect precision leads to frustration when animations appear slightly off or jittery.
Expert Zone
1
Animation delay combined with negative values can be used to synchronize multiple animations precisely without JavaScript.
2
Browsers optimize animations by running them on the compositor thread, but complex timing can cause frame drops affecting perceived duration.
3
When using multiple animations, mismatched duration and delay lists can cause unexpected behavior if not carefully aligned.
When NOT to use
Avoid relying solely on CSS animation duration and delay for complex interactive timing that depends on user input or dynamic conditions; use JavaScript animation libraries or Web Animations API for precise control.
Production Patterns
In production, developers use animation duration and delay to stagger entrance effects, create loading spinners with continuous loops, and coordinate multi-step animations for onboarding flows, often combining with easing functions and JavaScript triggers.
Connections
Event Loop (JavaScript)
Both control timing and scheduling of actions over time.
Understanding animation delay is similar to understanding how the event loop schedules tasks, helping grasp asynchronous timing in web apps.
Music Tempo and Rhythm
Animation timing parallels musical timing with beats (duration) and rests (delay).
Knowing how musicians use rests and note lengths helps visualize how animation delay and duration create rhythm in UI motion.
Project Management Scheduling
Animation delay and duration resemble task start times and durations in project timelines.
Seeing animation timing as scheduling tasks helps plan complex animation sequences like managing project milestones.
Common Pitfalls
#1Setting animation delay expecting it to repeat with each cycle.
Wrong approach:.box { animation-name: pulse; animation-duration: 1s; animation-delay: 2s; animation-iteration-count: infinite; } /* Expecting 2s delay before every pulse */
Correct approach:.box { animation-name: pulse; animation-duration: 1s; animation-delay: 2s; animation-iteration-count: infinite; } /* Delay applies only once before first pulse */
Root cause:Misunderstanding that delay only applies before the first animation iteration.
#2Using the same duration and delay values for multiple animations without separating them.
Wrong approach:.box { animation-name: fadeIn, slideRight; animation-duration: 2s; animation-delay: 1s; } /* Both animations get same duration and delay */
Correct approach:.box { animation-name: fadeIn, slideRight; animation-duration: 2s, 4s; animation-delay: 0s, 1s; } /* Each animation has its own timing */
Root cause:Not using comma-separated lists for multiple animations' timing properties.
#3Expecting animation duration and delay to be perfectly precise on all devices.
Wrong approach:.box { animation-name: spin; animation-duration: 3.333s; animation-delay: 1.666s; } /* Expect exact timing every time */
Correct approach:.box { animation-name: spin; animation-duration: 3.3s; animation-delay: 1.7s; } /* Accept small timing variations due to frame rounding */
Root cause:Ignoring browser frame rate and rendering engine limitations.
Key Takeaways
Animation duration controls how long an animation runs, while delay controls when it starts after being triggered.
Delay only postpones the start of the animation and does not add to the animation's active running time.
Negative animation delay values let animations start partway through immediately, enabling advanced timing control.
In looping animations, delay applies only once before the first cycle, not before each repeat.
Browser rendering and frame rates cause animation timing to be approximate, not perfectly precise.

Practice

(1/5)
1. What does the CSS property animation-duration control?
easy
A. How long the animation runs
B. When the animation starts
C. The color of the animation
D. The size of the animated element

Solution

  1. Step 1: Understand the property purpose

    animation-duration sets the length of time an animation takes to complete one cycle.
  2. Step 2: Compare with other properties

    animation-delay controls start time, not duration. Color and size are unrelated.
  3. Final Answer:

    How long the animation runs -> Option A
  4. Quick 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
A. animation-delay: 2s;
B. animation-delay = 2s;
C. animation-delay: 2;
D. animation-delay: '2s';

Solution

  1. Step 1: Recall CSS property syntax

    CSS uses colon : to assign values, not equals =.
  2. Step 2: Check value format

    Time values require units like s for seconds without quotes.
  3. Final Answer:

    animation-delay: 2s; -> Option A
  4. Quick 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:
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
A. Starts after 3 seconds delay and moves for 1 second
B. Starts immediately and moves for 3 seconds
C. Starts after 1 second delay and moves for 3 seconds
D. Starts immediately and moves for 1 second

Solution

  1. Step 1: Identify animation-delay effect

    The animation waits 1 second before starting because of animation-delay: 1s;.
  2. Step 2: Identify animation-duration effect

    The animation runs for 3 seconds as set by animation-duration: 3s;.
  3. Final Answer:

    Starts after 1 second delay and moves for 3 seconds -> Option C
  4. Quick 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
A. animation-duration cannot be less than animation-delay
B. Two animation-delay properties; the last one overrides the first
C. animation-name must come before animation-duration
D. animation-delay value must be in seconds only

Solution

  1. Step 1: Check for duplicate properties

    The CSS has two animation-delay lines; the second one (1s) overrides the first (500ms).
  2. Step 2: Understand CSS property overriding

    In CSS, later declarations override earlier ones if they target the same property.
  3. Final Answer:

    Two animation-delay properties; the last one overrides the first -> Option B
  4. Quick 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
A. animation-duration: 4s; animation-delay: 0s;
B. animation-delay: 4s; animation-duration: 2s;
C. animation-duration: 2s; animation-delay: 4s;
D. animation-delay: 2s; animation-duration: 4s;

Solution

  1. Step 1: Set the delay to 2 seconds

    The animation should wait 2 seconds before starting, so animation-delay: 2s; is correct.
  2. Step 2: Set the duration to 4 seconds

    The animation should run for 4 seconds, so animation-duration: 4s; is correct.
  3. Step 3: Verify order and values

    Order does not affect behavior, but values must match requirements exactly.
  4. Final Answer:

    animation-delay: 2s; animation-duration: 4s; -> Option D
  5. Quick 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