transition - CSS | MDN (original) (raw)

Baseline

Widely available *

Try it

transition: margin-right 2s;
transition: margin-right 2s 0.5s;
transition: margin-right 2s ease-in-out;
transition: margin-right 2s ease-in-out 0.5s;
transition:
  margin-right 2s,
  color 1s;
transition: all 1s ease-out;
<section id="default-example">
  <div id="example-element">Hover to see<br />the transition.</div>
</section>
#example-element {
  background-color: #e4f0f5;
  color: #000;
  padding: 1rem;
  border-radius: 0.5rem;
  font: 1em monospace;
  width: 100%;
  transition: margin-right 2s;
}

#default-example:hover > #example-element {
  background-color: #909;
  color: #fff;
  margin-right: 40%;
}

Transitions enable you to define the transition between two states of an element. Different states may be defined using pseudo-classes like :hover or :active or dynamically set using JavaScript.

Constituent properties

Syntax

/* Apply to 1 property */
/* property name | duration */
transition: margin-right 4s;

/* property name | duration | delay */
transition: margin-right 4s 1s;

/* property name | duration | easing function */
transition: margin-right 4s ease-in-out;

/* property name | duration | easing function | delay */
transition: margin-right 4s ease-in-out 1s;

/* property name | duration | behavior */
transition: display 4s allow-discrete;

/* Apply to 2 properties */
transition:
  margin-right 4s,
  color 1s;

/* Apply to all changed properties */
transition: all 0.5s ease-out allow-discrete;
transition: 200ms linear 50ms;

/* Global values */
transition: inherit;
transition: initial;
transition: revert;
transition: revert-layer;
transition: unset;

The transition property value is specified as one of the following:

Each single-property transition describes the transition that should be applied to a single property or all properties. It includes:

If you specify all as the transition property for one single-property transition, but then specify subsequent single-property transitions with values, those subsequent transitions will override the first one. For example:

transition:
  all 200ms,
  opacity 400ms;

In this case, all the properties that change as the element changes state will transition with a duration of 200ms except for opacity, which will take 400ms to transition.

See how things are handled when lists of property values aren't the same length. In short, extra transition descriptions beyond the number of properties actually being animated are ignored.

Formal definition

Formal syntax

transition =
#

=
none | [] ||
||
||

=
all |

=
|
|

=
linear |
<linear()>

=
ease |
ease-in |
ease-out |
ease-in-out |
<cubic-bezier()>

=
step-start |
step-end |
<steps()>

<linear()> =
linear( && {0,2} []# )

<cubic-bezier()> =
cubic-bezier( [ <number [0,1]> , []](/en-US/docs/Web/CSS/CSS%5FValues%5Fand%5FUnits/Value%5Fdefinition%5Fsyntax#brackets "Brackets: enclose several entities, combinators, and multipliers to transform them as a single component")#{2} )

<steps()> =
steps( , ? )

=
jump-start |
jump-end |
jump-none |
jump-both |
start |
end

Examples

Basic example

In this example, when the user hovers over the element, there is a half-second (500ms) delay before a two-second background-color transition occurs.

HTML

<a class="target">Hover over me</a>

CSS

We include two values. In the transition shorthand, the first <time> value is the transition-duration. The second time value is the transition-delay. Both default to 0s if omitted.

.target {
  font-size: 2rem;
  background-color: palegoldenrod;
  transition: background-color 2s 500ms;
}

.target:hover {
  background-color: darkorange;
}

Specifications

Specification
CSS Transitions # transition-shorthand-property

Browser compatibility

See also