Spec-Zone.ru › CSS

`transition` CSS property

Baseline Widely available

This feature is well established and works across many devices and browser versions. It's been available across browsers since September 2015.

The transition CSS shorthand property for transition-property, transition-duration, transition-timing-function, transition-delay, and transition-behavior.

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.

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: black;
  padding: 1rem;
  border-radius: 0.5rem;
  font: 1em monospace;
  width: 100%;
  transition: margin-right 2s;
}

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

Constituent properties

This property is a shorthand for the following CSS properties:

  • transition-behavior
  • transition-delay
  • transition-duration
  • transition-property
  • transition-timing-function

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:

  • The special value none, which specifies that no transitions will occur on this element. This is the default value.
  • One or more single-property transitions, separated by commas.

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

  • zero or one value representing the property or properties to which the transition should apply. This can be set as:
    • A <custom-ident> representing a single property.
    • The special value all, which specifies that the transition will be applied to all properties that change as the element changes state.
    • No value, in which case a value of all will be inferred and the specified transition will still apply to all changing properties.
  • zero or one <easing-function> value representing the easing function to use
  • zero, one, or two <time> values. The first value that can be parsed as a time is assigned to the transition-duration, and the second value that can be parsed as a time is assigned to transition-delay.
  • zero or one value declaring whether to start transitions for properties whose animation behavior is discrete. The value, if present, is either the keyword allow-discrete or the keyword normal.

If you specify all as the transition property for one single-property transition, but then specify subsequent single-property transitions with <custom-ident> 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

Initial value as each of the properties of the shorthand:
  • transition-delay
  • transition-duration
  • transition-property
  • transition-timing-function
  • transition-behavior
Applies to all elements, ::before and ::after <a href="/en-US/docs/Web/CSS/Reference/Selectors/Pseudo-elements">pseudo-elements</a>
Inherited no
Computed value as each of the properties of the shorthand:
  • transition-delay
  • transition-duration
  • transition-property
  • transition-timing-function
  • transition-behavior
Animation type Not animatable

Formal syntax

transition = 
  <single-transition>#

<single-transition> = 
  [ none | <single-transition-property> ] ||
  <time> ||
  <easing-function> ||
  <time> ||
  <transition-behavior-value>

<single-transition-property> = 
  all |
  <custom-ident>

<easing-function> = 
  <linear-easing-function> |
  <cubic-bezier-easing-function> |
  <step-easing-function>

<transition-behavior-value> = 
  normal |
  allow-discrete

<linear-easing-function> = 
  linear |
  <linear()>

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

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

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

<cubic-bezier()> = 
  cubic-bezier( [ <number [0,1]>, <number> ]#{2} )

<steps()> = 
  steps( <integer>, <step-position>? )

<integer> = 
  <number-token>

<step-position> = 
  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 <time> 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 Module Level 1
# transition-shorthand-property

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Opera Safari Chrome Android Firefox for Android Opera Android Safari on iOS Samsung Internet WebView Android WebView on iOS
transition
26
1
12
12
16Before Firefox 57, transitions do not work when transitioning from a text-shadow with a color specified to a text-shadow without a color specified (see bug 726550).
Before Firefox 57, cancelling a filling animation (for example, with animation-fill-mode: forwards set) can trigger a transition set on the same element, although only once (see bug 1192592 and these test cases for more information).
Before Firefox 57, the background-position property can't be transitioned between two values containing different numbers of <position> values, for example background-position: 10px 10px; and background-position: 20px 20px, 30px 30px; (see bug 1390446).
49
4–preview
12.1
15
10.1–15
9
3.1
26
18
16Before Firefox for Android 57, transitions do not work when transitioning from a text-shadow with a color specified to a text-shadow without a color specified (see bug 726550).
Before Firefox for Android 57, cancelling a filling animation (for example, with animation-fill-mode: forwards set) can trigger a transition set on the same element, although only once (see bug 1192592 and these test cases for more information).
Before Firefox for Android 57, the background-position property can't be transitioned between two values containing different numbers of <position> values, for example background-position: 10px 10px; and background-position: 20px 20px, 30px 30px; (see bug 1390446).
49
4
12.1
14
10.1–14
9
2
1.5
1.0
4.4
2
9
2
all
1
12
4
15
3.1
18
4
14
2
1.0
4.4
2
allow-discrete
37
12
4
24
10
37
4
24
10
3.0
37
10
ease
1
12
4
15
3.1
18
4
14
2
1.0
4.4
2
ease-in
1
12
4
15
3.1
18
4
14
2
1.0
4.4
2
ease-in-out
1
12
4
15
3.1
18
4
14
2
1.0
4.4
2
ease-out
1
12
4
15
3.1
18
4
14
2
1.0
4.4
2
gradients_can_animate
No
12–79
No
No
preview
No
No
No
No
No
No
No
linear
1
12
4
15
3.1
18
4
14
2
1.0
4.4
2
none
1
12
4
15
3.1
18
4
14
2
1.0
4.4
2
normal
37
12
4
24
10
37
4
24
10
3.0
37
10
step-end
1
12
4
15
3.1
18
4
14
2
1.0
4.4
2
step-start
1
12
4
15
3.1
18
4
14
2
1.0
4.4
2
transition-behavior
117
117
129
103
17.4
117
129
78
17.4
24.0
117
17.4

See also

  • CSS transitions module
  • Using CSS transitions
  • TransitionEvent

© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/transition

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API