CSS animation-name
The animation-name
property in CSS specifies the name of one or more @keyframes
animations that should be applied to an element. By defining the animation name, you link the animation’s keyframes to an element to create a visual effect.
Syntax of animation-name
</>
Copy
animation-name: keyframename | none | initial | inherit;
Values
Value | Description |
---|---|
keyframename | Specifies the name of the @keyframes animation to apply to the element. |
none | No animation is applied to the element. |
initial | Sets the property to its default value (none ). |
inherit | Inherits the value from the parent element. |
Default Value
none
Examples for animation-name
Using the CSS animation-name Property
The following examples demonstrate how the animation-name
property can be used to apply specific animations to elements.
</>
Copy
/* Apply the keyframes animation named "slide" */
.element {
animation-name: slide;
}
/* Disable any animation */
.element {
animation-name: none;
}
/* Inherit the animation name from the parent element */
.element {
animation-name: inherit;
}
Example for Applying animation-name
The following example demonstrates the use of animation-name
to apply a “slide” animation to one element and no animation to another element.
index.html
</>
Copy
<!DOCTYPE html>
<html lang="en">
<head>
<style>
@keyframes slide {
from {
transform: translateX(0);
}
to {
transform: translateX(200px);
}
}
.animated {
animation-name: slide;
animation-duration: 2s;
animation-timing-function: ease-in-out;
background-color: green;
margin: 10px;
padding: 10px;
}
.no-animation {
animation-name: none;
background-color: red;
margin: 10px;
padding: 10px;
}
div {
width: 200px;
height: 100px;
line-height: 100px;
color: white;
font-family: sans-serif;
}
</style>
</head>
<body>
<h1>CSS animation-name Example</h1>
<div class="animated">Animated Box</div>
<div class="no-animation">No Animation</div>
</body>
</html>
Animated Box (.animated
)
- The class
.animated
uses theanimation-name
property set toslide
. - The
slide
animation is defined in the@keyframes
block. It moves the element horizontally fromtranslateX(0)
totranslateX(200px)
. - The
animation-duration
is set to2s
, meaning the animation completes in 2 seconds. - The
animation-timing-function
is set toease-in-out
, which creates a smooth transition by starting and ending the animation slowly. - As a result, the green box moves from its initial position to 200px to the right over 2 seconds in a smooth motion.
No Animation Box (.no-animation
)
- The class
.no-animation
uses theanimation-name
property set tonone
. - The
animation-name: none
disables the animation, meaning no@keyframes
animation is applied to this element. - Even though other animation-related properties (like
animation-duration
) could be defined, they are ignored becauseanimation-name: none
prevents any animation from running. - As a result, the red box remains static and does not move or animate.
Browser Support
The animation-name
property is supported in most modern browsers. Below is a compatibility table:
Browser | Version |
---|---|
Chrome | 43 |
Edge | 10 |
Firefox | 16 |
Safari | 9 |
Opera | 30 |