Tillitsdone
down Scroll to discover

CSS Animation-Duration Enhance Your Web Designs

Learn how to use CSS animation-duration to control animation timing.

Options include seconds, milliseconds, inherit, initial, revert, and more.
thumbnail

What is animation-duration?

The animation-duration property in CSS controls how long an animation takes to complete one cycle. It’s essential for creating smooth and engaging animations that enhance user experience.

Syntax

Here’s how you can use animation-duration:

/* Single animation */
animation-duration: auto; /* Default */
animation-duration: 6s;
animation-duration: 120ms;
/* Multiple animations */
animation-duration: 1.64s, 15.22s;
animation-duration: 10s, 35s, 230ms;
/* Global values */
animation-duration: inherit;
animation-duration: initial;
animation-duration: revert;
animation-duration: revert-layer;
animation-duration: unset;
  • auto: Equivalent to 0s for time-based animations. For scroll-driven animations, it fills the entire timeline.
  • <time>: Specifies the duration in seconds (s) or milliseconds (ms). The value must be positive or zero.

Note: Negative values are invalid and will be ignored.

Property Values

  • auto: Same as 0s for time-based animations.
  • <time>: Duration in seconds (s) or milliseconds (ms).
  • inherit: Inherits the value from the parent element.
  • initial: Resets to the default value (0s).
  • revert/revert-layer: Resets to the user agent’s default value.
  • unset: Resets to the natural value.

Examples

Setting Animation Duration

Let’s create a simple animation that rotates a square.

HTML:

<div class="box"></div>

CSS:

.box {
background-color: rebeccapurple;
border-radius: 10px;
width: 100px;
height: 100px;
}
.box:hover {
animation-name: rotate;
animation-duration: 0.7s;
}
@keyframes rotate {
0% {
transform: rotate(0);
}
100% {
transform: rotate(360deg);
}
}

Multiple Animations

Let’s move a square horizontally and change its color with different durations.

HTML:

<div class="box"></div>

CSS:

.box {
background-color: rebeccapurple;
border-radius: 10px;
width: 100px;
height: 100px;
animation-name: move, colorChange;
animation-duration: 2s, 4s;
animation-iteration-count: infinite;
}
@keyframes move {
0% {
transform: translateX(0);
}
100% {
transform: translateX(200px);
}
}
@keyframes colorChange {
0% {
background-color: rebeccapurple;
}
50% {
background-color: steelblue;
}
100% {
background-color: rebeccapurple;
}
}

CSS Scroll-Driven Animations

For scroll-driven animations, animation-duration works differently.

HTML:

<div class="scroll-container">
<div class="scroll-element">Scroll-driven animation</div>
</div>

CSS:

.scroll-container {
height: 200vh; /* Make the container tall enough to scroll */
position: relative;
}
.scroll-element {
position: sticky;
top: 50%;
transform: translateY(-50%);
text-align: center;
font-size: 2rem;
opacity: 0;
animation-name: fadeIn;
animation-duration: 1ms;
animation-timeline: scroll();
animation-fill-mode: forwards;
}
@keyframes fadeIn {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}

Best Practices

  1. Keep Animations Subtle: Avoid overly long animations that can distract users.
  2. Performance Considerations: Test on different devices to ensure smooth performance.
  3. User Experience: Ensure animations enhance the user experience without overwhelming the interface.
  4. Consistent Timing: Maintain consistent timing across different animations.
  5. Accessibility: Provide options to disable animations for users with disabilities.
  6. Compatibility: Test across different browsers to ensure consistent behavior.
  7. Use Shorthand Properties: Use the shorthand animation property to simplify your CSS.
  8. Documentation: Document your animations and their durations in your codebase.
  9. Optimize for Mobile: Optimize animations for mobile devices.
  10. Feedback: Use animations to provide visual feedback to users.

Supported Browsers

The animation-duration property is widely supported across modern web browsers:

  • Google Chrome: Supported since version 43.0 (May 2015).
  • Mozilla Firefox: Supported since version 16.0 (October 2012).
  • Microsoft Edge: Supported since version 12.0 (2015).
  • Opera: Supported since version 30.0 (June 2015).
  • Safari: Supported since version 9.0 (September 2015).

Compatibility Considerations

Thoroughly test your animations across different browsers and devices to ensure consistent behavior. For CSS scroll-driven animations, setting animation-duration to 1ms is recommended for compatibility with browsers like Firefox.

FAQs

What is the animation-duration property in CSS?

The animation-duration property defines how long an animation takes to complete one cycle.

What values can be used with the animation-duration property?

You can use time values like seconds (s) or milliseconds (ms). Additionally, you can use values like inherit, initial, revert, revert-layer, and unset.

What is the default value of animation-duration?

The default value is 0s, meaning the animation won’t play unless explicitly specified.

How does animation-duration interact with animation-delay?

The animation-delay property specifies when the animation should start, while animation-duration defines how long it takes to complete.

How does animation-duration work with animation-iteration-count?

The animation-iteration-count property specifies the number of times an animation cycle should be played. The animation-duration property defines the duration of each cycle.

What happens if no value is provided for animation-duration?

If no value is provided, the default value of 0s is used.

Can negative values be used with animation-duration?

Negative values are invalid and will be ignored.

How do multiple animation-duration values apply to multiple animations?

When specifying multiple comma-separated values, they are applied to the animations in the order of the animation-name properties.

What is the recommended animation-duration for CSS scroll-driven animations?

Setting animation-duration to 1ms is recommended for scroll-driven animations to ensure compatibility with browsers like Firefox.

Related Properties

The animation-duration property is a key part of CSS animations, and it works with other properties to create amazing effects. Here are some related properties you can use to make your animations even better:

  1. animation: This shorthand property lets you set multiple animation properties at once, including animation-name, animation-duration, animation-timing-function, animation-delay, animation-iteration-count, animation-direction, animation-fill-mode, and animation-play-state.
  2. animation-name: Specifies the name of the keyframes animation to apply. This property is essential for defining the animation sequence.
  3. animation-timing-function: Defines the speed curve of the animation. It controls how the animation speeds up and slows down over its duration.
  4. animation-delay: Specifies the delay before the animation starts. This property is useful for staggering animations.
  5. animation-iteration-count: Sets the number of times an animation cycle should be played. You can specify a number or use infinite for continuous playback.
  6. animation-direction: Defines the direction of the animation. It can be set to normal, reverse, alternate, or alternate-reverse.
  7. animation-fill-mode: Determines how a CSS animation applies styles before and after it is executing. Values include none, forwards, backwards, and both.
  8. animation-play-state: Allows you to pause and resume animations. This property can be set to running or paused.
  9. animation-timeline: Specifies the timeline for the animation. This property is particularly useful for scroll-driven animations.
  10. animation-composition: This newer property allows you to composite multiple animations together, providing more control over how animations interact with each other.

By understanding and using these related properties, you can create sophisticated and engaging animations that enhance the user experience on your website. Each property plays a crucial role in defining the behavior and appearance of your animations, allowing you to create dynamic and interactive web designs.

FAQs

What is the animation-duration property in CSS?

The animation-duration property in CSS defines how long an animation should take to complete one cycle from start to finish. It’s crucial for controlling the timing of animations, making it essential for creating dynamic and engaging web designs.

What values can be used with the animation-duration property?

You can use time values like seconds (e.g., 2s) or milliseconds (e.g., 500ms) to specify the duration. Additionally, you can use values like inherit, initial, revert, revert-layer, and unset to control the property’s behavior.

What is the default value of animation-duration?

The default value is 0s, which means the animation won’t play because it has no duration unless explicitly specified.

How does animation-duration interact with animation-delay?

The animation-delay property specifies when the animation should start, while animation-duration defines how long it takes to complete. The delay occurs before the duration begins.

How does animation-duration work with animation-iteration-count?

The animation-iteration-count property specifies the number of times an animation cycle should be played. The animation-duration property defines the duration of each cycle. Together, they control the overall timing and repetition of the animation.

What happens if no value is provided for animation-duration?

If no value is provided, the default value of 0s is used. The animation still executes but may not be visible depending on the animation-fill-mode property.

Can negative values be used with animation-duration?

Negative values are invalid and will be ignored. Some early implementations may treat them as 0s.

How do multiple animation-duration values apply to multiple animations?

When specifying multiple comma-separated values, they are applied to the animations in the order of the animation-name properties. If the number of animations and animation-duration values do not match, see Setting multiple animation property values.

What is the recommended animation-duration for CSS scroll-driven animations?

For CSS scroll-driven animations, specifying an animation-duration value in seconds or milliseconds may not have the intended effect. Setting animation-duration to 1ms is recommended to ensure compatibility with browsers like Firefox.

By addressing these frequently asked questions, you can gain a deeper understanding of how to effectively use the animation-duration property in your CSS animations. This knowledge will help you create smooth, engaging, and performant animations that enhance the user experience on your website.

icons/css-4.svg CSS Blogs
CSS3 is the latest version of Cascading Style Sheets, offering advanced styling features like animations, transitions, shadows, gradients, and responsive design.
icons/logo-tid.svg

Talk with CEO

Ready to bring your web/app to life or boost your team with expert Thai developers?
Contact us today to discuss your needs, and let’s create tailored solutions to achieve your goals. We’re here to help at every step!
🖐️ Contact us
Let's keep in Touch
Thank you for your interest in Tillitsdone! Whether you have a question about our services, want to discuss a potential project, or simply want to say hello, we're here and ready to assist you.
We'll be right here with you every step of the way.
Contact Information
rick@tillitsdone.com+66824564755
Find All the Ways to Get in Touch with Tillitsdone - We're Just a Click, Call, or Message Away. We'll Be Right Here, Ready to Respond and Start a Conversation About Your Needs.
Address
9 Phahonyothin Rd, Khlong Nueng, Khlong Luang District, Pathum Thani, Bangkok Thailand
Visit Tillitsdone at Our Physical Location - We'd Love to Welcome You to Our Creative Space. We'll Be Right Here, Ready to Show You Around and Discuss Your Ideas in Person.
Social media
Connect with Tillitsdone on Various Social Platforms - Stay Updated and Engage with Our Latest Projects and Insights. We'll Be Right Here, Sharing Our Journey and Ready to Interact with You.
We anticipate your communication and look forward to discussing how we can contribute to your business's success.
We'll be here, prepared to commence this promising collaboration.
Frequently Asked Questions
Explore frequently asked questions about our products and services.
Whether you're curious about features, warranties, or shopping policies, we provide comprehensive answers to assist you.