Web Development
HTML Course
CSS Course
JavaScript Course
PHP Course
Python Course
SQL Course
SEO Course

CSS Transitions and Animations

This tutorial covers everything you need to know about transitions and animations in CSS — from basic concepts to advanced techniques. It is structured progressively, so you can learn step by step, even if starting from scratch.

CSS Transitions – Complete Guide

CSS transitions allow smooth changes of style property values without using JavaScript. Instead of a change being instantaneous, it unfolds gradually, providing a more fluid and pleasant visual experience.

What are transitions

Transitions are visual effects that occur when a CSS property changes. They can be applied on hover, focus, activation, or any other state change.

Involved properties

Simple transition example

The transition below changes the background color on hover, using the longhand form:

🔧 Live Example – Color Transition

transition-property

Defines which CSS property will be animated. It can take:

🔧 Live Example – transition-property

transition-duration

Defines how long the transition takes. Accepted values:

🔧 Live Example – transition-duration

transition-timing-function

Defines how the transition progresses over time. Controls the speed curve of the animation. Accepted values:

🔧 Live Example – transition-timing-function

transition-delay

Defines the delay before the transition starts. Accepted values:

🔧 Live Example – transition-delay

Shorthand form – transition

Once you understand how the individual properties work (transition-property, transition-duration, transition-timing-function, transition-delay), you can use the shorthand transition to combine them into a single line.

General structure:

transition: [property] [duration] [timing-function] [delay];

Example:

transition: background-color 1s ease 0.5s;

This line is equivalent to:

transition-property: background-color;
transition-duration: 1s;
transition-timing-function: ease;
transition-delay: 0.5s;
🔧 Live Example – Shorthand form

Example – Multiple transitions with shorthand

You can animate multiple properties at the same time using the shorthand form:

transition: background-color 0.5s ease, transform 0.3s ease-in;

This example animates both the background color and transformation (scaling) on hover.

🔧 Live Example – Multiple transitions

Introduction to CSS Animations

CSS animations allow detailed control over visual changes using @keyframes rules and dedicated properties. Unlike transitions, animations do not rely on direct interaction (hover, click); they can trigger automatically, repeat, reverse, or synchronize.

Difference between transitions and animations

🔧 Live Example – Transition vs Animation

Properties involved in CSS animations

🔧 Live Example – Complete Animation

Simple animation example – pulsing

This example creates a "pulse" animation that repeatedly enlarges and shrinks an element.

🔧 Live Example – Pulsing

Simple animation example – fade in

This example makes an element appear gradually, increasing opacity from 0 to 1.

🔧 Live Example – Fade In

What are timing functions?

Timing functions define the speed curve of an animation. They determine how the movement is distributed over the duration of the animation. In other words, they control the pace at which an element transforms over time.

For example, an animation can start slowly and accelerate towards the end, or vice versa. These functions provide fine control over how the user perceives the motion.

Live Example – Timing Functions
Live Example – Timing Function steps()
Live Example – Timing Function cubic-bezier()

Multiple animations and advanced control

CSS allows applying multiple animations simultaneously to the same element. Each animation can have its own name, duration, timing function, and behavior. By combining them, you can create complex and dynamic effects.

Additionally, properties like animation-delay, animation-fill-mode, and animation-direction offer fine control over how animations behave before, during, and after execution.

Live example – Multiple animations

Example – Shorthand animation

You can apply a complete animation in a single line using the shorthand form:

animation: bounce 1s ease-in-out infinite alternate;

This example makes the element bounce vertically infinitely, with back-and-forth motion.

🔧 Live example – Shorthand animation

Advanced animation control

CSS is great for simple effects, but if you want precise control over animations, JavaScript steps in. You can detect when an animation ends, synchronize them with each other, or trigger them conditionally.

1. JavaScript events: animationend, transitionend

These events are automatically triggered when an animation or transition finishes. You can use them to launch other actions or change styles.

🔧 Live example – animationend

2. Synchronization and chaining

You can trigger an animation only after another one has finished, using animationend or setTimeout. This allows you to create chained effects.

🔧 Live example – chaining

3. Conditional animations

You can start an animation only if a condition is met – for example, if an element has a class or if the user performs an action.

🔧 Live example – conditional animation

Practical applications

Now that you've mastered animations and their control, it's time to apply them in the interface. Below are real examples, used in modern websites and applications.

1. Hover effects

A simple yet elegant effect that provides visual feedback on interaction. In this example, the card slightly lifts and changes its color on hover.

transition: transform 0.3s ease, background-color 0.3s ease;
🔧 Live example – Hover effect

2. Animated loader

An animated loader indicates progress or waiting. Below, a circle pulses continuously to suggest activity.

🔧 Live example – Animated loader

3. Slide-in menu

A menu that slides in from the left, and the open button disappears when the menu is active. When closed, the button reappears.

🔧 Live example – Slide-in menu

4. Animated cards

Cards can have effects on appearance or interaction. In the example below, they animate with a fade-in on load.

🔧 Live example – Animated cards

5. Scroll-based animations (with JS)

Elements can appear with effects when they enter the viewport.

Elements animate automatically on scroll, using IntersectionObserver to detect the moment they enter the viewport.

🔧 Live example – Scroll-based animation with IntersectionObserver

Optimization and performance

Animations can enhance user experience, but if not well designed, they can impact performance. In this chapter, we learn what to animate, how to use the GPU, and which practices to apply for a smooth UI.

1. Which properties are expensive

Not all CSS properties are equal when it comes to performance. Some trigger complex recalculations and reflows in the browser.

❌ Expensive: width, height, top, left, margin  
✅ Recommended: transform, opacity
🔧 Live example – Expensive vs efficient properties

2. GPU vs CPU rendering

Properties like transform and opacity are GPU-accelerated, which makes them ideal for smooth animations. Other properties are processed by the CPU and can cause stuttering.

animation: transform 1s ease; /* GPU-friendly */  
animation: width 1s ease;     /* CPU-intensive */
🔧 Live example – GPU vs CPU rendering

3. Best practices

To keep animations fast and smooth, follow these recommendations:

🔧 Live example – prefers-reduced-motion

Animations are not just decoration — they are part of the rhythm and personality of an interface. But without optimization, beauty becomes a burden.

Choosing the right properties, using the GPU, and following best practices are not optional — they are essential.

When performance is smooth, the user doesn't notice the animation — they feel it. And that's what makes the difference between a site that looks good and one that feels alive.


🧠 Quiz – CSS Transitions and Animations

Top