Using CSS to create multiple transitions on elements is a great way to add interest and interactivity to your website. By incorporating various transitions, we can create a dynamic and engaging experience for our users. In this article, we’ll cover the basics of how to create multiple transitions on an element using CSS.
Cascading Style Sheets (CSS) are a powerful tool for styling web pages. One of its most useful features is the ability to create smooth and visually appealing transitions between different states of an element, such as when it is hovered or clicked.
What are CSS transformations?
Before we understand how to create multiple transitions, we first understand what CSS transitions are. A transition is a gradual change between two states of an element. For example, when we hover over a button, its background color gradually changes from one color to another. CSS allows us to specify the duration and timing of these transitions, as well as the properties being transformed.
grammar
css-selector{ transition: property duration timing-function delay; }
Transition properties in CSS
The transition properties we can use in CSS include -
transition-property - This property specifies which CSS properties should be transformed.
transition-duration - This property specifies the duration of the transition in seconds or milliseconds. By default, the transition duration is 0, which means no transition effect is applied.
transition-timing-function - This property controls the speed and timing of the transition.
transition-delay - This property specifies the delay before the transition begins.
Create basic transformation
To create a transition, we need to specify the properties we want to animate, such as the transition's duration, timing function, and any delays. For example, to create a transition for the width of a button, for this we can use the following code -
button { transition: width 0.5s ease-in-out; }
The code above specifies that the width of the button will transition over a period of 0.5 seconds using an ease-in-out timing function.
Create multiple transitions
To create multiple transitions on an element, we need to add additional transitions to the CSS code. For example, to create a button that converts both width and background color properties, for this we can use the following code -
button { transition: width 0.5s ease-in-out, background-color 0.5s ease-in-out; }
The code above specifies that the button's width and background color properties will transition over a period of 0.5 seconds using an ease-in-out timing function.
Here are some complete code examples of how to create multiple transitions on an element using CSS -
Example 1
In this example, we will create a button with toggle width and background color properties. However, we will use different durations for each transition to create a staggered effect.
<html> <head> <style> body{ text-align: center; } button { margin: auto; width: 100px; height: 50px; background-color: green; border: none; color: #fff; font-size: 16px; padding: 10px 20px; transition: width 0.5s ease-in, background-color 1s ease-out; } button:hover { width: 150px; background-color: red; } </style> </head> <body> <h3 id="Multiple-Transitions-with-Different-Durations">Multiple Transitions with Different Durations</h3> <button>Hover Me</button> </body> </html>
In the example above, we set a width of 100px and a green background-color for the button. Then, we set the transition properties to transition the width and background color properties. However, we use a duration of 0.5 seconds for the width transition and 1 second for the background color transition. This creates a staggered effect where the button width transitions faster than the background color. When the mouse is over the button, the width will expand to 150px and the background color will change to red.
Example 2
In this example, we will create a box that transforms both the background color and border radius properties. However, we will use a delay for the border radius transition.
<html> <head> <style> body{ text-align: center; } .box { margin: auto; width: 200px; height: 200px; background-color: red; border-radius: 50%; transition: background-color 0.5s ease-in-out, border-radius 0.5s ease-in-out 0.5s; } .box:hover { background-color: blue; border-radius: 0; } </style> </head> <body> <h3 id="Multiple-Transitions-with-Delays">Multiple Transitions with Delays</h3> <div> Hover over the below circle to see multiple transitions</div> <div class="box"></div> </body> </html>
In the above example, we set the width and height of the box to 200px and the background color to red. Then, we set the transition properties to transition the background color and border radius properties. However, we use a delay of 0.5 seconds for the boundary radius transition. This means that the background color will transition immediately, while the border radius will wait 0.5 seconds before transitioning. When the mouse hovers over the box, the background color will change to blue and the border radius will become 0, creating a square.
Example 3
Here, we will create a button that can convert the width and color properties. However, we will use different timing functions for each transition to create unique effects.
<html> <head> <style> body{ text-align: center; } button { margin: auto; width: 120px; height: 60px; background-color: blue; border: none; color: red; font-size: 18px; padding: 10px 20px; transition: width 0.5s cubic-bezier(0.25, 0.1, 0.25, 1), color 1s ease-in-out; } button:hover { width: 180px; color: #fff; } </style> </head> <body> <h3 id="Multiple-Transitions-with-Different-Timing-Functions">Multiple Transitions with Different Timing Functions</h3> <button>Hover Me</button> </body> </html>
In the above example, we set the width of the button to 120px, the background color to blue, and then set the transition properties to transition the width and color properties. However, we use a different timing function for each transition. The width transition uses a custom cubic Bezier function. When the mouse is over the button, the width expands to 180px and the text color changes from red to white.
in conclusion
CSS transitions are a powerful tool for creating smooth and visually appealing effects on web pages. By using transition properties, we can specify the duration, timing function, and properties being transitioned. We can also create multiple transitions on an element by specifying multiple properties in the transition attribute.
The above is the detailed content of How to create multiple transitions on one element using CSS?. For more information, please follow other related articles on the PHP Chinese website!

Here's what I'd like you to know upfront: this is a hard problem. If you've landed here because you're hoping to be pointed at a tool you can run that tells

Picture-in-Picture made its first appearance on the web in the Safari browser with the release of macOS Sierra in 2016. It made it possible for a user to pop

Gatsby does a great job processing and handling images. For example, it helps you save time with image optimization because you don’t have to manually

I learned something about percentage-based (%) padding today that I had totally wrong in my head! I always thought that percentage padding was based on the

SVG and canvas are both technologies that can draw stuff in web browsers, so they are worth comparing and understanding when one is more suitable than the

You know how you can style (to some degree) selected text with ::selection? Well, Jeff Starr uncovered a heck of a weird CSS bug.

You know the single-value syntax: .thing { display: block; }. The value "block" being a single value. There are lots of single values for display. For

I recently attended the ARTIFACT conference in Austin, TX, and was inspired by a few talks about accessibility through the lens of site performance. It became


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version
Useful JavaScript development tools

Notepad++7.3.1
Easy-to-use and free code editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.