CSS Transitions Module Level 3

[LONGSTATUS] [DATE]

This version:
http://dev.w3.org/csswg/css3-transitions/
Latest version:
[LATEST]
Previous version:
http://www.w3.org/TR/2009/WD-css3-transitions-20090320/
Editors:
Dean Jackson (Apple Inc)
David Hyatt (Apple Inc)
Chris Marrin (Apple Inc)

Abstract

CSS Transitions allows property changes in CSS values to occur smoothly over a specified duration.

Status of this document

Table of contents

Introduction

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.

Transitions

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:

Example(s):

  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:

Example(s):

  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.

The 'transition-property' Property

The 'transition-property' property specifies the name of the CSS property to which the transition is applied.

We may ultimately want to support a keypath syntax for this property. A keypath syntax would enable different transitions to be specified for components of a property. For example the blur of a shadow could have a different transition than the color of a shadow.
Name: transition-property
Value: none | all | [ <IDENT> ] [ ',' <IDENT> ]*
Initial: all
Applies to: block-level and inline-level 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.

We need to generate a list of properties that can be transitioned.
Is "none" even a useful value if the initial value is "all"? The syntax is more elegant if transition-duration defaults to 0 and this property defaults to "all", but another option is to default this property to "none" and duration to something reasonable, e.g., 250ms. This would force an author to specify transition-property in the shorthand all the time though.

The 'transition-duration' Property

The 'transition-duration' property defines the length of time that a transition takes.

Name: transition-duration
Value: <time> [, <time>]*
Initial: 0
Applies to: block-level and inline-level elements
Inherited: no
Percentages: N/A
Media: visual
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 '0', meaning that the transition is immediate (i.e. there will be no animation). A negative value for transition-duration is treated as '0'.

The '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.

The timing function is specified using a cubic bezier curve, which 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.

The timing function is a
    smooth curve from point P0 = (0,0) to point P3 = (1,1). The
    length and orientation of the line segment P0-P1 determines
    the tangent and the curvature of the curve at P0 and the
    line segment P2-P3 does the same at P3.

Timing Function Control Points

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.

Name: transition-timing-function
Value: ease | linear | ease-in | ease-out | ease-in-out | cubic-bezier(<number>, <number>, <number>, <number>) [, ease | linear | ease-in | ease-out | ease-in-out | cubic-bezier(<number>, <number>, <number>, <number>)]*
Initial: ease
Applies to: block-level and inline-level elements
Inherited: no
Percentages: N/A
Media: visual
Computed value: Same as specified value.

The timing functions have the following definitions.

ease
The ease function is equivalent to cubic-bezier(0.25, 0.1, 0.25, 1.0).
linear
The linear function is equivalent to cubic-bezier(0.0, 0.0, 1.0, 1.0).
ease-in
The ease-in function is equivalent to cubic-bezier(0.42, 0, 1.0, 1.0).
ease-out
The ease-out function is equivalent to cubic-bezier(0, 0, 0.58, 1.0).
ease-in-out
The ease-in-out function is equivalent to cubic-bezier(0.42, 0, 0.58, 1.0)
cubic-bezier
Specifies a cubic-bezier curve. The four values specify points P1 and P2 of the curve as (x1, y1, x2, y2). All values must be in the range [0, 1] or the definition is invalid.

The '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 '0' 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: 0
Applies to: block-level and inline-level elements
Inherited: no
Percentages: N/A
Media: visual
Computed value: Same as specified value.

The 'transition' Shorthand Property

The 'transition' shorthand property combines the four properties described above into a single property.

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: block-level and inline-level elements
Inherited: no
Percentages: N/A
Media: visual
Computed value: Same as specified value.
What should happen when a value is changed midway through a transition? One option is to simply begin a new transition from the current position. However things get interesting when from/to values are being flipped. For example you'd like symmetry on fade-in/fade-out hover effects if the user rolls over the object and rolls out before the animation finishes. This implies that there needs to be a convenient and straightforward rule for running transitions in reverse under certain circumstances. This could be controlled via a new property, or could perhaps be done simply by detecting when the from/to values are flipped.

Transition Events

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.

Interface TransitionEvent

The TransitionEvent interface provides specific contextual information associated with transitions.

IDL Definition
  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);
  };
  
Attributes
propertyName of type DOMString, readonly
The name of the CSS property associated with the transition.
elapsedTime of type float, readonly
The amount of time the transition has been running, in seconds, when this event fired. Note that this value is not affected by the value of transition-delay.
Methods
initTransitionEvent
The 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.
Parameters
typeArg of type DOMString
Specifies the event type.
canBubbleArg of type boolean
Specifies whether or not the event can bubble.
cancelableArg of type boolean
Specifies whether or not the event's default action can be prevented.
propertyNameArg of type DOMString
Specifies the name of the property associated with the Event
elapsedTimeArg of type float
Specifies the amount of time, in seconds, the transition has been running at the time of initialization.
No Return Value
No Exceptions

There is one type of transition event available.

transitionend
The 'transitionend' event occurs at the completion of the transition

Animation of property types

The following describes how each property type undergoes transition or animation.

Animatable properties

Properties from CSS

Property Name Type
background-colorcolor
background-imageonly gradients
background-positionpercentage, length
border-bottom-colorcolor
border-bottom-widthlength
border-colorcolor
border-left-colorcolor
border-left-widthlength
border-right-colorcolor
border-right-widthlength
border-spacinglength
border-top-colorcolor
border-top-widthlength
border-widthlength
bottomlength, percentage
colorcolor
croprectangle
font-sizelength, percentage
font-weightnumber
grid-*various
heightlength, percentage
leftlength, percentage
letter-spacinglength
line-heightnumber, length, percentage
margin-bottomlength
margin-leftlength
margin-rightlength
margin-toplength
max-heightlength, percentage
max-widthlength, percentage
min-heightlength, percentage
min-widthlength, percentage
opacitynumber
outline-colorcolor
outline-offsetinteger
outline-widthlength
padding-bottomlength
padding-leftlength
padding-rightlength
padding-toplength
rightlength, percentage
text-indentlength, percentage
text-shadowshadow
toplength, percentage
vertical-alignkeywords, length, percentage
visibilityvisibility
widthlength, percentage
word-spacinglength, percentage
z-indexinteger
zoomnumber

Properties from SVG

Property NameType
stop-colorcolor
stop-opacityfloat
fillpaint server
fill-opacityfloat
strokepaint server
stroke-dasharraylist of numbers
stroke-dashoffsetnumber
stroke-miterlimitnumber
stroke-opacityfloat
stroke-widthfloat
viewport-fillcolor
viewport-fill-opacitycolor

References

Normative references

Other references

Property index

Index