What is CSS Transition ?

What is CSS Transition ?

How can you use it effectively ?

·

7 min read

In the coming weeks, I plan to create articles that will help you easily accomplish important tasks in CSS and other languages. However, this article focuses solely on the CSS transition, including its various properties and how to use them. I wish I can show you how it works, so I am considering creating a video demonstration to help illustrate the concepts covered in this article—no promise made.

Now, let's delve into the main topic at hand.

What is Transition in CSS?

CSS Transition is a CSS property that allows for smooth animation of changes in a CSS property over a set duration when hovering or focusing on it. Instead of sudden shifts, transitions allow for gradual changes to take place over time. Simply put, transitions make CSS changes appear seamless over a defined period. Pretty cool, right? Nonetheless, not all CSS properties support CSS transitions. At the end of the article, I will provide you with a list of properties that can be transitioned with CSS.

The CSS transition property comprises four components. By setting the values of these properties, you can control the speed, delay, and property of your transition. This allows you to create smooth and seamless animations that can enhance the user experience of your website. The four properties are discussed below:

  • transition-property: This refers to the specific CSS property that you want to apply the transition to. This could be something like the color of an element, its size or even its background-color.

  • transition-duration: This is the amount of time it takes for the changes in the CSS property to occur. This can be measured in seconds (s) or milliseconds (ms). For example, if you set the transition-duration to 2s, the transition will take 2 seconds to complete.

  • transition-delay: It is the amount of time that elapses before the transition begins. This can also be measured in seconds (s) or milliseconds (ms). For example, if you set the transition-delay to 1s, there will be a 1-second delay before the transition starts. This is useful when you want to stagger the transition of multiple elements on the page.

To further understand the CSS transition properties, here's an example. If I have a button and on hover, I want to make a transition to the color, but I want the changes to occur over 2s, which is the same as 2000ms, and I want a 1s wait before the color changes; this is how I will write the CSS code:

button {
height: 50px;
    width: 200px;
    font-size: 28px;
    color: #FFFFFF;
    border: 1px solid #FFFFFF;
    background-color: #0B0D17;
    transition-property: color;
    transition-duration: 2s;
    transition-delay: 1s;
}

button:hover {
    color: red;
}

In this example, when the button is hovered, the color will change to red over a period of 2 seconds, with a 1-second delay before the transition begins.

The above CSS code shows just one property but let’s assume we want to change the width, height, color and background-color. You can list them out like this:

button {
height: 50px;
    width: 200px;
    font-size: 28px;
    color: #FFFFFF;
    border: 1px solid #FFFFFF;
    background-color: #0B0D17;
    transition-property: width, height, color, background-color;
    transition-duration: 2s;
    transition-delay: 1s;
}

button:hover {
    width: 350px;
    height: 70px;
    color: greenyellow;
    background-color: pink;
}

In this example, when the button is hovered, the width and height will change to 350px and 70px, respectively, the color will change to green-yellow, and background-color will change to pink over a period of 2 seconds, with a 1-second delay before the transition begins.

or you can use “all” to represent the properties like this:

button {
height: 50px;
    width: 200px;
    font-size: 28px;
    color: #FFFFFF;
    border: 1px solid #FFFFFF;
    background-color: #0B0D17;
    transition-property: all;
    transition-duration: 2s;
    transition-delay: 1s;
}

button:hover {
    width: 350px;
    height: 70px;
    color: greenyellow;
    background-color: pink;
}

Before I move to explain other ways you can write your code, I want to mention the 4th transition property.

  • transition-timing–function: This simply refers to how the changes in the property will occur. This property has five values, each of which performs a different function. Keep in mind that the transition-timing-function is optional, and if you don't specify it, it will take the default value of ease.

    1. ease: This is the default value for the transition-timing-function property, and it creates a gradual acceleration and deceleration effect on the transition. The transition starts slowly, speeds up, and then slows down again before ending. This creates a smooth and natural-looking animation.

    2. linear: This means the transition will occur at a constant rate. The animation will start and end at the same speed with no acceleration or deceleration.

    3. ease-in: The transition will start slowly and then accelerate before ending. This creates an effect where the animation starts slow and ends fast.

    4. ease-out: The transition will start fast and then decelerate before ending. This creates an effect where the animation starts fast and ends slowly.

    5. ease-in-out: The transition will start slowly, accelerate, and then decelerate before ending. This creates an effect where the animation starts slow, speeds up, and then slows down again before ending.

For example, if you want the width of a button to change from 50px to 100px over a period of 2 seconds, and you want the transition to start slow and finish fast, you can use the following code:

button {
height: 50px;
    width: 200px;
    font-size: 28px;
    color: #FFFFFF;
    border: 1px solid #FFFFFF;
    background-color: #0B0D17;
    transition-property: width;
    transition-duration: 2s;
transition-timing-function: ease-in

}

button:hover {
    width: 100px;
}

It is worth noting that you can also use the cubic-bezier curve to create a custom timing function by specifying two control points; this can give you more flexibility and control over the animation.

what is the cubic-bezier curve?

The cubic-bezier curve is a powerful tool for defining the timing of a transition. It's specified by four coordinates (x1, y1, x2, y2), representing the curve's two control points. These coordinates are values between 0 and 1, and they control how fast the transition starts and ends.

To use the cubic-bezier curve in a transition, you simply need to set the transition-timing-function property to cubic-bezier(x1, y1, x2, y2). This will give you more control over the animation and help you create a more natural and smooth transition.

button {
    height: 50px;
    transition-property: width;
    transition-duration: 2s;
    transition-timing-function: cubic-bezier(0.25, 0.1, 0.25, 1);
}

button:hover {
    height: 100px;
}

But how do you combine transitions property in one line?

To combine these different properties, you can use the CSS property “transition”. Although you don’t have to specify all the values, transition-propertyand transition-duration are enough for the transition to occur.

Let’s assume you want to use the four properties; the syntax is:

transition: <transition-property> <transition-duration> <transition-timing-function> <traansition-delay>

There are two ways of using this; you can either specify the actions you want on each property or use the “all” value we talked about earlier. So, let me show you an example of how to use this.

Method 1: This way, you can specify the changes you want to see and how you want to see them on each property.

button {
    height: 50px;
    width: 200px;
    font-size: 28px;
    color: #FFFFFF;
    border: 1px solid #FFFFFF;
    background-color: #0B0D17;
    transition: color 3s, width 2s ease-in-out, background-color 1s ease-in 3s;

}

button:hover {
    color: #adff2f;
    width: 350px;
    background-color: #ffc0cb;
}

Method 2: When using this method, only the same action will be applied to all the properties.


button {
    height: 50px;
    width: 200px;
    font-size: 28px;
    color: #FFFFFF;
    border: 1px solid #FFFFFF;
    background-color: #0B0D17;
    transition: all 1s ease-in 3s;

}

button:hover {
    color: #adff2f;
    width: 350px;
    background-color: #ffc0cb;
}

Does transition work on all properties?

The simple answer is No. Truly, the CSS Transition is a powerful tool; however, it can only work on certain properties like color, background-color, border-color, transform, height and many others. While Properties like display and position cannot be transitioned using CSS. As such, It is important to keep in mind which properties are animatable using CSS and which are not when working with transitions in your web development projects.

In conclusion, it is important to always remember that having too many properties being transitioned simultaneously can affect the website's performance. It is best practice to use the minimum number of properties necessary for the desired effect.

For more information on CSS transition and how to use it effectively, visit.

Thank you for making it down here; I hope this article has helped you understand what CSS transition is and how to use it effectively. Kindly share with others who may find it useful. Also, remember to like, comment and don't hesitate to reach out if you have any further questions.

See you in the next article.


https://amzn.to/3l3Msqw

Did you find this article valuable?

Support ZordCoder by becoming a sponsor. Any amount is appreciated!