Table of Contents
What are CSS transformations?
grammar
Transition properties in CSS
Create basic transformation
Create multiple transitions
Example 3
in conclusion
Home Web Front-end CSS Tutorial How to create multiple transitions on one element using CSS?

How to create multiple transitions on one element using CSS?

Aug 26, 2023 pm 01:37 PM

如何使用 CSS 在一个元素上创建多个过渡?

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

1

2

3

css-selector{

   transition: property duration timing-function delay;

}

Copy after login

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 -

1

2

3

button {

   transition: width 0.5s ease-in-out;

}

Copy after login

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 -

1

2

3

button {

   transition: width 0.5s ease-in-out, background-color 0.5s ease-in-out;

}

Copy after login

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.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

<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>Multiple Transitions with Different Durations</h3>

      <button>Hover Me</button>

   </body>

</html>

Copy after login

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.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

<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>Multiple Transitions with Delays</h3>

      <div> Hover over the below circle to see multiple transitions</div>

      <div class="box"></div>

   </body>

</html>

Copy after login

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.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

<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>Multiple Transitions with Different Timing Functions</h3>

      <button>Hover Me</button>

   </body>

</html>

Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Building an Ethereum app using Redwood.js and Fauna Building an Ethereum app using Redwood.js and Fauna Mar 28, 2025 am 09:18 AM

With the recent climb of Bitcoin’s price over 20k $USD, and to it recently breaking 30k, I thought it’s worth taking a deep dive back into creating Ethereum

Vue 3 Vue 3 Apr 02, 2025 pm 06:32 PM

It&#039;s out! Congrats to the Vue team for getting it done, I know it was a massive effort and a long time coming. All new docs, as well.

Can you get valid CSS property values from the browser? Can you get valid CSS property values from the browser? Apr 02, 2025 pm 06:17 PM

I had someone write in with this very legit question. Lea just blogged about how you can get valid CSS properties themselves from the browser. That&#039;s like this.

A bit on ci/cd A bit on ci/cd Apr 02, 2025 pm 06:21 PM

I&#039;d say "website" fits better than "mobile app" but I like this framing from Max Lynch:

Stacked Cards with Sticky Positioning and a Dash of Sass Stacked Cards with Sticky Positioning and a Dash of Sass Apr 03, 2025 am 10:30 AM

The other day, I spotted this particularly lovely bit from Corey Ginnivan’s website where a collection of cards stack on top of one another as you scroll.

Using Markdown and Localization in the WordPress Block Editor Using Markdown and Localization in the WordPress Block Editor Apr 02, 2025 am 04:27 AM

If we need to show documentation to the user directly in the WordPress editor, what is the best way to do it?

Comparing Browsers for Responsive Design Comparing Browsers for Responsive Design Apr 02, 2025 pm 06:25 PM

There are a number of these desktop apps where the goal is showing your site at different dimensions all at the same time. So you can, for example, be writing

Let's use (X, X, X, X) for talking about specificity Let's use (X, X, X, X) for talking about specificity Mar 24, 2025 am 10:37 AM

I was just chatting with Eric Meyer the other day and I remembered an Eric Meyer story from my formative years. I wrote a blog post about CSS specificity, and

See all articles