CSS Keyframes Definition and Basic Properties
CSS Animations Keyframes and Properties
Introduction to CSS Animations
CSS animations are a powerful tool for adding motion and transition effects to web content. At their core, animations are defined using keyframes, which are predefined sequences of styles that dictate how an element changes over time. These keyframes are then controlled by animation properties, which determine how the animation is applied, paused, or restarted.
Keyframes
Keyframes define the sequence of styles that an element adopts during an animation. They are specified using the @keyframes rule, which allows developers to define the visual changes an element undergoes.
Syntax and Structure
@keyframes example {
0% {
background-color: red;
transform: scale(1);
}
50% {
background-color: blue;
transform: scale(1.2);
}
100% {
background-color: green;
transform: scale(1);
}
}
This example defines a simple animation where an element transitions from red to blue to green, scaling up and down in between.
Keyframe Timing
Keyframes are defined with timing functions to control the pacing of the animation. Common values include linear (constant speed), ease (acceleration and deceleration), and ease-in-out (acceleration, peak, deceleration).
Keyframe Positioning
Keyframes are specified using percentages (e.g., 0%, 50%, 100%) to determine their position in the animation timeline. The first keyframe (0%) is the starting point, while the last (100%) is the ending point.
Properties
Properties are the attributes that define how an animation behaves. These include:
- animation-name: Specifies the name of the keyframe animation.
- animation-duration: Defines the total duration of the animation.
- animation-timing-function: Controls the speed curve of the animation.
- animation-delay: Adds a delay before the animation starts.
- animation-iteration-count: Determines how many times the animation should play.
- animation-direction: Specifies the direction of the animation (e.g.,
normal,reverse,alternate). - animation-play-state: Controls whether the animation is playing or paused.
Animation Properties
Animation properties are the properties that control the animation's behavior. They are specified using the animation- prefix and include:
- animation-name: Specifies the name of the keyframe animation.
- animation-duration: Defines the total duration of the animation.
- animation-timing-function: Controls the speed curve of the animation.
- animation-delay: Adds a delay before the animation starts.
- animation-iteration-count: Determines how many times the animation should play.
- animation-direction: Specifies the direction of the animation (e.g.,
normal,reverse,alternate). - animation-play-state: Controls whether the animation is playing or paused.
Examples
Example 1: Bouncing Ball
A simple animation where a ball bounces up and down:
.ball {
width: 50px;
height: 50px;
background-color: yellow;
animation-name: bounce;
animation-duration: 1s;
animation-timing-function: ease;
animation-iteration-count: infinite;
}
@keyframes bounce {
0% {
transform: translateY(0);
}
50% {
transform: translateY(-10px);
}
100% {
transform: translateY(0);
}
}
This example creates a bouncing ball animation using keyframes and animation properties.
Example 2: Fade In
An animation where an element fades in over 2 seconds:
.fade-in {
opacity: 0;
animation-name: fadeIn;
animation-duration: 2s;
animation-timing-function: linear;
}
@keyframes fadeIn {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}
The animation-play-state can be used to pause or resume the animation at any point.
Best Practices
- Use
animation-iteration-countto control the number of times an animation repeats. - Combine
animation-timing-functionwithanimation-durationto create smooth transitions. - Use
animation-delayto add pauses or delays to the animation. - Test animations using browser developer tools to ensure they behave as expected.
By mastering keyframes and properties, developers can create dynamic and engaging user experiences with minimal code.