Open In App

Transition shorthand with multiple properties in CSS?

Last Updated : 03 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The transition shorthand in CSS allows specifying multiple properties for smooth transitions between different states or values. The transition property in CSS is used to create transitions in an element, enabling changes to occur smoothly over a specified duration. This article demonstrates how to use the transition shorthand to apply hover effects on a <div> element, changing its width and height after the transition.

The list of transition properties is given below:

PropertyDescription
transition-propertySpecifies the CSS property to which the transition is applied.
transition-durationSpecifies the duration over which the transition occurs.
transition-timing-functionSpecifies the speed curve of the transition effect.
transition-delaySpecifies the delay before the transition starts.

Syntax

div {
    transition: <property> <duration> <timing-function> <delay>;
}

Example 1: Basic Transition Properties

In this example we use CSS transitions to expand the width of a text input from 100px to 250px when focused. The transition property is set for a smooth effect.

html
<!DOCTYPE html>
<html>

<head>
    <title>Transition shorthand </title>
    <style>
        h1 {
            color: Green;
            text-align: center;
        }

        h3 {
            text-align: center;
        }

        input[type=text] {
            width: 100px;
            -webkit-transition: width .35s ease-in-out;
            transition: width .35s ease-in-out;
        }

        input[type=text]:focus {
            width: 250px;
        }
    </style>
</head>

<body>
    <h1>GeeksforGeeks</h1>
    <h3>Search:
        <input type="text" name="searchbox">
    </h3>
</body>

</html>

Output:

transition

Note: If the duration part is not specified, the transition will have no effect, because its default value is 0. The transition property specifies mainly two things. The first one is the CSS property to add effects and the second one is the duration unless the transition will be effect less.

Example 2: Adding More Properties to the Transition

In this example we demonstrates CSS transitions using the shorthand transition property. When the div is hovered over, it smoothly expands from 1px by 0px to 300px by 240px, revealing an image.

html
<!DOCTYPE html>
<html>

<head>
    <title>Transition shorthand</title>
    <style>
        h1 {
            color: Green;
        }

        div {
            width: 1px;
            height: 0px;
            text-align: center;
            background: Green;
            -webkit-transition: width 2s, height 2s;
            transition: width 2s, height 2s;
        }

        div:hover {
            width: 300px;
            height: 240px;
        }
    </style>
</head>

<body>
    <h1>GeeksforGeeks</h1>
    <div>
        <img src=
"https://media.geeksforgeeks.org/wp-content/uploads/GG-1.jpg" 
             align="middle">
    </div>
</body>

</html>

Output:

transition


Similar Reads