Transitions

Introduction

CSS transitions offer a smooth way to animate the change of an element's properties over a specified duration. By using transitions, web developers can enhance user experience by providing subtle, interactive effects that trigger when a user interacts with an element or when a property changes. This feature allows for visual continuity and an enhanced feel of interactivity on websites. I will describe the different aspects of CSS transitions, covering the main properties, syntax, and examples. A CSS transition is a way to create animated changes in CSS property values when an element's state changes. For instance, when a user hovers over a button, the background colour may change smoothly from one colour to another. Transitions can be applied to many CSS properties, such as color, height, width, position, opacity, and many more.

CSS transitions consist of a few key components:

  • transition-property: Specifies which property you want to transition.
  • transition-duration: Defines how long the transition will take.
  • transition-timing-function: Determines the speed curve of the transition, essentially controlling how the transition behaves over time.
  • transition-delay: Specifies a delay before the transition starts.

These four properties can be combined into one declaration with the following syntax:

.element_class {
    transition: property duration timing-function delay;
}

The Transition Property

The transition-property defines which CSS property will be the target of the transition. This property indicates which CSS property should undergo the animated transition. It can be a single property or a comma-separated list of properties if you want to transition multiple properties at once.

Commonly transitioned properties include:

  • background-color
  • height
  • width
  • opacity
  • transform
  • color
button {
    transition-property: background-color;
}

This specifies that only the background-color property will transition when its value changes. You can also apply transitions to multiple properties simultaneously. For example:

button {
    transition-property: background-color, transform;
}

In this case, both the background-color and transform properties will transition when triggered.

The Transition Duration

The transition-duration property defines the length of time the transition will take. This can be specified in seconds (s) or milliseconds (ms). It determines how long the transition should run from the start to the end.

button {
    transition-duration: 2s;
}

This rule specifies that the transition will take 2 seconds to complete. You can also define different durations for different properties. For example:

button {
    transition: background-color 1s, transform 0.5s;
}

Here, the background-color transition lasts 1 second, and the transform transition takes 0.5 seconds.

The Transition Timing Function

The transition-timing-function property controls the acceleration curve of the transition. This property defines how the transition progresses over time, allowing for effects like easing in and out, or maintaining a constant pace. Common values for transition-timing-function include:

  • linear: The transition occurs at a constant speed.
  • ease: The transition starts slow, speeds up, and then slows down at the end.
  • ease-in: The transition starts slowly and speeds up toward the end.
  • ease-out: The transition starts quickly and slows down toward the end.
  • ease-in-out: The transition starts slow, speeds up in the middle, and slows down at the end.
button {
    transition-timing-function: ease-in-out;
}

This specifies that the transition will start slow, speed up, and then slow down as it completes. If you wish to use a cubic Bezier curve, you can use the following format:

button {
    transition-timing-function: cubic-bezier(0.42, 0, 0.58, 1);
}

This specifies a custom easing curve, which can result in unique transition effects.

The Transition Delay

The transition-delay property defines how much time should pass before the transition begins. The delay can be specified in seconds (s) or milliseconds (ms). This allows you to introduce a delay before the transition starts, creating a staggered effect for multiple elements or animations.

button {
    transition-delay: 0.5s;
}

Here, the transition will start 0.5 seconds after the change occurs.

If you are transitioning multiple properties and want different delays for each, you can specify them individually.

button {
    transition: background-color 1s ease-in 0.5s, transform 0.5s ease-out 0.2s;
}

In this case, the background-color will start transitioning after 0.5 seconds, while the transform transition will start after 0.2 seconds.

Combining Multiple Transitions

You can combine multiple transitions into a single rule by separating each transition with a comma. This is useful when you want to apply different transitions to multiple properties.

button {
    transition: background-color 1s ease-in, transform 0.5s ease-out;
}

In this example the background-color transitions with a duration of 1 second and uses the ease-in timing function. The transform property transitions with a duration of 0.5 seconds and uses the ease-out timing function.

Practical Examples of CSS Transitions

Hover Effects on Buttons

button {
    background-color: blue;
    color: white;
    transition: background-color 0.3s ease, transform 0.2s ease-in;
}

button:hover {
    background-color: red;
    transform: scale(1.1);
}

In this example, when the button is hovered, the background-color changes smoothly over 0.3 seconds, and the button scales up using the transform property.

Image Hover Effects

img {
    opacity: 1;
    transition: opacity 0.4s ease-in-out;
}

img:hover {
    opacity: 0.5;
}

Here, the opacity of the image changes from fully visible (1) to 50% opacity (0.5) when the image is hovered over, creating a fade-out effect.