CSS transitions are one of the easiest and most effective ways to add life to your website. Whether you’re building a portfolio, an online store, or a blog, using transitions can make your website more interactive and visually appealing.
In this complete guide, you’ll learn:
- What CSS transitions are
- Why they matter for user experience
- How to use them step-by-step
- Real examples with HTML and CSS
- Best practices and performance tips
What Are CSS Transitions?
CSS Transitions allow you to make changes to CSS property values smoothly over a set period of time, instead of happening instantly.
For example:
- A button that fades from blue to green on hover
- A card that lifts off the page with a shadow
- Text that fades in or out
By default, when a style changes in CSS, the change is immediate. But with a transition, you can define how long the change should take and what style of animation it should use.
Why Use CSS Transitions?
CSS transitions help you:
- Improve user experience
- Make your design feel smoother and modern
- Highlight elements subtly (hover effects, focus states, etc.)
- Keep animations lightweight without JavaScript
CSS Transition Syntax
Here’s the basic syntax:
transition: property duration timing-function delay;
Example:
transition: background-color 0.4s ease-in-out 0s;
Breakdown:
background-color
→ the property to animate0.4s
→ durationease-in-out
→ timing function0s
→ delay (optional)
Transition Shorthand vs Longhand
You can either use shorthand or define each property separately:
Shorthand
transition: all 0.3s ease-in;
Longhand
transition-property: all;
transition-duration: 0.3s;
transition-timing-function: ease-in;
transition-delay: 0s;
Common CSS Properties You Can Animate
Property | Animatable? | Example Use Case |
---|---|---|
background-color | ✅ Yes | Button hover |
color | ✅ Yes | Link focus |
width , height | ✅ Yes | Expanding divs |
opacity | ✅ Yes | Fade in/out text |
transform | ✅ Yes | Rotate, scale, translate |
box-shadow | ✅ Yes | Card hover shadow |
margin , padding | ✅ Yes | Spacing animations |
display | ❌ No | Not animatable directly |
🚫
display
,position
, andz-index
are not animatable.
Simple Example: Button Hover Transition
HTML code
<button class="transition-btn">Hover Me</button>
CSS
.transition-btn {
background-color: #007BFF;
color: white;
padding: 12px 24px;
border: none;
border-radius: 4px;
cursor: pointer;
transition: background-color 0.4s ease;
}
.transition-btn:hover {
background-color: #28a745;
}
When you hover over the button, it smoothly changes from blue to green.
Multi-Property Transitions
You can transition multiple CSS properties at once:
.card {
background-color: white;
box-shadow: 0 0 0 rgba(0, 0, 0, 0);
transform: scale(1);
transition: background-color 0.4s ease, box-shadow 0.3s ease, transform 0.4s ease;
}
.card:hover {
background-color: #f8f9fa;
box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);
transform: scale(1.05);
}
CSS Transition with Delay
.alert {
opacity: 1;
transition: opacity 0.5s ease-in 2s;
}
.alert.hide {
opacity: 0;
}
This fades out an alert after a 2-second delay.
Here is the Real Example: Image Hover Zoom Effect
HTML:
<div class="zoom-container">
<img src="https://via.placeholder.com/300" alt="Example Image" />
</div>
CSS:
.zoom-container img {
width: 100%;
transition: transform 0.5s ease;
}
.zoom-container img:hover {
transform: scale(1.1);
}
This adds a zoom-in effect when hovering over an image.
CSS Transition Timing Functions
Timing functions control the speed curve of the transition:
Function | Description |
---|---|
ease | Default: slow start, fast middle, slow end |
linear | Constant speed |
ease-in | Slow start |
ease-out | Slow end |
ease-in-out | Slow start and end |
cubic-bezier | Custom timing curve |
Example:
transition: all 0.3s cubic-bezier(0.68, -0.55, 0.265, 1.55);
CSS Transitions vs CSS Animations
Feature | Transitions | Animations |
---|---|---|
Trigger | State change (e.g., hover ) | Keyframes or infinite loop |
Control | Limited | More detailed (keyframes) |
Looping | ❌ No | ✅ Yes |
JavaScript Needed | ❌ No | ❌ No (unless advanced) |
✅ Use transitions for simple hover or focus effects.
✅ Use animations when you need sequences or loops.
Best Practices for CSS Transitions
- ✅ Animate lightweight properties like
opacity
,transform
,color
. - ❌ Avoid animating layout-heavy properties like
top
,left
, orwidth
unless needed. - ✅ Keep transitions under 0.5s for fast UI.
- ✅ Combine with
:hover
,:focus
, or JavaScript for interaction. - ✅ Always test on mobile and different browsers.
Here is HTML Starter Template, you can copy/paste to try yourself
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>CSS Transitions Example</title>
<style>
.hover-box {
background: #3498db;
width: 200px;
height: 200px;
transition: background 0.4s ease-in-out;
}
.hover-box:hover {
background: #e74c3c;
}
</style>
</head>
<body>
<h1>CSS Transition Demo</h1>
<div class="hover-box"></div>
</body>
</html>