DEV Community

Code WithDhanian
Code WithDhanian

Posted on

1

Mastering the CSS `animation-delay` Property: A Handy Cheatsheet.

The animation-delay property in CSS is a powerful tool for controlling the timing of animations. Whether you're creating subtle transitions or complex animations, understanding how to use animation-delay effectively can elevate your web design. This article serves as a quick reference and cheatsheet for the animation-delay property, covering its syntax, values, and practical examples.


Syntax of animation-delay

The animation-delay property is straightforward to use. Here’s the basic syntax:

animation-delay: time | initial | inherit;
Enter fullscreen mode Exit fullscreen mode
  • time: Specifies the delay before the animation starts. It can be defined in seconds (s) or milliseconds (ms).
  • initial: Sets the property to its default value (0s).
  • inherit: Inherits the value from the parent element.

Values of animation-delay

1. Positive Delay

A positive value delays the start of the animation. For example:

animation-delay: 2s;       /* Animation starts after 2 seconds */
animation-delay: 500ms;    /* Animation starts after 500 milliseconds */
Enter fullscreen mode Exit fullscreen mode

This is useful for creating staggered animations or sequencing multiple effects.


2. Negative Delay

A negative value makes the animation start immediately, but it begins as if it has already been playing for the specified time. For example:

animation-delay: -1s;      /* Animation starts as if it has already been running for 1 second */
Enter fullscreen mode Exit fullscreen mode

This is particularly useful for creating animations that appear to be in progress when the page loads.


3. Zero Delay

Using 0s (or simply 0) starts the animation immediately:

animation-delay: 0s;       /* No delay, animation starts right away */
Enter fullscreen mode Exit fullscreen mode

4. Multiple Delays

When applying multiple animations to an element, you can specify different delays for each animation. For example:

animation-name: fadeIn, slideIn;
animation-delay: 1s, 2s;   /* fadeIn starts after 1s, slideIn starts after 2s */
Enter fullscreen mode Exit fullscreen mode

This allows for complex, multi-step animations with precise timing.


Practical Examples

Example 1: Simple Delay

Delay an animation by 3 seconds:

.element {
  animation-name: spin;
  animation-duration: 2s;
  animation-delay: 3s;
}
Enter fullscreen mode Exit fullscreen mode

Example 2: Staggered Animations

Create a staggered effect for a list of items:

.item {
  animation-name: fadeIn;
  animation-duration: 1s;
}

.item:nth-child(1) { animation-delay: 0s; }
.item:nth-child(2) { animation-delay: 0.5s; }
.item:nth-child(3) { animation-delay: 1s; }
Enter fullscreen mode Exit fullscreen mode

Example 3: Negative Delay

Make an animation appear as if it’s already in progress:

.element {
  animation-name: slideIn;
  animation-duration: 3s;
  animation-delay: -1s; /* Starts mid-animation */
}
Enter fullscreen mode Exit fullscreen mode

Browser Support

The animation-delay property is widely supported across all modern browsers, including Chrome, Firefox, Safari, Edge, and Opera. For older browsers, consider using vendor prefixes like -webkit- or -moz-.


Conclusion

The animation-delay property is an essential tool for controlling the timing of CSS animations. Whether you're creating simple transitions or complex sequences, mastering this property allows you to craft more engaging and dynamic user experiences. Keep this cheatsheet handy for quick reference, and experiment with different values to see how they can enhance your animations!

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

DEV is better (more customized, reading settings like dark mode etc) when you're signed in!

Okay