Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

CSS Properties

CSS animation-fill-mode Property

Posted in CSS Tutorial
Updated on Oct 01, 2024
By Mari Selvan
👁️ 11 - Views
⏳ 4 mins
💬 1 Comment
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.

Syntax
Copied
Copy To Clipboard
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

ValueDescription
noneThe animation has no effect on the element before the animation starts or after it ends.
forwardsThe element retains the styles defined in the final keyframe after the animation ends.
backwardsThe element applies the styles from the first keyframe before the animation starts.
bothThe 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.

index.html
Copied
Copy To Clipboard
<!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:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

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

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy