CSS animations and transitions
CSS animations and transitions are powerful tools for creating dynamic, interactive web experiences. They allow you to animate changes to CSS properties and enhance user interfaces with smooth visual effects
CSS Transitions
CSS transitions enable you to change property values smoothly over a specified duration. They are ideal for simple effects like changing colors or sizes when a user interacts with an element.
Basic Syntax
selector {
transition: property duration timing-function delay;
}
- property: The CSS property you want to animate (e.g.,
background-color
,width
,opacity
). - duration: How long the transition should take (e.g.,
0.5s
for 0.5 seconds). - timing-function: The speed curve of the transition (e.g.,
ease
,linear
,ease-in
,ease-out
,ease-in-out
). - delay: How long to wait before starting the transition (e.g.,
0s
).
Example
.box {
width: 100px;
height: 100px;
background-color: blue;
transition: width 0.5s ease, background-color 0.5s ease;
}
.box:hover {
width: 200px;
background-color: red;
}
In this example, when you hover over the .box
element, its width and background color change smoothly over 0.5 seconds.
CSS Animations
CSS animations provide more control than transitions and allow for complex sequences of changes. They involve defining keyframes and applying them to an element.
Basic Syntax
Define Keyframes
@keyframes animation-name { from { /* Initial state */ } to { /* Final state */ } }
You can also use multiple keyframes to create more complex animations:
@keyframes example { 0% { transform: scale(1); } 50% { transform: scale(1.5); } 100% { transform: scale(1); } }
Apply Animation to an Element
selector { animation: animation-name duration timing-function delay iteration-count direction fill-mode; }
- animation-name: The name of the keyframes animation.
- duration: How long the animation should take (e.g.,
2s
for 2 seconds). - timing-function: The speed curve of the animation (e.g.,
ease
,linear
,ease-in
,ease-out
,ease-in-out
). - delay: How long to wait before starting the animation (e.g.,
0s
). - iteration-count: The number of times the animation should repeat (e.g.,
infinite
for an infinite loop). - direction: The direction of the animation (e.g.,
normal
,reverse
,alternate
,alternate-reverse
). - fill-mode: How the animation applies styles before and after it runs (e.g.,
none
,forwards
,backwards
,both
).
Example
.box {
width: 100px;
height: 100px;
background-color: blue;
animation: example 2s ease-in-out infinite;
}
@keyframes example {
0% { transform: scale(1); }
50% { transform: scale(1.5); }
100% { transform: scale(1); }
}
In this example, the .box
element scales up and down infinitely over 2 seconds with an ease-in-out timing function.
Key Differences
- Transitions: Smoothly animate property changes in response to user interactions (e.g., hover, focus). They require a trigger event to start.
- Animations: Allow for complex sequences of keyframes and can run continuously or in response to a trigger. They are more flexible and can animate multiple properties simultaneously.
Best Practices
- Performance: Use CSS animations and transitions sparingly to avoid performance issues, especially on mobile devices. Prefer animations that leverage hardware acceleration (e.g.,
transform
andopacity
). - Accessibility: Ensure animations don’t cause issues for users with motion sensitivity. Provide options to reduce or disable animations if needed.
- Fallbacks: Provide alternative styling for browsers that do not support animations or transitions.