How to Fix CSS Animation Not Working: Simple Solutions
@keyframes rule is missing or incorrectly named, the animation properties are not applied properly, or the element is missing required styles like display or position. Ensure your animation-name matches the @keyframes name exactly and that the element is visible and styled to animate.Why This Happens
CSS animations fail when the animation name does not match the @keyframes name, or when the element lacks styles that allow animation to show. Sometimes, missing display or position properties cause the animation to not appear. Also, forgetting to include the animation-duration means the animation won't run.
div {
animation-name: slidein;
animation-duration: 2s;
}
/* Missing @keyframes or wrong name */The Fix
Make sure the @keyframes rule is defined with the exact name used in animation-name. Add animation-duration to specify how long the animation runs. Also, ensure the element has styles like display: block or position: relative if needed. This makes the animation visible and functional.
div {
display: block;
position: relative;
animation-name: slidein;
animation-duration: 2s;
animation-fill-mode: forwards;
}
@keyframes slidein {
from {
transform: translateX(0);
}
to {
transform: translateX(100px);
}
}Prevention
Always double-check that your animation-name matches the @keyframes name exactly, including case sensitivity. Use developer tools to inspect if the animation properties are applied. Set a clear animation-duration and ensure the element is visible and styled properly. Use CSS validators or linters to catch syntax errors early.
Related Errors
Other common issues include animations not running because of animation-play-state set to paused, or conflicts with other CSS properties like overflow: hidden clipping the animation. Also, animations on inline elements may not work as expected without changing their display property.
Key Takeaways
animation-name exactly matches the @keyframes name.animation-duration to make the animation run.display and position for visibility.