A complete guide to CSS transition

CSS transitions allow you to animate changes to CSS properties over a specified duration. Transitions are a useful way to add subtle, visually appealing effects to your web page without the need for complex animations. In this guide, we’ll take a closer look at CSS transitions and show you how to use them in your code.

Basic Syntax

The basic syntax for a CSS transition is as follows:

css
transition: property duration timing-function delay;

property: The CSS property you want to animate (e.g. background-color, width, height, etc.)
duration: The length of time (in seconds or milliseconds) that the transition should take to complete.
timing-function: The easing function that controls the transition’s acceleration and deceleration. This can be a preset value (e.g. ease-in, ease-out, ease-in-out) or a custom cubic-bezier curve.
delay: The length of time (in seconds or milliseconds) to wait before starting the transition.

For example, the following CSS rule will animate changes to an element’s background-color property over 1 second, using an ease-in-out timing function:

css
transition: background-color 1s ease-in-out;

Specifying Multiple Properties

You can also specify multiple CSS properties to animate by separating them with commas. For example, the following rule will animate changes to both the background-color and width properties:

css
transition: background-color 1s ease-in-out, width 0.5s linear;

This will animate changes to the background-color property over 1 second using an ease-in-out timing function, and changes to the width property over 0.5 seconds using a linear timing function.

Shorthand Syntax

You can also use the shorthand transition property to specify multiple transitions for different properties with different durations and timing functions. Here’s the syntax:

css
transition: property1 duration1 timing-function1, property2 duration2 timing-function2, ...;

For example, the following rule will animate changes to the background-color property over 1 second using an ease-in-out timing function, and changes to the width property over 0.5 seconds using a linear timing function:

css
transition: background-color 1s ease-in-out, width 0.5s linear;

Using Transition Events

You can use JavaScript to listen for transition events, such as when a transition starts, ends, or is cancelled. Here are the available events:

transitionstart: Fires when a transition starts.
transitionend: Fires when a transition ends.
transitioncancel: Fires when a transition is cancelled.

For example, the following code will log a message to the console when a transition ends:

javascript
var element = document.querySelector('.my-element');
element.addEventListener('transitionend', function(event) {
console.log('Transition ended');
});

Conclusion

CSS transitions are a powerful tool for animating changes to CSS properties over a specified duration. By using transitions effectively, you can add subtle, visually appealing effects to your web page without the need for complex animations. By mastering the basic syntax and available options, you can create professional-looking transitions that enhance the user experience on your site.