Transitions in CSS
Do not miss this exclusive book on Binary Tree Problems. Get it now for free.
CSS Transitions allows property changes in CSS values to occur smoothly over a specified duration.
Transitions in CSS allows us to control how the transition takes place between the two states of the element. For example, when we hovering the mouse over a button, we can change the background color, dimensions, shape of the element with the help of CSS selector, and pseudo-class. We can change any other or combination of properties over a duration of time, though. The transition allows us to determine how the change in color and dimensions takes place. We can use the transitions to animate the changes, and make the changes visually alluring and attractive to the user and hence, giving better user experience and involvement.
Transition is an effective way of representing the change in state, and different styles for each state. The straightforward way for directing styles for different states is by using the: hover, :focus, :active, and :target pseudo-classes.
transition: property|duration|timing-function|delay
Transition Properties are as follows:
- transition
- transition-delay(specifies the delay in transition effect)
- transition-duration(specifies the duration of effect in seconds)
- transition-property(specifies the name of property to which css applies)
- transition-timing-function(specifies how intermediate values will be calculated,allows transition to change speed over its duration)
To apply the transition effect we need to specify two things
- transition property
- transition duration
Transitions are a set of properties that allows front-end website and webapp developers to smoothly change the values of a specific CSS property over a given time.
The use the shorthand transition property to control CSS transitions as well the use of this shorthand is not only easy, but it can also avoid out-of-sync parameters that can be quite annoying to debug in CSS.
Here's a simple transition example:
div {
width: 80px;
height: 80px;
background: yellow;
transition: width 2s;
}
div:hover {
width: 200px;
}
Before hovering:
After hovering:
A value of none means that no property will transition. Likewise, a list of properties to be transitioned, or the keyword all indicates that all properties are to be transitioned.
Transition delay property can have the following values:
transition-delay: time|initial|inherit;
- time - specifies seconds to wait for transition to take place
- initial - set property to default value
- inherit - inherit property from parent element
Transition duration property can have the following values:
transition-duration: time|initial|inherit;
- time - specifis seconds a transition effect takes to complete.
- initial - set property to default value.
- inherit - inherit property from parent element.
Transition property can have the following values:
transition-property: none|all|property|initial|inherit;
- none - no transition effect will take place.
- all - all transition properties will take place.
- property - Defines a comma separated list of CSS property names the transition effect is for.
Transition-timing-function property can have the following values:
transition-timing-function: linear|ease|ease-in|ease-out|ease-in-out|step-start|step-end
- ease - specifies a transition with slow start,then fast and end slowly.
- linear - specifies transition with same speed from start to end.
- ease-in - specifies a transition with slow start.
- ease-out - specifies transition with slow end.
- ease-in-out - specifies transition with slow start and end.
- cubic-bezier - Define your own values in the cubic-bezier function(values from 0 to 1).
Here's an example using all the transition properties:
div {
width: 50px;
height: 50px;
background: #8DFF33;
transition-property: height;
transition-duration: 1s;
transition-timing-function: linear;
transition-delay: 2s;
}
div:hover {
height: 200px;
}
Before hovering:
After hovering:
Property values | Description |
---|---|
inherit | inherit this property from parent element |
initial | sets property to initial value |
here's an example showing transition code for different browsers:
.box {
width: 150px;
height: 150px;
background: red;
margin-top: 20px;
margin-left: auto;
margin-right: auto;
}
.box:hover {
background-color: green;
cursor: pointer;
-webkit-transition: background-color 1s ease-out;
-moz-transition: background-color 1s ease-out;
-o-transition: background-color 1s ease-out;
transition: background-color 1s ease-out;
}
Before hovering:
After hovering:
Apllications of transitions:
1.CSS transitions provide a way to control animation speed when changing CSS properties.
2.CSS transitions helps to add an animated look and feel to the web page.
3.CSS transitions can be used along with CSS tranform to create graphics.
Question
Which of the following css property is used to define a delay before an animation starts?
Question
Which of the following css property is used to define the time it takes one iteration of an animation to play?
Question
Which of the following css property is used to describe how the animation will play?
With this article at OpenGenus, you must have a good idea of transitions in CSS.
Sign up for FREE 3 months of Amazon Music. YOU MUST NOT MISS.