Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
CSS transition-duration property
The CSS transition-duration property is used to set the duration of CSS transitions. It specifies how long a transition takes to complete when an element changes from one state to another.
Syntax
selector {
transition-duration: time;
}
Possible Values
| Value | Description |
|---|---|
time |
Duration in seconds (s) or milliseconds (ms) |
0s |
No transition (default) |
inherit |
Inherits from parent element |
Example: Basic Transition Duration
The following example demonstrates a 2-second transition duration on hover −
<!DOCTYPE html>
<html>
<head>
<style>
.box {
width: 150px;
height: 150px;
background-color: blue;
transition-property: height;
transition-duration: 2s;
margin: 20px;
}
.box:hover {
height: 200px;
}
</style>
</head>
<body>
<h3>Hover over the box to see transition</h3>
<div class="box"></div>
</body>
</html>
A blue box that smoothly expands its height from 150px to 200px over 2 seconds when hovered.
Example: Multiple Duration Values
When transitioning multiple properties, you can specify different durations for each property −
<!DOCTYPE html>
<html>
<head>
<style>
.multi-box {
width: 100px;
height: 100px;
background-color: red;
border-radius: 0px;
transition-property: width, background-color, border-radius;
transition-duration: 1s, 3s, 0.5s;
margin: 20px;
}
.multi-box:hover {
width: 200px;
background-color: green;
border-radius: 50px;
}
</style>
</head>
<body>
<h3>Multiple properties with different durations</h3>
<div class="multi-box"></div>
</body>
</html>
A red box that on hover: changes width in 1 second, background color in 3 seconds, and border-radius in 0.5 seconds.
Conclusion
The transition-duration property controls how long transitions take to complete. Use shorter durations (0.2s-0.5s) for subtle effects and longer durations (1s-3s) for more dramatic animations.
Advertisements
