Home  >  Article  >  Web Front-end  >  How to create multiple transitions on one element using CSS?

How to create multiple transitions on one element using CSS?

王林
王林forward
2023-08-26 13:37:02956browse

如何使用 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

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>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>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>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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete
Previous article:CSS pause propertyNext article:CSS pause property