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
transition-property– defines which property will be animatedtransition-duration– duration of the transition (e.g.,0.5s,200ms)transition-timing-function– how the transition progresses (e.g.,ease,linear)transition-delay– delay before the transition starts
Simple transition example
The transition below changes the background color on hover, using the longhand form:
transition-property
Defines which CSS property will be animated. It can take:
all– animates all properties that can be transitionedbackground-color,width,opacity, etc. – animates only the specified propertynone– disables transitions
transition-duration
Defines how long the transition takes. Accepted values:
s– seconds (e.g.,0.5s)ms– milliseconds (e.g.,200ms)
transition-timing-function
Defines how the transition progresses over time. Controls the speed curve of the animation. Accepted values:
ease– starts slow, accelerates, then slows down (default)linear– constant speed throughout the transitionease-in– starts slow, then acceleratesease-out– starts fast, then slows downease-in-out– starts and ends slowcubic-bezier(x1, y1, x2, y2)– custom function for advanced control
transition-delay
Defines the delay before the transition starts. Accepted values:
0s– no delay0.5s,200msetc. – custom 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;
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.
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
- Transitions – triggered by a CSS property change (e.g., hover), simpler
- Animations – defined with
@keyframes, can be complex, repetitive, and independent of interaction
Properties involved in CSS animations
@keyframes– defines the stages of the animationanimation-name– the name of the animation (linked to@keyframes)animation-duration– duration of one iterationanimation-timing-function– how the animation progresses over timeanimation-delay– delay before the animation startsanimation-iteration-count– how many times the animation repeatsanimation-direction– direction of the animation (normal,reverse,alternate)animation-fill-mode– behavior before/after the animation (none,forwards,backwards,both)
Simple animation example – pulsing
This example creates a "pulse" animation that repeatedly enlarges and shrinks an element.
Simple animation example – fade in
This example makes an element appear gradually, increasing opacity from 0 to 1.
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.
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.
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.
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.
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.
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.
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;
2. Animated loader
An animated loader indicates progress or waiting. Below, a circle pulses continuously to suggest activity.
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.
4. Animated cards
Cards can have effects on appearance or interaction. In the example below, they animate with a fade-in on load.
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.
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
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 */
3. Best practices
To keep animations fast and smooth, follow these recommendations:
- Use
transformandopacityfor animations - Avoid modifying layout during animation
- Test on real devices, not just in the browser
- Respect user preferences:
@media (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.