How to create smooth CSS animations with @keyframes
· Category: HTML & CSS
Short answer
@keyframes define CSS animations by specifying styles at various points during the animation sequence, which are then applied using the animation property.
Steps
- Define a
@keyframesrule with a name and percentage stops. - Apply it with
animation: name duration timing-function. - Use
animation-fill-modeto persist the end state.
Example
@keyframes slideIn {
from { transform: translateX(-100%); opacity: 0; }
to { transform: translateX(0); opacity: 1; }
}
.panel {
animation: slideIn 0.5s ease-out forwards;
}
Tips
- Use
animation-delayto stagger multiple elements. - Prefer
transformandopacityfor performant animations. - Consider
prefers-reduced-motionfor accessibility.