Create your own
Lesson illustration

CSS Transitions for Smooth Animations

Hello! Welcome back to your front-end development course.

In our last lesson on pseudo-elements, we saw a glimpse of how the transition property could create smooth, animated effects on hover. You learned to add decorative elements without extra HTML, and now we'll focus on bringing those elements—and any others—to life with fluid motion.

Today's lesson is dedicated to mastering CSS transitions. Your goal is to apply CSS transitions to create smooth animations on property changes. As a designer, you know that motion is a powerful tool for guiding users, providing feedback, and creating a polished, high-quality feel. CSS transitions are the fundamental building block for turning your static designs into dynamic and engaging user interfaces.

1. The Anatomy of a Transition

At its core, a CSS transition tells the browser to smoothly animate a property change from a start state to an end state, rather than having it happen instantly. This is controlled by four main properties:

  • transition-property: Which CSS property should be animated (e.g., background-color, transform).
  • transition-duration: How long the animation should take (e.g., 300ms, 0.5s).
  • transition-timing-function: How the speed of the animation changes over its duration (e.g., starting slow and speeding up). This is often called the "easing" or "animation curve."
  • transition-delay: How long to wait before starting the animation.

Let's watch a short video that introduces these four properties and shows how they work together.

Learn All CSS Transition Property for Smooth Animation

This video from the CSSnippets channel provides a quick and clear visual introduction to the four fundamental transition properties.

Please watch the following segments: Introduction (00:00 - 00:52): This explains the purpose of transitions. Applying Basic Properties (00:52 - 01:40): This part demonstrates transition-property and transition-duration. Transition Delay and Shorthand (03:40 - 04:14): This covers transition-delay and introduces the shorthand syntax we'll use later.

As you saw, by defining these properties on an element, you instruct the browser to animate any changes to the specified transition-property (like those triggered by a :hover state) over the transition-duration.

2. Controlling the "Feel": Timing Functions

The transition-timing-function is arguably the most critical property for a designer. It dictates the acceleration and deceleration of the animation, which fundamentally changes its character. A linear transition feels mechanical, while an ease-out transition feels responsive and natural.

The best way to understand timing functions is to see and interact with them.

An Interactive Guide to CSS Transitions

The article 'An Interactive Guide to CSS Transitions' by Josh W. Comeau is one of the best resources on this topic. It features interactive examples that perfectly illustrate the different timing functions.

Please read the section titled 'Timing functions'. Interact with the demos for linear, ease-out, ease-in, ease-in-out, and ease. Pay close attention to the descriptions of when you might use each one, as this connects directly to UX design principles.

As the article mentions, the default value is ease, which is a great starting point for many animations. However, for more precise control, you can define your own curve using cubic-bezier(). This is analogous to adjusting Bézier curves in a design tool like Figma or Illustrator, giving you complete control over the animation's feel. Tools like cubic-bezier.com let you visually craft these curves.

3. The transition Shorthand and Animating Multiple Properties

Writing out all four properties individually can be verbose. In practice, you will almost always use the transition shorthand property. The most common order is:

transition: <property> <duration> <timing-function> <delay>;

For example:
transition: transform 500ms ease-out;

What if you want to animate multiple properties? You can provide a comma-separated list. Or, if all properties should share the same animation settings, you can use the keyword all.

Let's see a practical example that combines these concepts to transform one shape into another.

#31 CSS Transition Tutorial - CSS Full Tutorial

This video from Dev Dreamer builds a great practical example: transforming a red square into a large, rotating green circle. It's a perfect demonstration of animating multiple properties at once.

Please watch from 04:51 to 07:57. The key parts are: Transitioning Multiple Properties (04:51 - 05:35): See how to use the all keyword. Using the Shorthand (05:35 - 06:24): This demonstrates the shorthand syntax. Practical Example (06:24 - 07:57): Watch how background-color, border-radius, and transform are all animated smoothly on hover.

This example shows how powerful transitions can be. With a single line of CSS, you can coordinate changes to size, shape, color, and position to create a sophisticated animation.

4. Performance, UX, and Accessibility

Creating beautiful animations is one thing; ensuring they are smooth, performant, and accessible is what separates professional front-end work from amateur efforts.

Animation Performance

Not all CSS properties are created equal. Animating properties that affect the layout of the page (like width, height, or margin) forces the browser to recalculate the position of other elements on every frame. This is computationally expensive and can lead to stuttering, "janky" animations, especially on less powerful devices.

In contrast, animating transform and opacity is very cheap. The browser can often offload these calculations to the GPU (hardware acceleration), resulting in buttery-smooth 60fps animations.

As a rule of thumb, always prefer animating transform and opacity over layout-affecting properties. For example, instead of animating left, use transform: translateX(). Instead of animating width, use transform: scaleX().

UX and Accessibility

Subtle details in motion design can significantly improve the user experience. We will also look at how to respect user preferences for reduced motion.

Let's return to Josh Comeau's article to explore these professional-grade considerations.

An Interactive Guide to CSS Transitions

This final reading covers the critical topics of performance and accessibility, which are essential for creating high-quality, user-centric web experiences.

Please read the following three sections from the article: 'Animation performance': This is the most important part. It explains why transform and opacity are better to animate and introduces will-change. 'UX touches': This section offers brilliant, design-centric advice, like creating different animations for mouse-enter and mouse-leave events. 'Respecting motion preferences': Learn how to use the prefers-reduced-motion media query to disable animations for users who need it. This is a crucial accessibility practice.

Conclusion

Today you've learned how to add fluid motion to your designs using CSS transitions. This is a cornerstone of modern front-end development that directly impacts the perceived quality and usability of a website.

Key Takeaways:

  • The Core Properties: Transitions are controlled by transition-property, transition-duration, transition-timing-function, and transition-delay.
  • Shorthand is Standard: Use the transition shorthand for cleaner code (e.g., transition: opacity 300ms ease-in-out;).
  • Timing is Everything: The transition-timing-function (e.g., ease-out) defines the animation's character and is key to making motion feel natural.
  • Performance is Paramount: For smooth animations, prioritize animating the transform and opacity properties. Avoid animating properties that affect layout like width or height.
  • Design for Accessibility: Always use the @media (prefers-reduced-motion: reduce) media query to disable or reduce animations for users who request it.

We have now covered a great deal of HTML and CSS, giving you the tools to build and style well-structured, responsive, and interactive user interfaces.

In our next module, we will make a significant shift. To handle user input, manage application data, and create truly dynamic experiences, we need to add logic to our pages. Our next lesson will introduce you to JavaScript, the programming language of the web, starting with the fundamentals of variables and data types.

Can't find a good explanation? Sign up and we'll make it for you

Sign up