Transition

Transition

Table of contents

Transition in CSS

  1. 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.

  2. The transition property can be applied to individual CSS properties or multiple properties at once. Here's the syntax for the transition 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 include linear, 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;
}
  1. In this example, when you hover over the .box element, its width will transition smoothly from 200px to 400px over a duration of 1s. The ease-in-out timing function is applied, creating a gradual acceleration and deceleration effect.

  2. We can apply the transition property to various CSS properties, such as width, height, color, opacity, transform, and many others, to achieve different types of transition effects.

  3. 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.