Introduction to CSS Animations
CSS Animations allow you to create complex animation sequences without relying on JavaScript or Flash. They are defined using the @keyframes rule and applied to elements using the animation property.
Core Concepts
@keyframes: Defines the stages of an animation.animation: A shorthand property to set various animation properties like name, duration, timing function, delay, iteration count, direction, fill mode, and play state.
@keyframes Example
This defines an animation named "pulse" that scales an element up and down.
@keyframes pulse {
0% {
transform: scale(1);
box-shadow: 0 0 0 0 rgba(0, 168, 168, 0.7);
}
70% {
transform: scale(1.05);
box-shadow: 0 0 0 15px rgba(0, 168, 168, 0);
}
100% {
transform: scale(1);
box-shadow: 0 0 0 0 rgba(0, 168, 168, 0);
}
}
Applying the Animation
Here's how to apply the "pulse" animation to an element:
.animated-box {
width: 150px;
height: 150px;
background-color: #00a8a8; /* accent-color */
border-radius: 10px;
display: flex;
justify-content: center;
align-items: center;
font-size: 1.5rem;
font-weight: bold;
color: #f0f0f0; /* light-text */
animation: pulse 2s infinite ease-in-out;
}
CSS Transitions vs. Animations
While similar, transitions and animations serve different purposes:
- Transitions: Used for animating property changes when an element's state changes (e.g., on hover, focus, or class change). They are simpler, reacting to state changes.
- Animations: Provide more control over complex, multi-step sequences that can run independently of state changes, defined by
@keyframes.
Transition Example (Hover Effect)
This box smoothly changes color and shape when you hover over it.
.transition-example {
width: 100px;
height: 100px;
background-color: #0078d4; /* primary-color */
margin: 20px auto;
transition: background-color 0.5s ease-in-out, transform 0.5s ease-in-out;
}
.transition-example:hover {
background-color: #00a8a8; /* accent-color */
transform: rotate(45deg) scale(1.2);
}
Key Animation Properties
Understanding the various properties that control animations:
animation-name: Specifies the name of the@keyframesrule to apply.animation-duration: Sets how long the animation should take to complete.animation-timing-function: Defines the speed curve (e.g.,ease,linear,ease-in-out).animation-delay: Sets a delay before the animation starts.animation-iteration-count: Specifies how many times the animation should repeat (e.g.,infinite).animation-direction: Determines whether the animation should play forwards, backwards, or alternate.animation-fill-mode: Defines which styles are applied to the element before and after the animation (e.g.,forwards,backwards,both).animation-play-state: Allows pausing and resuming animations (e.g.,paused,running).
animation-name and animation-fill-mode Example
This box animates in from the right and stays visible.
.animation-name-example {
width: 100px;
height: 100px;
background-color: #00a8a8; /* accent-color */
margin: 20px auto;
animation: slideInRight 1.5s forwards;
}
@keyframes slideInRight {
from { transform: translateX(100%); opacity: 0; }
to { transform: translateX(0); opacity: 1; }
}
animation-iteration-count and animation-direction Example
This circular element smoothly changes its gradient colors and alternates direction.
.keyframe-example {
width: 120px;
height: 120px;
background: linear-gradient(45deg, #0078d4, #00a8a8);
border-radius: 50%;
margin: 20px auto;
animation: colorChange 4s infinite alternate;
}
@keyframes colorChange {
0% { background: linear-gradient(45deg, #0078d4, #00a8a8); }
50% { background: linear-gradient(45deg, #00a8a8, #0078d4); }
100% { background: linear-gradient(45deg, #0078d4, #00a8a8); }
}
Advanced Techniques & Best Practices
- Combining Properties: Use the shorthand
animationproperty for cleaner code. - Performance: Animate properties that trigger fewer layout recalculations (e.g.,
transformandopacity) for better performance. Avoid animating properties likewidth,height,margin, ortop/leftif possible. - Accessibility: Provide options to disable animations for users sensitive to motion (e.g., using
@media (prefers-reduced-motion: reduce)).
Explore further resources on MDN Web Docs for comprehensive details on CSS animations and transitions.