CSS Animations and Transitions
CSS Animations and Transitions
CSS animations allow you to create smooth transitions between different states of an element. They
provide an easy way to add movement and interactivity to web pages. Animations can be defined
using keyframes, which specify the styles at various points in the animation timeline.
Syntax of @keyframes
The @keyframes rule defines the animation. It specifies the styles for different points in the
animation:
@keyframes example {
0% { background-color: red; }
Applying Animations
Example:
div {
animation-name: example;
animation-duration: 4s;
}
CSS Transitions
CSS transitions allow changes in CSS properties to occur smoothly over a duration. This is useful
Example:
button {
- Animations are more complex and can have multiple keyframes, whereas transitions only define a
- Animations can be looped or run indefinitely, while transitions are one-time changes.
- Animations require the @keyframes rule, while transitions can be applied directly.
Practical Examples
button:hover {
background-color: blue;
color: white;
transition: background-color 0.5s ease;
2. Spinning Animation:
@keyframes spin {
to { transform: rotate(360deg); }
div {
Conclusion
CSS animations and transitions are powerful tools for creating dynamic and engaging web pages.