Home 〉 CSS Tutorials 〉 CSS Transition Property Explained with Easy Examples | Learn CSS Animation
CSS Transition Property Explained with Easy Examples | Learn CSS Animation !
Want to animate changes like color or size smoothly? The `transition` property lets you add animations to any CSS property change. Learn how to use `transition-duration`, `transition-timing-function`, and more. It's ideal for buttons, hovers, and interactive designs.
The CSS transition property allows you to create smooth animations when CSS property values change. Instead of changes happening instantly, transitions let them occur over time, making your website feel more dynamic and polished.
CSS transitions make it easier to enhance user experience without writing JavaScript. They help in:
Syntax ✍
selector {
transition: property duration timing-function delay;
}
Example:
Example 📄
.box {
transition: background-color 0.5s ease-in-out;
}
Change background color when hovering.
<!DOCTYPE html>
<html>
<head>
<style>
.btn {
background-color: #3498db;
color: white;
padding: 10px 20px;
border: none;
transition: background-color 0.3s ease;
}
.btn:hover {
background-color: #2ecc71;
}
</style>
</head>
<body>
<button class="btn">Hover Me</button>
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
Explanation: The 'transition' makes the background color shift smoothly when the user hovers over the button.
<!DOCTYPE html>
<html>
<head>
<style>
.box {
width: 100px;
height: 100px;
background: red;
transition: width 1s ease, height 1s ease;
}
.box:hover {
width: 200px;
height: 200px;
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
Explanation: On hover, the box gradually changes size.
<!DOCTYPE html>
<html>
<head>
<style>
.circle {
width: 50px;
height: 50px;
background: purple;
border-radius: 50%;
transition: transform 0.5s ease 0.3s;
}
.circle:hover {
transform: scale(1.5);
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
Explanation: The scale effect will begin 0.3s after hover and last for 0.5s.
Property | Description |
---|---|
'transition-property' | The CSS property to animate (e.g. 'width', 'color') |
'transition-duration' | Time the transition takes (e.g. '1s', '500ms') |
'transition-timing-function' | Easing function ('ease', 'linear', 'ease-in') |
'transition-delay' | Delay before the transition starts |
CSS transitions are an easy and powerful way to animate changes on your webpage without needing JavaScript. They help create a smooth user experience and bring a more interactive feel to your design. Mastering 'transition' is the first step toward building more advanced CSS animations.
Yes! You can separate each transition with commas.
No, you can use them with just CSS, triggered by hover, focus, active, etc.
'transition' animates changes in property values. 'animation' allows more control like keyframes and looping.
No, 'display' is not animatable. Use opacity or transform instead.