Skip to main content

Command Palette

Search for a command to run...

CSS Transitions Tutorial

Learn How to Animate CSS Elements with Transitions

Published
3 min read
CSS Transitions Tutorial
R

Passionate Software Engineer with three years of experience, dedicated to creating robust and scalable web applications. I have a strong foundation in JavaScript, React, Python, and Node.js, and enjoy working with modern tech stacks to build innovative solutions. I'm committed to writing clean and efficient code, following best practices, and staying updated with the latest industry trends.

Throughout my career, I've gained valuable experience in full-stack development, including frontend design, backend development, and database management. I love solving complex problems and implementing creative solutions that enhance user experiences. I believe in the power of collaboration and thrive in cross-functional teams where I can contribute my skills and learn from others.

Beyond coding, I am an avid learner and enjoy sharing my knowledge with the developer community. I regularly contribute to open-source projects, write technical blog posts, and engage in discussions on emerging technologies. I'm always seeking opportunities to expand my skill set and stay at the forefront of software engineering.

In this tutorial, we’re taking a look at how to animate components using CSS transitions!

A transition occurs when we tell a CSS property value to change, over a specified period of time.

We do this with the transition property, which is a shorthand of the following properties:

  • transition-property
  • transition-duration
  • transition-timing-function
  • transition-delay
    .element {
    transition-property: property;
    transition-duration: duration;
    transition-timing-function: timing-function;
    transition-delay: delay;
    }
    
    This is equivalent to:
    .element {
    transition: property duration timing-function delay;
    }
    

Transition Properties

transition-property is the property you wish to transition.

transition-duration is the duration of the transition. The value is specified in seconds (eg. 5s for 5 seconds).

transition-timing-function is how the transition occurs. We’ll look at this later.

transition-delay is how long you want to wait before starting the duration. Again, the value is specified in seconds (eg. 5s for 5 seconds).

Activating transitions

You can activate a CSS transition with a pseudo-class like :hover (when a mouse hovers over the element), :focus (when a user tabs onto or click an input element), or :active (when user clicks the element).

Let’s see an example:

.button {
  background-color: green;
  transition: background-color 2s ease-out;
}
.button:hover {
  background-color: gold;
}

What is the transition-timing function?

The transition-timing-function dictates how a transition occurs. All transitions are set to linear by default, meaning the property changes evenly throughout the transition.

For example:

.element {
  transition-property: transform;
  transition-duration: 1s;
  transition-timing-function: linear;
}

Which is the same as:

.element {
  transition: transform 1s linear;
}

There are a number of values we can use for our transition-timing-function:

  • ease - the transition has a slow start, fast middle, then a slow end
  • linear - the transition has a consistent speed from start to end
  • ease-in - the transition will have a slow start
  • ease-out - the transition will have a slow end
  • ease-in-out - the transition has a slow start and end
  • cubic-bezier(n,n,n,n) - a customizable transition values that we specify ourselves. It’s handy to use generator tools such as https://cubic-bezier.com/.

Let’s see them all in action:

Transitioning multiple properties

We can transition multiple properties at once, by separating them with a comma.

.element {
  transition: color 2s ease-out,
              background-color 2s ease-out;
}

Which properties can be animated?

Lots! Here’s the full list here.

Conclusion

If you liked this blog post, follow me on Twitter where I post daily about Tech related things!

🌎 Let's Connect

CSS Tutorials

Part 24 of 25

Learn the ins and outs of CSS with my comprehensive blog series! From the basics of CSS syntax to advanced techniques like responsive design and CSS animations, these tutorials will help you!

Up next

CSS Animation Tutorial

Learn How to Create Animations with CSS

More from this blog

Rembert Designs Blog | Helping Web Developers And Designers with Resources

107 posts

Passionate Software Engineer with three years of experience, dedicated to creating robust and scalable web applications. I have a strong foundation in JavaScript, React, Python, and Node.js.