Table of contents
Transition in CSS
The
transition
property in CSS is used to specify the transition effect for a CSS property when its value changes. It allows you to control the timing and animation of property changes, creating smooth and visually appealing transitions between different states of an element.The
transition
property can be applied to individual CSS properties or multiple properties at once. Here's the syntax for thetransition
property:
transition: [property] [duration] [timing-function] [delay];
[property]
: Specifies the CSS property or properties that you want to apply the transition effect to. Multiple properties can be separated by commas.[duration]
: Specifies the duration of the transition effect. It can be specified in seconds (s) or milliseconds (ms).[timing-function]
: Specifies the timing function that defines the speed curve of the transition. It controls how the intermediate property values are calculated. Common timing functions includelinear
,ease
,ease-in
,ease-out
,ease-in-out
, and more.[delay]
: Specifies an optional delay before the transition effect starts. It can be specified in seconds (s) or milliseconds (ms).
Here's an example that demonstrates the usage of the transition
property:
.box {
width: 200px;
height: 200px;
background-color: blue;
transition: width 1s ease-in-out;
}
.box:hover {
width: 400px;
}
In this example, when you hover over the
.box
element, its width will transition smoothly from200px
to400px
over a duration of1s
. Theease-in-out
timing function is applied, creating a gradual acceleration and deceleration effect.We can apply the
transition
property to various CSS properties, such aswidth
,height
,color
,opacity
,transform
, and many others, to achieve different types of transition effects.By utilizing the
transition
property, you can add subtle animations and smooth transitions to your web page elements, enhancing the user experience and adding visual interest.