transition CSS 属性是 {{ cssxref("transition-property") }}、{{ cssxref("transition-duration") }}、{{ cssxref("transition-timing-function") }} 和 {{ cssxref("transition-delay") }} 的一个简写属性

{{InteractiveExample("CSS Demo: transition")}}

```css interactive-example-choice transition: margin-right 2s;


```css interactive-example-choice transition: margin-right 2s 0.5s;

```css interactive-example-choice transition: margin-right 2s ease-in-out;


```css interactive-example-choice transition: margin-right 2s ease-in-out 0.5s;

```css interactive-example-choice transition: margin-right 2s, color 1s;


```css interactive-example-choice transition: all 1s ease-out;

```html interactive-example

Hover to see
the transition.


```css interactive-example #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%; }

过渡可以为一个元素在不同状态之间切换的时候定义不同的过渡效果。比如在不同的伪元素之间切换,像是 {{cssxref(":hover")}},{{cssxref(":active")}} 或者通过 JavaScript 实现的状态变化。

语法

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

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

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

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

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

/* Apply to all changed properties */
transition: all 0.5s ease-out;

/* Global values */
transition: inherit;
transition: initial;
transition: unset;

transition属性可以被指定为一个或多个 CSS 属性的过渡效果,多个属性之间用逗号进行分隔。

每个单属性转换都描述了应该应用于单个属性的转换(或特殊值allnone)。这包括:

  • 零或一个值,表示转换应适用的属性。这可能是以下任何一种:

    • 关键字none
    • 关键字all
    • 命名 CSS 属性的 {{cssxref("<custom-ident>")}} 。
  • 零或一个 {{cssxref("<single-transition-timing-function>")}} 值表示要使用的过渡函数

  • 零,一或两个 {{cssxref("<time>")}} 值。可以解析为时间的第一个值被分配给 {{cssxref("transition-duration")}},并且可以解析为时间的第二个值被分配给{{cssxref("transition-delay")}}。

请查看“如何处理”以了解当transition属性的值个数超过可以接收的值的个数时该如何处理。简而言之,当transition属性的值个数超过可以接收的值的个数时,多余的值都会被忽略掉,不再进行解析。

标准语法

{{csssyntax}}

示例

使用 CSS 过渡”一文中还包含了几个 CSS 过渡示例。