Transitions and Animations in CSS

Table of Contents

  1. Introduction to CSS Transitions
  2. Transition Properties and Examples
  3. Introduction to CSS Animations
  4. Keyframes in CSS Animations
  5. Animation Properties and Examples
  6. Transition vs Animation: Key Differences
  7. Advanced Animation Techniques
  8. Browser Support and Compatibility
  9. Best Practices for Using Transitions and Animations
  10. Conclusion

1. Introduction to CSS Transitions

CSS transitions allow you to change property values smoothly over a specified duration, which helps in creating interactive and visually appealing web pages. A transition occurs when a CSS property changes from one value to another, such as hovering over a button to change its color or a background fading in over time.

Key Concept

A transition requires two states:

  • Initial State (the state before the transition).
  • Final State (the state after the transition is triggered).

To implement transitions, you must specify:

  • Which property you want to transition.
  • The duration of the transition.
  • Optionally, a timing function and delay.

2. Transition Properties and Examples

Basic Syntax for Transitions:

selector {
transition: property duration timing-function delay;
}
  • property: The CSS property to which the transition will be applied.
  • duration: How long the transition should take.
  • timing-function: Defines how the intermediate states are calculated.
  • delay: How long to wait before starting the transition.

Example 1: Changing Background Color on Hover

button {
background-color: blue;
color: white;
padding: 10px 20px;
border: none;
cursor: pointer;
transition: background-color 0.3s ease-in-out;
}

button:hover {
background-color: green;
}

In the example above, the background-color smoothly transitions between blue and green over 0.3 seconds when the user hovers over the button.

Example 2: Scaling Element on Hover

div {
width: 200px;
height: 200px;
background-color: red;
transition: transform 0.3s ease;
}

div:hover {
transform: scale(1.2);
}

When the user hovers over the div, it grows 20% larger over 0.3 seconds.


3. Introduction to CSS Animations

CSS animations allow you to create more complex and dynamic effects compared to transitions. Unlike transitions, which can only occur when a property changes, animations enable continuous movement and transformations over time.

CSS animations consist of two parts:

  1. @keyframes rule: Defines the animation sequence.
  2. Animation properties: Apply the animation to elements.

4. Keyframes in CSS Animations

The @keyframes rule allows you to define intermediate steps in an animation sequence. It can define the start, end, and any intermediate states of an animation.

Basic Syntax for @keyframes:

@keyframes animation-name {
0% {
/* Initial state */
}
50% {
/* Midpoint state */
}
100% {
/* Final state */
}
}

Example 1: Bouncing Ball Animation

@keyframes bounce {
0% {
transform: translateY(0);
}
50% {
transform: translateY(-50px);
}
100% {
transform: translateY(0);
}
}

div {
width: 50px;
height: 50px;
background-color: red;
animation: bounce 1s infinite;
}

In this example, the div will bounce up and down every 1 second in a loop. The animation is defined with @keyframes and is applied using the animation property.


5. Animation Properties and Examples

Basic Syntax for Animations:

selector {
animation: animation-name duration timing-function delay iteration-count direction;
}
  • animation-name: The name of the animation (defined in the @keyframes rule).
  • duration: How long the animation should run.
  • timing-function: Defines the speed curve of the animation.
  • delay: Delay before the animation starts.
  • iteration-count: Number of times the animation should play (e.g., infinite for continuous looping).
  • direction: Defines the direction of the animation, such as normal, reverse, or alternate.

Example 2: Fading In and Out

@keyframes fade {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}

div {
width: 200px;
height: 200px;
background-color: green;
animation: fade 2s ease-in-out infinite alternate;
}

The div element fades in and out every 2 seconds, infinitely alternating between fully transparent and fully visible states.


6. Transition vs Animation: Key Differences

FeatureTransitionAnimation
TriggerOccurs on state change (e.g., hover)Runs continuously or on trigger
StatesTwo states (before and after change)Multiple intermediate states
ComplexitySimple and immediate effectsComplex, allows for more control
Use CaseSimple effects like hover statesDynamic and continuous effects

7. Advanced Animation Techniques

a. CSS Animation Timing Functions

The timing function controls the pacing of an animation. It defines how the animation progresses over time. Some common timing functions are:

  • ease (default): Starts slow, speeds up, then slows down.
  • linear: Constant speed throughout the animation.
  • ease-in: Starts slow and accelerates.
  • ease-out: Starts fast and decelerates.
  • cubic-bezier(): Allows custom control over the animation’s timing.

b. Chaining Animations

You can apply multiple animations to the same element by separating them with commas:

@keyframes rotate {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}

div {
width: 100px;
height: 100px;
background-color: blue;
animation: rotate 2s infinite, fade 3s infinite;
}

This example combines both a rotation and fading animation on the same element.

c. Using animation-delay for Staggered Animations

@keyframes move {
0% {
transform: translateX(0);
}
100% {
transform: translateX(100px);
}
}

div:nth-child(1) {
animation: move 2s ease-in-out 0s;
}

div:nth-child(2) {
animation: move 2s ease-in-out 0.5s;
}

div:nth-child(3) {
animation: move 2s ease-in-out 1s;
}

By adjusting the animation-delay property, you can stagger the start times of multiple animations for a sequence.


8. Browser Support and Compatibility

Most modern browsers fully support CSS transitions and animations. However, older versions of Internet Explorer (IE 9 and below) and other legacy browsers may not support certain properties. Always refer to Can I use for detailed browser compatibility information.

FeatureChromeFirefoxSafariEdgeIE 11
transition✅✅✅✅✅
@keyframes✅✅✅✅✅
animation✅✅✅✅✅

9. Best Practices for Using Transitions and Animations

  • Use sparingly: While animations can make websites feel more interactive, too many animations can lead to performance issues and make the page feel cluttered.
  • Performance: For smoother animations, use transform and opacity properties instead of properties like width, height, or top that trigger reflows.
  • Accessibility: Ensure that animations do not trigger unintended effects for users with sensitivities. Use the prefers-reduced-motion media query to respect user preferences.
@media (prefers-reduced-motion: reduce) {
* {
animation: none;
transition: none;
}
}

10. Conclusion

CSS transitions and animations are powerful tools for enhancing user experience and interactivity on your website. Transitions allow for simple, smooth state changes, while animations offer a wider range of complex visual effects. Understanding the differences, use cases, and performance considerations for both can help you create rich, dynamic web designs that engage users.