@keyframes
Defines the stages of an animation, known as keyframes.
- Initial value
not applicable (at-rule)- Inherited
- No
- Applies to
- a rule in a stylesheet, used by animation-name
- JavaScript
element.animate([{ opacity: 0 }, { opacity: 1 }], 500)
Example
#@keyframes shake {
0%, 100% { translate: 0; }
20%, 60% { translate: -6px; }
40%, 80% { translate: 6px; }
}
.input.error {
animation: shake 0.4s;
}<style>
@keyframes shake { 0%, 100% { translate: 0; } 20%, 60% { translate: -6px; } 40%, 80% { translate: 6px; } }
.shake { animation: shake .4s; border: 1px solid #f43f5e; }
</style>
<input class="shake" value="bad@address" style="padding: 8px 10px; border-radius: 8px; background: #111;">
<script>
const field = document.querySelector('.shake')
setInterval(() => { field.style.animation = 'none'; field.offsetHeight; field.style.animation = '' }, 2000)
</script>Definition and usage
#The @keyframes rule describes how an element should look at successive moments of an animation. You give it a name and then list the keyframes: from (0%) and to (100%), or any percentages in between.
The browser calculates the intermediate states between keyframes on its own. If you leave out from or to, it uses the element’s current styles as the missing keyframe, which lets you create animations “from the current state”.
You attach the rule to an element through animation-name or the animation shorthand. Keyframes can change many properties at once, but transform and opacity run the most smoothly.
Syntax
#@keyframes name {
from { … }
50% { … }
to { … }
}Values
#- Two keyframes: the start and the end of the movement.
percentages
Three keyframes: halfway through, the element is in the upper right corner.- Movement, a color change, and rotation at once.
from the current state
No starting keyframe: the animation begins from the element’s normal styles.
Values in practice
#from / to
@keyframes frames {
from { transform: translateX(0); }
to { transform: translateX(180px); }
}0% / 50% / 100%
@keyframes frames {
0%, 100% { transform: translate(0, 40px); }
50% { transform: translate(180px, 0); }
}multiple properties
@keyframes frames {
50% { transform: translateX(180px) rotate(180deg); background: #f43f5e; border-radius: 50%; }
}no from
@keyframes frames {
to { transform: translateX(180px) scale(0.6); opacity: 0.4; }
}Browser support
#Widely available
Works in all modern browsers, phones included. You can use it without worry.
- Chrome
- Edge
- Firefox
- Safari
Good practices
#- Name keyframes after the effect, for example
fade-in,shake,slide-up. They are easy to reuse many times. - You can combine identical keyframes with a comma, e.g.
0%, 100% { … }, when the start and end look the same.
Related entries
#- animationA shorthand that sets all the parameters of an animation based on @keyframes.
- animation-nameSpecifies which @keyframes an element plays.
- animation-timing-functionSets the pacing curve of the animation between keyframes.
- transformMoves, rotates, scales, or skews an element without affecting the page layout.
- @propertyRegisters a CSS variable with a type, an initial value, and an inheritance rule. Such a variable can be animated smoothly, e.g. a gradient angle.
Practise it in lessons
#Spotted a mistake or missing an example? Write to us.