Understanding CSS Transitions

Abstract image used to represent Understanding CSS Transitions
Image by Lance Asper.

CSS transitions are a simple yet powerful tool that allows us to create smooth, animated changes between different states of an element. Introduced in CSS3, transitions enable us to add a touch of interactivity and polish to our interfaces without the need for JavaScript. With this article, I intend to explore what CSS transitions are, how they work, and offer some practical examples on how to get started.


What are CSS Transitions?

At their core, CSS transitions define how changes to CSS properties are animated over time. Rather than changes between an element's states happening instantly, transitions allow these adjustments to occur smoothly, making for a more visually appealing experience.

For example, consider a button that changes its background colour from blue to red when we hover over it:

button {
  background-color: blue;
}

button:hover {
  background-color: red;
}

Adding a transition to the default state tells the browser to make this transition smoother:

button {
  background-color: blue;
  transition: background-color 0.5s ease;
}

button:hover {
  background-color: red;
}

This is instructing the browser: "when changing from this default state, transition the background-color property over a halfsecond period".

Now, the background colour transitions smoothly from blue to red over half a second.


How Do CSS Transitions Work?

CSS transitions are controlled by four key properties:

  • transition-property: Specifies which CSS properties to animate. For example, background-color, width, etc. We can also use the keyword all to include every animatable property.
  • transition-duration: Defines how long the transition should take (e.g., 0.5s or 200ms).
  • transition-timing-function: Controls the speed curve of the transition. Common values include ease, linear, ease-in, ease-out, and ease-in-out.
  • transition-delay: Sets a delay before the transition begins (e.g., 1s).

These can be combined into a shorthand property for brevity. For example:

button {
  background-color: blue;
  transition: background-color 0.5s ease-in-out 0.2s;
}

Here, the background colour transition lasts 0.5 seconds, follows an ease-in-out curve, and starts after a 0.2second delay.


Practical Examples of CSS Transitions

Hover Effects

Transitions are most commonly used for hover effects, creating a more interactive feel and giving the user visual feedback:

a {
  color: black;
  text-decoration: none;
  transition: color 0.3s ease;
}

a:hover {
  color: orange;
}

Expanding Elements

There are lots of cases where smoothly expanding elements would improve our users' experience. Dropdowns or accordions are good examples:

div {
  height: 50px;
  overflow: hidden;
  transition: height 0.4s ease;
}

div.open {
  height: 150px;
}

Here, adding the .open class to the div will trigger the height change with a smooth transition between 50px and 150px.

Changing Multiple Properties

Transitions can of course handle multiple properties simultaneously, for example:

.card {
  background-color: white;
  transform: scale(1);
  transition: background-color 0.3s, transform 0.3s;
}

.card:hover {
  background-color: lightgray;
  transform: scale(1.1);
}

When hovered over, the card's background-color changes and it grows slightly larger via scale.


Best Practices for CSS Transitions

  • Use HardwareAccelerated Properties

    : Animating properties like transform and opacity tends to perform better than animating layoutaffecting properties like width or height.
  • Keep Transitions Subtle

    : Overusing transitions or making them too slow can frustrate users. Stick to brief and purposeful animations.
  • Test across devices: check that transitions respond well on the older browsers and devices the project supports. Brief effects can still feel slow when the page has other work to do.

Wrapping Up

CSS transitions are a fantastic way to add polish and interactivity to our designs without overcomplicating the code. Transitions create a more engaging user experience by animating changes to CSS properties. Whether we're crafting hover effects or creating smooth animations, transitions are a valuable tool in modern web design.

Key Takeaways

  • CSS transitions animate changes to properties over time, allowing us to give visual feedback to our users alongside more indepth animation and movement within our web applications.
  • Use the shorthand transition to define property, duration, timing function, and delay.
  • Best practices include animating transform or opacity for better performance.

Mastering CSS transitions allows us to build interfaces that feel fluid, responsive, and polished, all essential qualities in any modern web project.

Postscript

January 2020: prefers-reduced-motion is now available across the main browsers. Use it to reduce nonessential movement for people who request it; the original examples above reflect the May 2017 article.

For the card example, add @media (prefers-reduced-motion: reduce) { .card { transition: none; } .card:hover { transform: none; } } after the existing rules. The hover colour still changes, but the card no longer scales or animates that change.

For the expanding element, give the example's <div> a class such as .expanding-panel, then add @media (prefers-reduced-motion: reduce) { .expanding-panel { transition: none; } }. Its open state remains available, with the height change happening immediately.

Planning a platform change?

I help teams make difficult platform work clearer, from architecture decisions and migrations to launch recovery, performance, and search visibility.