CSS transitions

Quick reference for CSS transitions: which properties can interpolate, why auto is not a length, and why discrete properties should not be over-generalized.

参考页正文目前为英文。

A transition interpolates a property from an old used value to a new one when that property changes. It needs a non-zero transition-duration; the initial duration is 0s, so writing only transition-property does not produce a visible transition. The usual interpolable properties include colors, opacity, transforms, and lengths with definite values.

Not every property interpolates that way. auto is not a length, so a height change between auto and 8rem is not the same as a change between 4rem and 8rem. Discrete properties such as display do not have a midpoint in the length sense. Newer CSS can flip some discrete values with transition-behavior: allow-discrete, but that is not a reason to say every engine animates display, or that height can never transition.

Examples are complete HTML and CSS fragments. They were parsed with PostCSS and a selector AST parser for syntax and structure. They were not rendered in a browser here, and this is not a visual timing test. Use prefers-reduced-motion, on the media queries page, when the motion is decorative.

Official sources: MDN and the CSS specification. Fragments were checked with PostCSS 8 for syntax and structure. That is not a CSS conformance suite, and this is not a browser render or visual test.

CSS reference catalog → · CSS tutorial overview →

Transitions need a duration and an interpolable property

The shorthand is transition: property duration timing-function delay. The longhands are transition-property (initial all), transition-duration (initial 0s), transition-timing-function (initial ease), and transition-delay (initial 0s). Without a duration, there is nothing to interpolate over time.

color, opacity, transform, and definite lengths are the common interpolable cases. The property you name must actually change; transitioning color does nothing if only the margin changed.

Syntax or propertyInitial / defaultInheritedApplies toNotes
transition-propertyallnoall elementsWhich properties transition. all is the initial value, but a 0s duration still means no visible transition. none disables transitions.
transition-duration0snoall elementsThe interpolation time. A non-zero duration is required for a visible transition.
transition-timing-functioneasenoall elementsHow the interpolation progresses. The initial value is ease, not linear.
transition-delay0snoall elementsA wait before the interpolation starts.
transitionsee longhandsnoall elementsShorthand for the longhands above. A trailing time is a duration unless a duration is already present, in which case it is a delay.

Color and opacity can interpolate when a duration is set

.fade { color: #0B5853; opacity: 1; transition: color 0.2s ease, opacity 0.2s ease; }
.fade:hover { color: #1A1A1A; opacity: 0.7; }

Two interpolable properties share a 0.2s ease transition. The initial duration is 0s, so the duration has to be written.

  • The :hover rule is only a change trigger in the stylesheet. This is not a pointer or timing measurement.
html
<button class="fade" type="button">Save</button>
<style>
.fade { font-family: sans-serif; color: #0B5853; background: #F3EFE4; border: 1px solid #0B5853; opacity: 1; transition: color 0.2s ease, opacity 0.2s ease; }
.fade:hover { color: #1A1A1A; opacity: 0.7; }
</style>

What this markup means

transition lists color and opacity, each with a 0.2s ease duration. Both properties interpolate between specified values: two colors, and two numbers in 0–1. The button remains in the document. If the duration were omitted, the initial 0s would apply and the same property changes would snap. prefers-reduced-motion, on the media queries page, is the place to turn decorative motion off.

Related: Colors and opacity, Media queries (reduced motion)

MDN documentation → · CSS specification →

auto and discrete values are not length interpolation

A definite height, such as 4rem to 8rem, can interpolate as a length. height: auto is a content-based size, not a length, so a transition between auto and 8rem is not the same kind of interpolation. Saying “height cannot transition” is too broad; saying “auto behaves like 8rem” is too loose.

display and some other discrete properties do not have intermediate values in the interpolation tables of CSS Transitions Level 1. Visibility is historically discrete as well. CSS Transitions Level 2 defines transition-behavior: allow-discrete so a discrete value can flip at a defined point, often together with @starting-style. That is an explicit opt-in, not a promise that display now animates smoothly in every engine, and this reference does not treat it as a default.

Syntax or propertyInitial / defaultNotes
height (definite vs auto)autoDefinite lengths can interpolate. auto cannot be treated as a start or end length in the same way. The Sizing page covers percentage and auto height in layout; this note is only about transitioning.
display / visibilitysee those propertiesDiscrete in the Level 1 transition model. Do not assume a smooth fade. Do not assume they can never change under a transition either: allow-discrete is a later, explicit mechanism.

A definite height and an auto height are different cases

.definite { height: 4rem; transition: height 0.2s ease; }
.definite:hover { height: 8rem; }
.auto { height: auto; transition: height 0.2s ease, display 0.2s allow-discrete; }

The first box transitions between two lengths. The second keeps height: auto and names allow-discrete only to show an explicit discrete opt-in, not a universal engine behaviour.

  • allow-discrete is written so the syntax is visible. This is not a support claim and not a demo that display now fades.
html
<div class="definite">4rem to 8rem</div>
<div class="auto">height: auto</div>
<style>
.definite, .auto { width: 16rem; font-family: sans-serif; background: #F3EFE4; border: 1px solid #0B5853; }
.definite { height: 4rem; transition: height 0.2s ease; }
.definite:hover { height: 8rem; }
.auto { height: auto; transition: height 0.2s ease, display 0.2s allow-discrete; }
</style>

What this markup means

The first rule transitions height between 4rem and 8rem, two definite lengths. The second rule also names height, but the used value is auto, which is not a length to interpolate toward 8rem. The same rule includes display 0.2s allow-discrete as the explicit Level 2 opt-in for a discrete property. That keyword is not implied by a normal transition, and this page does not claim that every browser will animate display or that auto height will start interpolating if the keyword is present.

Related: Sizing and overflow, Display

MDN documentation → · CSS specification →