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!

This is the 3rd post in a small series we did on form accessibility. If you missed the second post, check out "Managing User Focus with :focus-visible". In

This tutorial demonstrates creating professional-looking JavaScript forms using the Smart Forms framework (note: no longer available). While the framework itself is unavailable, the principles and techniques remain relevant for other form builders.

The CSS box-shadow and outline properties gained theme.json support in WordPress 6.1. Let's look at a few examples of how it works in real themes, and what options we have to apply these styles to WordPress blocks and elements.

Building an inline text editor isn't trivial. The process starts by making the target element editable, handling potential SyntaxError exceptions along the way. Creating Your Editor To build this editor, you'll need to dynamically modify the content

If you’ve recently started working with GraphQL, or reviewed its pros and cons, you’ve no doubt heard things like “GraphQL doesn’t support caching” or

The Svelte transition API provides a way to animate components when they enter or leave the document, including custom Svelte transitions.

This article explores the top PHP form builder scripts available on Envato Market, comparing their features, flexibility, and design. Before diving into specific options, let's understand what a PHP form builder is and why you'd use one. A PHP form

This tutorial guides you through building a file upload system using Node.js, Express, and Multer. We'll cover single and multiple file uploads, and even demonstrate storing images in a MongoDB database for later retrieval. First, set up your projec


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

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.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download
The most popular open source editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6
Visual web development tools
