Creating Complex CSS Transitions with Custom Properties and cubic-bezier()
Build Complex CSS Transitions Using Custom Properties and cubic-bezier()
web design is all about creating visually engaging interfaces, and transitions are one of the most powerful tools for creating those effects. By combining custom CSS properties and using the cubic-bezier() function, you can create complex transitions that will help you create smooth animations for your designs. In this tutorial, we’ll look at how to create these effects with ease.
What Are Custom Properties?
Custom properties, or CSS variables, are a way to store values that can be used throughout your CSS code. Think of them like variables in programming languages — once you set the value of a custom property, you can access it anywhere in your stylesheet. This makes it easy to maintain consistency across your style rules and quickly change all instances of a value with a single change.
Creating a Transition with Custom Properties
Let’s start by taking a look at how to create a basic transition with custom properties. Here, we have a box with a height of 200px that we want to animate when it’s hovered over. First, we’ll create a custom property to store the desired height.
/* Store the desired height as a custom property */
:root {
--boxHeight: 200px;
}
Now, we can add the animation to the box class. Here, we have the starting and ending values stored in custom properties. We also specify a duration of 500ms and an easing curve of ease-in-out.
.box {
transition: height 500ms ease-in-out;
height: var(--boxHeight);
background-color: #ccc;
}
.box:hover {
height: calc(var(--boxHeight) * 2);
}
When the box is hovered, it will transition from the initial height of 200px to a final height of 400px in 500ms with an ease-in-out curve. Easy, right?
Using cubic-bezier() to Create More Complex Transitions
The cubic-bezier() function gives you the ability to create a more complex transition. This is done by specifying four points on a graph that represents the easing curve. The values you specify determine the way the animation behaves. Let’s take a look at how to create a more complex transition with this function.
We’ll again use the same box class from our previous example, except this time we’ll use the cubic-bezier() function to specify a custom easing curve. Instead of using the ease-in-out curve, we’ll use a bouncing curve that has a fast start, slow middle, and fast finish. To do this, we just need to specify the x and y coordinates of the four points of the graph.
.box {
transition: height 500ms cubic-bezier(0.37, 0.17, 0.61, 0.89);
height: var(--boxHeight);
background-color: #ccc;
}
.box:hover {
height: calc(var(--boxHeight) * 2);
}
Now, instead of transitioning with an ease-in-out curve, the box will have a bouncy animation with a fast start, slow middle, and fast finish. Try tweaking the x and y coordinates of the graph to get different effects.
Conclusion
By combining custom CSS properties and the cubic-bezier() function, you can create complex transitions that will make your designs look amazing. With the knowledge from this tutorial, you should now be confident in your ability to create complex transitions with ease.