- CSS Transitions
- Browser Support for Transitions
- How to Use CSS Transitions?
- Example
- Example
- Change Several Property Values
- Example
- Specify the Speed Curve of the Transition
- Example
- Delay the Transition Effect
- Example
- Transition + Transformation
- Example
- More Transition Examples
- Example
- Example
- CSS Transition Properties
- transition-timing-function
- Try it
- Syntax
- Values
- Accessibility concerns
- Formal definition
- Formal syntax
- Examples
- Cubic-Bezier examples
- Step examples
- Specifications
- Browser compatibility
- See also
- Found a content problem with this page?
- MDN
- Support
- Our communities
- Developers
CSS Transitions
CSS transitions allows you to change property values smoothly, over a given duration.
Mouse over the element below to see a CSS transition effect:
In this chapter you will learn about the following properties:
- transition
- transition-delay
- transition-duration
- transition-property
- transition-timing-function
Browser Support for Transitions
The numbers in the table specify the first browser version that fully supports the property.
Property | |||||
---|---|---|---|---|---|
transition | 26.0 | 10.0 | 16.0 | 6.1 | 12.1 |
transition-delay | 26.0 | 10.0 | 16.0 | 6.1 | 12.1 |
transition-duration | 26.0 | 10.0 | 16.0 | 6.1 | 12.1 |
transition-property | 26.0 | 10.0 | 16.0 | 6.1 | 12.1 |
transition-timing-function | 26.0 | 10.0 | 16.0 | 6.1 | 12.1 |
How to Use CSS Transitions?
To create a transition effect, you must specify two things:
Note: If the duration part is not specified, the transition will have no effect, because the default value is 0.
The following example shows a 100px * 100px red element. The element has also specified a transition effect for the width property, with a duration of 2 seconds:
Example
The transition effect will start when the specified CSS property (width) changes value.
Now, let us specify a new value for the width property when a user mouses over the element:
Example
Notice that when the cursor mouses out of the element, it will gradually change back to its original style.
Change Several Property Values
The following example adds a transition effect for both the width and height property, with a duration of 2 seconds for the width and 4 seconds for the height:
Example
Specify the Speed Curve of the Transition
The transition-timing-function property specifies the speed curve of the transition effect.
The transition-timing-function property can have the following values:
- ease — specifies a transition effect with a slow start, then fast, then end slowly (this is default)
- linear — specifies a transition effect with the same speed from start to end
- ease-in — specifies a transition effect with a slow start
- ease-out — specifies a transition effect with a slow end
- ease-in-out — specifies a transition effect with a slow start and end
- cubic-bezier(n,n,n,n) — lets you define your own values in a cubic-bezier function
The following example shows some of the different speed curves that can be used:
Example
Delay the Transition Effect
The transition-delay property specifies a delay (in seconds) for the transition effect.
The following example has a 1 second delay before starting:
Example
Transition + Transformation
The following example adds a transition effect to the transformation:
Example
More Transition Examples
The CSS transition properties can be specified one by one, like this:
Example
div <
transition-property: width;
transition-duration: 2s;
transition-timing-function: linear;
transition-delay: 1s;
>
or by using the shorthand property transition :
Example
CSS Transition Properties
The following table lists all the CSS transition properties:
Property | Description |
---|---|
transition | A shorthand property for setting the four transition properties into a single property |
transition-delay | Specifies a delay (in seconds) for the transition effect |
transition-duration | Specifies how many seconds or milliseconds a transition effect takes to complete |
transition-property | Specifies the name of the CSS property the transition effect is for |
transition-timing-function | Specifies the speed curve of the transition effect |
transition-timing-function
The transition-timing-function CSS property sets how intermediate values are calculated for CSS properties being affected by a transition effect.
Try it
This, in essence, lets you establish an acceleration curve so that the speed of the transition can vary over its duration.
This acceleration curve is defined using one for each property to be transitioned.
You may specify multiple easing functions; each one will be applied to the corresponding property as specified by the transition-property property, which acts as a transition-property list. If there are fewer easing functions specified than in the transition-property list, the user agent must calculate which value is used by repeating the list of values until there is one for each transition property. If there are more easing functions, the list is truncated to the right size. In both cases, the CSS declaration stays valid.
Syntax
/* Keyword values */ transition-timing-function: ease; transition-timing-function: ease-in; transition-timing-function: ease-out; transition-timing-function: ease-in-out; transition-timing-function: linear; transition-timing-function: step-start; transition-timing-function: step-end; /* Function values */ transition-timing-function: steps(4, jump-end); transition-timing-function: cubic-bezier(0.1, 0.7, 1, 0.1); /* Steps Function keywords */ transition-timing-function: steps(4, jump-start); transition-timing-function: steps(10, jump-end); transition-timing-function: steps(20, jump-none); transition-timing-function: steps(5, jump-both); transition-timing-function: steps(6, start); transition-timing-function: steps(8, end); /* Multiple easing functions */ transition-timing-function: ease, step-start, cubic-bezier(0.1, 0.7, 1, 0.1); /* Global values */ transition-timing-function: inherit; transition-timing-function: initial; transition-timing-function: revert; transition-timing-function: revert-layer; transition-timing-function: unset;
Values
The non-step keyword values (ease, linear, ease-in-out, etc.) each represent cubic Bézier curve with fixed four point values, with the cubic-bezier() function value allowing for a non-predefined value. The step easing functions divides the input time into a specified number of intervals that are equal in length. It is defined by a number of steps and a step position.
Equal to cubic-bezier(0.25, 0.1, 0.25, 1.0) , the default value, increases in velocity towards the middle of the transition, slowing back down at the end.
Equal to cubic-bezier(0.0, 0.0, 1.0, 1.0) , transitions at an even speed.
Equal to cubic-bezier(0.42, 0, 1.0, 1.0) , starts off slowly, with the transition speed increasing until complete.
Equal to cubic-bezier(0, 0, 0.58, 1.0) , starts transitioning quickly, slowing down as the transition continues.
Equal to cubic-bezier(0.42, 0, 0.58, 1.0) , starts transitioning slowly, speeds up, and then slows down again.
An author-defined cubic-Bezier curve, where the p1 and p3 values must be in the range of 0 to 1.
Displays the transition along n stops along the transition, displaying each stop for equal lengths of time. For example, if n is 5, there are 5 steps. Whether the transition holds temporarily at 0%, 20%, 40%, 60% and 80%, on the 20%, 40%, 60%, 80% and 100%, or makes 5 stops between the 0% and 100% along the transition, or makes 5 stops including the 0% and 100% marks (on the 0%, 25%, 50%, 75%, and 100%) depends on which of the following jump terms is used:
Denotes a left-continuous function, so that the first jump happens when the transition begins;
Denotes a right-continuous function, so that the last jump happens when the animation ends;
There is no jump on either end. Instead, holding at both the 0% mark and the 100% mark, each for 1/n of the duration
Includes pauses at both the 0% and 100% marks, effectively adding a step during the transition time.
Equal to steps(1, jump-start)
Accessibility concerns
Some animations can be helpful such as to guide users to understand what actions are expected, to show relationships within the user interface, and to inform users as to what actions have occurred. Animations can help reduce cognitive load, prevent change blindness, and establish better recall in spatial relationships. However, some animations can be problematic for people with cognitive concerns such as Attention Deficit Hyperactivity Disorder (ADHD) and certain kinds of motion can be a trigger for Vestibular disorders, epilepsy, and migraine and Scotopic sensitivity.
Consider providing a mechanism for pausing or disabling animation, as well as using the Reduced Motion Media Query to create a complimentary experience for users who have expressed a preference for no animated experiences.
Formal definition
Formal syntax
transition-timing-function =
#
=
linear |
|
|
=
linear( )
=
ease |
ease-in |
ease-out |
ease-in-out |
cubic-bezier( , , , )
=
step-start |
step-end |
steps( [, ]? )
=
[ ]#
=
jump-start |
jump-end |
jump-none |
jump-both |
start |
end
=
&&
?
=
Examples
Cubic-Bezier examples
div class="parent"> div class="ease">easediv> div class="easein">ease-indiv> div class="easeout">ease-outdiv> div class="easeinout">ease-in-outdiv> div class="linear">lineardiv> div class="cb">cubic-bezier(0.2,-2,0.8,2)div> div>
.parent > .parent > div[class] width: 12em; min-width: 12em; margin-bottom: 4px; background-color: black; border: 1px solid red; color: white; transition-property: all; transition-duration: 7s; > .parent > div.box1 width: 90vw; min-width: 24em; background-color: magenta; color: yellow; border: 1px solid orange; transition-property: all; transition-duration: 2s; >
function updateTransition() const els = document.querySelectorAll(".parent > div[class]"); for (let i = 0; i els.length; i++) els[i].classList.toggle("box1"); > > const intervalID = setInterval(updateTransition, 10000);
.ease transition-timing-function: ease; > .easein transition-timing-function: ease-in; > .easeout transition-timing-function: ease-out; > .easeinout transition-timing-function: ease-in-out; > .linear transition-timing-function: linear; > .cb transition-timing-function: cubic-bezier(0.2, -2, 0.8, 2); >
Step examples
div class="parent"> div class="jump-start">jump-startdiv> div class="jump-end">jump-enddiv> div class="jump-both">jump-bothdiv> div class="jump-none">jump-nonediv> div class="step-start">step-startdiv> div class="step-end">step-enddiv> div>
.parent > .parent > div[class] width: 12em; min-width: 12em; margin-bottom: 4px; background-color: black; border: 1px solid red; color: white; transition-property: all; transition-duration: 7s; > .parent > div.box1 width: 90vw; min-width: 24em; background-color: magenta; color: yellow; border: 1px solid orange; transition-property: all; transition-duration: 2s; >
function updateTransition() const els = document.querySelectorAll(".parent > div[class]"); for (let i = 0; i els.length; i++) els[i].classList.toggle("box1"); > > const intervalID = setInterval(updateTransition, 10000);
.jump-start transition-timing-function: steps(5, jump-start); > .jump-end transition-timing-function: steps(5, jump-end); > .jump-none transition-timing-function: steps(5, jump-none); > .jump-both transition-timing-function: steps(5, jump-both); > .step-start transition-timing-function: step-start; > .step-end transition-timing-function: step-end; >
Specifications
Browser compatibility
BCD tables only load in the browser
See also
Found a content problem with this page?
This page was last modified on Jul 18, 2023 by MDN contributors.
Your blueprint for a better internet.
MDN
Support
Our communities
Developers
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998– 2023 by individual mozilla.org contributors. Content available under a Creative Commons license.