The animation-fill-mode
property in CSS is used to specify how a CSS animation should apply styles to its target before and after its execution. This property is essential for controlling the end state of an animation, ensuring that the final visual state of an element matches the design intent even after the animation has completed.
Controlling the animation end state with animation-fill-mode
can enhance the user experience by maintaining a consistent appearance, preventing elements from abruptly reverting to their initial state. This article will explore the basics and advanced techniques of using animation-fill-mode
, providing practical examples and applications to help you master this important CSS property.
Understanding animation-fill-mode
The animation-fill-mode
property defines the styles that an element should retain before an animation starts and after it ends. It has four possible values: none
, forwards
, backwards
, and both
.
Syntax and Values
The syntax for animation-fill-mode
is as follows:
animation-fill-mode: value;
none
: The default value. The element does not retain any styles from the animation.forwards
: The element retains the styles defined in the last keyframe after the animation ends.backwards
: The element applies the styles defined in the first keyframe before the animation starts.both
: The element applies the styles from both the first and last keyframes before and after the animation.
For example:
animation-fill-mode: forwards; /* Retains the end state */
animation-fill-mode: backwards; /* Applies the start state before animation */
Basic Usage of animation-fill-mode
Let’s start with a simple example where we use the animation-fill-mode
property to control the end state of an animation.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Basic Animation Fill Mode</title>
<style>
.box {
width: 100px;
height: 100px;
background-color: blue;
animation-name: changeColor;
animation-duration: 2s;
animation-fill-mode: forwards;
}
@keyframes changeColor {
from {background-color: blue;}
to {background-color: red;}
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
In this example, the .box
element changes its background color from blue to red over a period of 2 seconds. The animation-fill-mode: forwards;
property ensures that the element retains the red background color after the animation ends, preventing it from reverting to blue.
Advanced Techniques with animation-fill-mode
You can combine animation-fill-mode
with other animation properties like animation-delay
, animation-iteration-count
, and animation-timing-function
to create more complex animations.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Advanced Animation Fill Mode</title>
<style>
.circle {
width: 50px;
height: 50px;
background-color: coral;
border-radius: 50%;
animation-name: moveAndFade;
animation-duration: 3s;
animation-iteration-count: infinite;
animation-timing-function: ease-in-out;
animation-fill-mode: both;
}
@keyframes moveAndFade {
from {transform: translateY(0); opacity: 1;}
to {transform: translateY(-100px); opacity: 0;}
}
</style>
</head>
<body>
<div class="circle"></div>
</body>
</html>
In this example, the .circle
element moves upwards and fades out over a period of 3 seconds. The animation-fill-mode: both;
property ensures that the element starts with the initial state and retains the final state after each animation iteration, creating a seamless loop.
Practical Applications
One practical application of animation-fill-mode
is creating a fade-in effect where an element gradually appears and remains visible 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>Fade-In Effect</title>
<style>
.fade-in {
width: 200px;
height: 200px;
background-color: green;
opacity: 0;
animation-name: fadeIn;
animation-duration: 3s;
animation-fill-mode: forwards;
}
@keyframes fadeIn {
from {opacity: 0;}
to {opacity: 1;}
}
</style>
</head>
<body>
<div class="fade-in"></div>
</body>
</html>
In this example, the .fade-in
element gradually becomes fully opaque over 3 seconds. The animation-fill-mode: forwards;
property ensures that the element remains fully opaque after the animation ends, creating a lasting fade-in effect.
Conclusion
The animation-fill-mode
property is crucial for controlling the end state of animations in CSS. By specifying how an element should retain styles before and after an animation, you can ensure a consistent and visually appealing design. Whether you want an element to maintain its final state or apply initial styles before starting, understanding how to use animation-fill-mode
effectively is essential for creating seamless animations.
Experiment with different animation-fill-mode
values and combine them with other animation properties to see how they affect the overall behavior of your animations. Practice creating various animations to understand the impact of animation-fill-mode
on user experience.
Additional Resources
To further your understanding of CSS animations and fill mode, explore these resources:
By utilizing these resources and practicing regularly, you can master the art of controlling animation end states, making your web designs more engaging and interactive.