Copyright © 2011 W3C® (MIT, ERCIM, Keio), All Rights Reserved. W3C liability, trademark and document use rules apply.
CSS Transitions allows property changes in CSS values to occur smoothly over a specified duration.
This is a public copy of the editors' draft. It is provided for discussion only and may change at any moment. Its publication here does not imply endorsement of its contents by W3C. Don't cite this document other than as work in progress.
The (archived) public mailing list www-style@w3.org (see instructions) is preferred for discussion of this specification. When sending e-mail, please put the text “css3-transitions” in the subject, preferably like this: “[css3-transitions] …summary of comment…”
This document was produced by the CSS Working Group (part of the Style Activity).
This document was produced by a group operating under the 5 February 2004 W3C Patent Policy. W3C maintains a public list of any patent disclosures made in connection with the deliverables of the group; that page also includes instructions for disclosing a patent. An individual who has actual knowledge of a patent which the individual believes contains Essential Claim(s) must disclose the information in accordance with section 6 of the W3C Patent Policy.
The list of changes made to this specification is available.
This section is not normative.
This document introduces new CSS features to enable implicit transitions, which describe how CSS properties can be made to change smoothly from one value to another over a given duration.
Normally when the value of a CSS property changes, the rendered result is instantly updated, with the affected elements immediately changing from the old property value to the new property value. This section describes a way to specify transitions using new CSS properties. These properties are used to animate smoothly from the old state to the new state over time.
For example, suppose that transitions of one second have been defined
on the ‘left
’ and ‘background-color
’ properties. The
following diagram illustrates the effect of updating those properties on
an element, in this case moving it to the right and changing the
background from red to blue. This assumes other transition parameters
still have their default values.
Transitions of ‘left
’ and ‘background-color
’
Transitions are a presentational effect. The computed value of a property transitions over time from the old value to the new value. Therefore if a script queries the computed style of a property as it is transitioning, it will see an intermediate value that represents the current animated value of the property.
Only animatable CSS properties can be transitioned. See the table at the end of this document for a list of properties that are animatable.
The transition for a property is defined using a number of new properties. For example:
div { transition-property: opacity; transition-duration: 2s; }The above example defines a transition on the ‘
opacity
’
property that, when a new value is assigned to it, will cause a smooth
change between the old value and the new value over a period of two
seconds.Each of the transition properties accepts a comma-separated list, allowing multiple transitions to be defined, each acting on a different property. In this case, the individual transitions take their parameters from the same index in all the lists. For example:
div { transition-property: opacity, left; transition-duration: 2s, 4s; }This will cause the ‘
opacity
’ property to transition over a
period of two seconds and the left property to transition over a period of
four seconds.In the case where the list of values in transition properties do not have the same length, the list is repeated as a whole in order to provide necessary values.
Issue: Are the lists repeated to all be the length of the
longest list, or are they repeated or truncated to match the length of the
‘transition-property
’ list?
div { transition-property: opacity, left, top, width; transition-duration: 2s, 1s; }The above example defines a transition on the ‘
opacity
’
property of 2 seconds duration, a transition on the ‘left
’
property of 1 second duration, a transition on the ‘top
’
property of 2 seconds duration and a transition on the ‘width
’
property of 1 second duration.transition-property
’ Property The ‘transition-property
’ property
specifies the name of the CSS property to which the transition is applied.
Name: | transition-property |
Value: | none | all | [ <IDENT> ] [ ‘, ’ <IDENT> ]*
|
Initial: | all |
Applies to: | all elements, :before and :after pseudo elements |
Inherited: | no |
Percentages: | N/A |
Media: | visual |
Computed value: | Same as specified value. |
A value of ‘none
’ means that no
property will transition. A value of ‘all
’ means that every property that is able to
undergo a transition will do so. Otherwise, a list of properties to be
transitioned is given.
If one of the identifiers listed is not a recognized property name or
is not an animatable property, the implementation must still start
transitions on the animatable properties in the list using the duration,
delay, and timing function at their respective indices in the lists for
‘transition-duration
’, ‘transition-delay
’, and ‘transition-timing-function
’. In other
words, unrecognized or non-animatable properties must be kept in the list
to preserve the matching of indices.
Are ‘all
’,
‘none
’, ‘inherit
’, and ‘initial
’ allowed as items in a list of
identifiers (of length greater than one)?
If one of the identifiers listed is a shorthand property, implementations must start transitions for any of its longhand sub-properties that are animatable, using the duration, delay, and timing function at the index corresponding to the shorthand.
If a property is specified multiple times in the value of ‘transition-property
’ (either on its own or
via a shorthand that contains it), then the transition that starts uses
the duration, delay, and timing function at the index corresponding to the
last occurrence of the property.
transition-duration
’ Property The ‘transition-duration
’ property
defines the length of time that a transition takes.
Name: | transition-duration |
Value: | <time> [, <time>]* |
Initial: | 0s |
Applies to: | all elements, :before and :after pseudo elements |
Inherited: | no |
Percentages: | N/A |
Media: | interactive |
Computed value: | Same as specified value. |
This property specifies how long the transition from the old value to
the new value should take. By default the value is ‘0s
’, meaning that the transition is immediate (i.e.
there will be no animation). A negative value for transition-duration is treated as
‘0s
’.
transition-timing-function
’
Property The ‘transition-timing-function
’
property describes how the intermediate values used during a transition
will be calculated. It allows for a transition to change speed over its
duration. These effects are commonly called easing functions. In
either case, a mathematical function that provides a smooth curve is used.
Timing functions are either defined as a stepping function or a cubic bezier curve. The timing function takes as its input the current elapsed percentage of the transition duration and outputs a percentage that determines how close the transition is to its goal state.
A stepping function is defined by a number that divides the domain of operation into equally sized intervals. Each subsequent interval is a equal step closer to the goal state. The function also specifies whether the change in output percentage happens at the start or end of the interval (in other words, if 0% on the input percentage is the point of initial change).
Step timing functions
A cubic
bezier curve is defined by four control points, P0 through
P3 (see Figure 1). P0 and P3 are always
set to (0,0) and (1,1). The ‘transition-timing-function
’
property is used to specify the values for points P1 and
P2. These can be set to preset values using the keywords listed
below, or can be set to specific values using the ‘cubic-bezier
’ function. In the ‘cubic-bezier
’ function, P1
and P2 are each specified by both an X and Y value.
Bezier Timing Function Control Points
Name: | transition-timing-function |
Value: | ease | linear | ease-in | ease-out | ease-in-out | step-start | step-end | steps(<integer>[, start | end ]) | cubic-bezier(<number>, <number>, <number>, <number>) [, ease | linear | ease-in | ease-out | ease-in-out | step-start | step-end | steps(<number>[, start | end ]) | cubic-bezier(<number>, <number>, <number>, <number>)]* |
Initial: | ease |
Applies to: | all elements, :before and :after pseudo elements |
Inherited: | no |
Percentages: | N/A |
Media: | interactive |
Computed value: | Same as specified value. |
The timing functions have the following definitions.
start
’ or ‘end
’, and specifies the point at which the
change of values occur within the interval. If the second parameter is
omitted, it is given the value ‘end
’.
transition-delay
’ Property The ‘transition-delay
’ property defines
when the transition will start. It allows a transition to begin execution
some some period of time from when it is applied. A ‘transition-delay
’ value of
‘0s
’ means the transition will execute
as soon as the property is changed. Otherwise, the value specifies an
offset from the moment the property is changed, and the transition will
delay execution by that offset.
If the value for ‘transition-delay
’ is a negative
time offset then the transition will execute the moment the property is
changed, but will appear to have begun execution at the specified offset.
That is, the transition will appear to begin part-way through its play
cycle. In the case where a transition has implied starting values and a
negative ‘transition-delay
’, the starting
values are taken from the moment the property is changed.
Name: | transition-delay |
Value: | <time> [, <time>]* |
Initial: | 0s |
Applies to: | all elements, :before and :after pseudo elements |
Inherited: | no |
Percentages: | N/A |
Media: | interactive |
Computed value: | Same as specified value. |
transition
’ Shorthand Property The ‘transition
’ shorthand property
combines the four properties described above into a single property.
Note that order is important in this property. The first value that can be parsed as a time is assigned to the transition-duration. The second value that can be parsed as a time is assigned to transition-delay.
An alternative proposal is to accept the font shorthand approach of using a "/" character between the values of the same type. eg. 2s/4s would mean a duration of 2 seconds and a delay of 4 seconds.
Name: | transition |
Value: | [<‘transition-property ’> ||
<‘transition-duration ’> ||
<‘transition-timing-function ’> ||
<‘transition-delay ’> [, [<‘transition-property ’> ||
<‘transition-duration ’> ||
<‘transition-timing-function ’> ||
<‘transition-delay ’>]]*
|
Initial: | see individual properties |
Applies to: | all elements, :before and :after pseudo elements |
Inherited: | no |
Percentages: | N/A |
Media: | interactive |
Computed value: | Same as specified value. |
When the computed value of an animatable property changes,
implementations must decide what transitions to start based on the values
of the ‘transition-property
’, ‘transition-duration
’, ‘transition-timing-function
’, and ‘transition-delay
’ properties at the time
the animatable property would first have its new computed value.
li { transition: background-color linear 1s; background: blue; } li:hover { transition-duration: 2s; background-color: green; }
When a list item with these style rules enters the :hover state, the
computed ‘transition-duration
’ at the time that
‘background-color
’ would have its
new value (‘green
’) is
‘2s
’, so the transition from
‘blue
’ to ‘green
’ takes 2 seconds. However, when the
list item leaves the :hover state, the transition from ‘green
’ to ‘blue
’ takes 1 second.
Since this specification does not define when computed values change, and thus what changes to computed values are considered simultaneous, authors should be aware that changing any of the transition properties a small amount of time after making a change that might transition can result in behavior that varies between implementations, since the changes might be considered simultaneous in some implementations but not others.
Say something about simulaneity
Once the transition of a property has started, it must continue running
based on the original timing function, duration, and delay, even if the
‘transition-timing-function
’, ‘transition-duration
’, or ‘transition-delay
’ property changes before
the transition is complete. However, if the ‘transition-property
’ property changes such
that the transition would not have started, the transition must stop (and
the property must immediately change to its final value).
Implementations must not start a transition when the computed value of a property changes as a result of declarative animation (as opposed to scripted animation).
Implementations also must not start a transition when the computed value changes because it is inherited (directly or indirectly) from another element that is transitioning the same property.
A common type of transition effect is when a running transition is interrupted and the property is reset to its original value. An example is a hover effect on an element, where the pointer enters and exits the element before the effect has completed. If the outgoing and incoming transitions are executed using their specified durations and timing functions, the resulting effect can be distractingly asymmetric. Instead, the expected behavior is that the new transition should be the reverse of what has already executed.
If a running transition with duration T, executing so far for duration TE, from state A, to state B, is interrupted by a property change that would start a new transition back to state A, and all the transition attributes are the same (duration, delay and timing function), then the new transition must reverse the effect. The new transition must:
For example, suppose there is a transition with a duration of two seconds. If this transition is interrupted after 0.5 seconds and the property value assigned to the original value, then the new transition effect will be the reverse of the original, as if it had begun 1.5 seconds in the past.
Note that by using the defined from and to states for the reversing transition, it is also possible that it may reverse again, if interrupted; for example, if the transition reversing to state A was again interrupted by a property change to state B.
Issue: This introduces the concept of reversing a timing function, which the spec has otherwise resisted doing, and also introduces a discontinuity between transitions that have almost completed (which get automatically reversed and thus have their timing function reversed) and transitions that have fully completed (where the reversal doesn't lead to the timing function being reversed). An alternative proposal that avoids this is to follow the normal timing function algorithm, except multiply the duration (and also shorten any negative delay) by the (output) value of the transition timing function of the incomplete transition at the time it was interrupted, and, to account for multiple reverses in sequence, to divide by the shortening applied to the transition being interrupted. For more details see this thread: November 2009 part, December 2009 part, January 2010 part.
The completion of a CSS Transition generates a corresponding DOM Event. An event is fired for each property that undergoes a transition. This allows a content developer to perform actions that synchronize with the completion of a transition.
Each event provides the name of the property the transition is associated with as well as the duration of the transition.
The TransitionEvent
interface provides specific
contextual information associated with transitions.
interface TransitionEvent : Event { readonly attribute DOMString propertyName; readonly attribute float elapsedTime; void initTransitionEvent(in DOMString typeArg, in boolean canBubbleArg, in boolean cancelableArg, in DOMString propertyNameArg, in float elapsedTimeArg); };
propertyName
of
type DOMString
, readonly
elapsedTime
of
type float
, readonly
initTransitionEvent
initTransitionEvent
method is
used to initialize the value of a TransitionEvent
created through the DocumentEvent
interface. This method may only be called before the
TransitionEvent
has been dispatched via the
dispatchEvent
method, though it may be called multiple
times during that phase if necessary. If called multiple times, the
final invocation takes precedence.
typeArg
of type
DOMString
canBubbleArg
of type
boolean
cancelableArg
of type
boolean
propertyNameArg
of type
DOMString
Event
elapsedTimeArg
of type
float
There is one type of transition event available.
transitionend
’ event
occurs at the completion of the transition. In the case where a
transition is removed before completion, such as if the
transition-property is removed, then the event will not fire.
The following describes how each property type undergoes transition or animation.
floor()
.
background-position
’ in an example of
a property that would transition between lists of different lengths). If
the property does not allow extending its list, then no interpolation
will occur.
Issue: Need to describe handling of out-of-range values that can result from cubic-bezier(). Clamping values to the allowed range is probably the best solution.
Property Name | Type |
---|---|
background-color | color |
background-position | percentage, length |
border-bottom-color | color |
border-bottom-width | length |
border-color This is a shorthand, so probably shouldn't be here. | color |
border-left-color | color |
border-left-width | length |
border-right-color | color |
border-right-width | length |
border-spacing | length |
border-top-color | color |
border-top-width | length |
border-width This is a shorthand, so probably shouldn't be here. | length |
bottom | length, percentage |
clip | rectangle |
color | color |
crop This is not a standard property. | rectangle |
font-size | length, percentage |
font-weight | number It's not that simple. |
grid-* | various |
height | length, percentage |
left | length, percentage |
letter-spacing | length |
line-height | number, length, percentage |
margin-bottom | length |
margin-left | length |
margin-right | length |
margin-top | length |
max-height | length, percentage |
max-width | length, percentage |
min-height | length, percentage |
min-width | length, percentage |
opacity | number |
outline-color | color |
outline-offset | integer |
outline-width | length |
padding-bottom | length |
padding-left | length |
padding-right | length |
padding-top | length |
right | length, percentage |
text-indent | length, percentage |
text-shadow | shadow |
top | length, percentage |
vertical-align | keywords, length, percentage |
visibility | visibility |
width | length, percentage |
word-spacing | length, percentage |
z-index | integer |
zoom This is not a standard property. | number |
This list omits the following properties that Gecko can animate, and which likely should be included: background-size, border-*-radius, box-shadow, column-count, column-gap, column-rule-color, column-rule-width, column-width, font-size-adjust, font-stretch, marker-offset, text-decoration-color, transform.
All properties defined as animatable in the SVG specification, provided they are one of the property types listed above.
Property | Values | Initial | Applies to | Inh. | Percentages | Media |
---|---|---|---|---|---|---|
transition | [<‘transition-property’> || <‘transition-duration’> || <‘transition-timing-function’> || <‘transition-delay’> [, [<‘transition-property’> || <‘transition-duration’> || <‘transition-timing-function’> || <‘transition-delay’>]]* | see individual properties | all elements, :before and :after pseudo elements | no | N/A | interactive |
transition-delay | <time> [, <time>]* | 0s | all elements, :before and :after pseudo elements | no | N/A | interactive |
transition-duration | <time> [, <time>]* | 0s | all elements, :before and :after pseudo elements | no | N/A | interactive |
transition-property | none | all | [ <IDENT> ] [ ‘,’ <IDENT> ]* | all | all elements, :before and :after pseudo elements | no | N/A | visual |
transition-timing-function | ease | linear | ease-in | ease-out | ease-in-out | step-start | step-end | steps(<integer>[, start | end ]) | cubic-bezier(<number>, <number>, <number>, <number>) [, ease | linear | ease-in | ease-out | ease-in-out | step-start | step-end | steps(<number>[, start | end ]) | cubic-bezier(<number>, <number>, <number>, <number>)]* | ease | all elements, :before and :after pseudo elements | no | N/A | interactive |