CSS Properties
CSS animation-fill-mode Property
Photo Credit to CodeToFun
🙋 Introduction
The animation-fill-mode
property in CSS defines how a CSS animation should apply styles to its target before and after its execution.
By using this property, you can control whether the element retains the styles applied by the animation at the start, the end, or throughout the duration of the animation.
💡 Syntax
The syntax for the animation-fill-mode
property is straightforward. It can be applied to any element that has an animation applied to it.
element {
animation-fill-mode: value;
}
Here, value can be one of the predefined keywords that determine how the animation's styles affect the element outside its active period.
🎛️ Default Value
The default value of the animation-fill-mode
property is none, which means the animation will not affect the styles of the element before it starts or after it ends.
🏠 Property Values
Value | Description |
---|---|
none | The animation has no effect on the element before the animation starts or after it ends. |
forwards | The element retains the styles defined in the final keyframe after the animation ends. |
backwards | The element applies the styles from the first keyframe before the animation starts. |
both | The element applies the styles from the first keyframe before the animation starts and retains the styles from the final keyframe after the animation ends. |
📄 Example
In this example, we’ll create a simple animation that moves an element across the screen. We'll use the animation-fill-mode
property to make sure the element stays in its final position after the animation ends.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS animation-fill-mode Example</title>
<style>
.move {
width: 100px;
height: 100px;
background-color: red;
position: relative;
animation: moveRight 2s;
animation-fill-mode: forwards;
}
@keyframes moveRight {
from { left: 0; }
to { left: 200px; }
}
</style>
</head>
<body>
<h1>Box with animation-fill-mode Property</h1>
<div class="move"></div>
</body>
</html>
In this example:
- The box will move from left to right over 2 seconds.
- Thanks to the animation-fill-mode: forwards; property, the box will remain at its final position after the animation completes.
🖥️ Browser Compatibility
The animation-fill-mode
property is widely supported across all modern browsers, including Chrome, Firefox, Safari, Edge, and Opera. However, it's advisable to check your animations across different browsers to ensure consistent behavior.
🎉 Conclusion
The animation-fill-mode
property is a useful tool for controlling how animations affect the styles of elements before and after they run.
By understanding and utilizing this property, you can create more polished and predictable animations in your web projects. Experiment with the different values to see how they impact your animations and choose the one that best fits your design needs.
👨💻 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-fill-mode Property), please comment here. I will help you immediately.