CSS Properties
CSS animation-duration Property
Photo Credit to CodeToFun
🙋 Introduction
The animation-duration
property in CSS is used to specify the length of time an animation takes to complete one cycle.
This property is crucial when defining animations, as it controls the speed at which the animation plays. By adjusting the animation-duration
, you can create animations that are fast and dynamic or slow and smooth, depending on your design needs.
💡 Syntax
The animation-duration
property is defined in seconds (s) or milliseconds (ms). You can apply it to any element that you want to animate.
element {
animation-duration: time;
}
Here, time can be a value in seconds (e.g., 2s) or milliseconds (e.g., 2000ms).
🎛️ Default Value
The default value of the animation-duration
property is 0s, which means that without explicitly setting this property, the animation will not play because it has no duration.
🏠 Property Values
- <time>: Defines the duration of the animation. It can be specified in:
- Seconds (s): For example, 2s for two seconds.
- Milliseconds (ms): For example, 500ms for five hundred milliseconds.
📄 Example
In this example, we will animate a div element's background color change over a duration of 3 seconds.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS animation-duration Example</title>
<style>
@keyframes colorChange {
from { background-color: red; }
to { background-color: yellow; }
}
.animated-box {
width: 100px;
height: 100px;
animation-name: colorChange;
animation-duration: 3s;
animation-timing-function: ease-in-out;
animation-iteration-count: infinite;
}
</style>
</head>
<body>
<h1>Animated Box with Duration</h1>
<div class="animated-box"></div>
</body>
</html>
🖥️ Browser Compatibility
The animation-duration
property is widely supported across modern browsers. It works in the latest versions of Chrome, Firefox, Safari, Edge, and Opera. However, for older versions or specific legacy browsers, always test your animations to ensure they work as expected.
🎉 Conclusion
The animation-duration
property is a fundamental part of creating animations in CSS.
By defining how long an animation should run, you can control the pacing and fluidity of visual effects on your website. Experiment with different durations to achieve the desired visual impact and enhance user experience with engaging animations.
👨💻 Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (CSS animation-duration Property), please comment here. I will help you immediately.