CSS Animations Level 1

Editor’s Draft, 5 April 2014

This version:
http://dev.w3.org/csswg/css3-animations/
Latest version:
http://www.w3.org/TR/css3-animations/
Editor’s Draft:
http://dev.w3.org/csswg/css3-animations/
Previous Versions:
http://www.w3.org/TR/2013/WD-css3-animations-20130219/
http://www.w3.org/TR/2012/WD-css3-animations-20120403/
Feedback:
www-style@w3.org with subject line “[css-animations] … message topic …”(archives)
Test Suite:
http://test.csswg.org/suites/css-animations-1/nightly-unstable/
Editors:
(Apple Inc.)
(Mozilla)
(Adobe)
Former Editors:
David Hyatt (Apple Inc.)
Chris Marrin (Apple Inc.)

Abstract

This CSS module describes a way for authors to animate the values of CSS properties over time, using keyframes. The behavior of these keyframe animations can be controlled by specifying their duration, number of repeats, and repeating behavior. CSS is a language for describing the rendering of structured documents (such as HTML and XML) on screen, on paper, in speech, etc.

Status of this document

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 “css-animations” in the subject, preferably like this: “[css-animations] …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.

Table of Contents

1 Introduction

This section is not normative

CSS Transitions [CSS3-TRANSITIONS] provide a way to interpolate CSS property values when they change as a result of underlying property changes. This provides an easy way to do simple animation, but the start and end states of the animation are controlled by the existing property values, and transitions provide little control to the author on how the animation progresses.

This proposal introduces defined animations, in which the author can specify the changes in CSS properties over time as a set of keyframes. Animations are similar to transitions in that they change the presentational value of CSS properties over time. The principal difference is that while transitions trigger implicitly when property values change, animations are explicitly executed when the animation properties are applied. Because of this, animations require explicit values for the properties being animated. These values are specified using animation keyframes, described below.

Many aspects of the animation can be controlled, including how many times the animation iterates, whether or not it alternates between the begin and end values, and whether or not the animation should be running or paused. An animation can also delay its start time.

2 Values

This specification follows the CSS property definition conventions from [CSS21]. Value types not defined in this specification are defined in CSS Level 2 Revision 1 [CSS21]. Other CSS modules may expand the definitions of these value types: for example [CSS3VAL], when combined with this module, expands the definition of the <length> value type as used in this specification.

In addition to the property-specific values listed in their definitions, all properties defined in this specification also accept the ‘initial’ and ‘inherit’ keyword as their property value. For readability it has not been repeated explicitly.

3 Animations

CSS Animations affect computed property values. This effect happens by adding a specified value to the CSS cascade ([CSS3CASCADE]) (at the level for CSS Animations) that will produce the correct computed value for the current state of the animation. As defined in [CSS3CASCADE], animations override all normal rules, but are overridden by !important rules.

If at one point in time there are multiple animations specifying behavior for the same property, the animation whose name occurs last in the value of animation-name will override the other animations at that point.

An animation does not affect the computed value before the application of the animation, before the animation delay has expired, and after the end of the animation.

Computation of animated property values

The diagram above shows how property values are computed. The intrinsic style is shown at the top of the diagram. The computed value is derived from intrinsic style at the times when an animation is not running and also when an animation is delayed (see below for specification of animation delay). During an animation, the computed style is derived from the animated value.

The start time of an animation is the latter of two moments: the time at which the style is resolved that specifies the animation, or the time the document’s load event is fired. Therefore, an animation specified in the document style sheet will begin at the document load. An animation specified on an element by modifying the style after the document has loaded will start when the style is resolved. That may be immediately in the case of a pseudo style rule such as hover, or may be when the scripting engine returns control to the browser (in the case of style applied by script).

An animation applies to an element if its name appears as one of the identifiers in the computed value of the animation-name property. Once an animation has started it continues until it ends or the animation-name is removed. The values used for the keyframes and animation properties are snapshotted at the time the animation starts. Changing them during the execution of the animation has no effect. Note also that changing the value of animation-name does not necessarily restart an animation (e.g., if a list of animations are applied and one is removed from the list, only that animation will stop; The other animations will continue). In order to restart an animation, it must be removed then reapplied.

The end of the animation is defined by the combination of the animation-duration, animation-iteration-count and animation-fill-mode properties.

div {
  animation-name: diagonal-slide;
  animation-duration: 5s;
  animation-iteration-count: 10;
}

@keyframes diagonal-slide {

  from {
    left: 0;
    top: 0;
  }

  to {
    left: 100px;
    top: 100px;
  }

}

This will produce an animation that moves an element from (0, 0) to (100px, 100px) over five seconds and repeats itself nine times (for a total of ten iterations).

Setting the display property to none will terminate any running animation applied to the element and its descendants. If an element has a display of none, updating display to a value other than none will start all animations applied to the element by the animation-name property, as well as all animations applied to descendants with display other than none.

While authors can use animations to create dynamically changing content, dynamically changing content can lead to seizures in some users. For information on how to avoid content that can lead to seizures, see Guideline 2.3: Seizures: Do not design content in a way that is known to cause seizures ([WCAG20]).

4 Keyframes

Keyframes are used to specify the values for the animating properties at various points during the animation. The keyframes specify the behavior of one cycle of the animation; the animation may iterate one or more times.

Keyframes are specified using a specialized CSS at-rule. A @keyframes rule consists of the keyword "@keyframes", followed by an identifier giving a name for the animation (which will be referenced using animation-name), followed by a set of style rules (delimited by curly braces).

The keyframe selector for a keyframe style rule consists of a comma-separated list of percentage values or the keywords from or to. The selector is used to specify the percentage along the duration of the animation that the keyframe represents. The keyframe itself is specified by the block of property values declared on the selector. The keyword from is equivalent to the value 0%. The keyword to is equivalent to the value 100%. Note that the percentage unit specifier must be used on percentage values. Therefore, 0 is an invalid keyframe selector.

If a 0% or from keyframe is not specified, then the user agent constructs a 0% keyframe using the computed values of the properties being animated. If a 100% or to keyframe is not specified, then the user agent constructs a 100% keyframe using the computed values of the properties being animated. If a keyframe selector specifies negative percentage values or values higher than 100%, then the keyframe will be ignored.

The keyframe declaration block for a keyframe rule consists of properties and values. Properties that are unable to be animated are ignored in these rules, with the exception of animation-timing-function, the behavior of which is described below. In addition, keyframe rule declarations qualified with !important are ignored.

Issue: Need to describe what happens if a property is not present in all keyframes.

The @keyframes rule that is used by an animation will be the last one encountered in sorted rules order that matches the name of the animation specified by the animation-name property. @keyframes rules do not cascade; therefore, an animation will never derive keyframes from more than one @keyframes rule.

Note: Since empty @keyframes rule are valid, they may hide the keyframes of those preceding animation definitions with a matching name.

To determine the set of keyframes, all of the values in the selectors are sorted in increasing order by time. If there are any duplicates, then the last keyframe specified inside the @keyframes rule will be used to provide the keyframe information for that time. There is no cascading within a @keyframes rule if multiple keyframes specify the same keyframe selector values.

If a property is not specified for a keyframe, or is specified but invalid, the animation of that property proceeds as if that keyframe did not exist. Conceptually, it is as if a set of keyframes is constructed for each property that is present in any of the keyframes, and an animation is run independently for each property.

@keyframes wobble {
  0% {
    left: 100px;
  }

  40% {
    left: 150px;
  }

  60% {
    left: 75px;
  }

  100% {
    left: 100px;
  }
}

Four keyframes are specified for the animation named "wobble". In the first keyframe, shown at the beginning of the animation cycle, the value of the left property being animated is 100px. By 40% of the animation duration, left has animated to 150px. At 60% of the animation duration, left has animated back to 75px. At the end of the animation cycle, the value of left has returned to 100px. The diagram below shows the state of the animation if it were given a duration of 10s.

Animation states specified by keyframes

The following is the grammar for the keyframes rule:

  keyframes_rule: KEYFRAMES_SYM S+ IDENT S* '{' S* keyframes_blocks '}' S*;

  keyframes_blocks: [ keyframe_selector '{' S* declaration? [ ';' S* declaration? ]* '}' S* ]* ;

  keyframe_selector: [ FROM_SYM | TO_SYM | PERCENTAGE ] S* [ ',' S* [ FROM_SYM | TO_SYM | PERCENTAGE ] S* ]*;

  @{K}{E}{Y}{F}{R}{A}{M}{E}{S}   {return KEYFRAMES_SYM;}
  {F}{R}{O}{M}                   {return FROM_SYM;}
  {T}{O}                         {return TO_SYM;}

4.1 Timing functions for keyframes

A keyframe style rule may also declare the timing function that is to be used as the animation moves to the next keyframe.

@keyframes bounce {

  from {
    top: 100px;
    animation-timing-function: ease-out;
  }

  25% {
    top: 50px;
    animation-timing-function: ease-in;
  }

  50% {
    top: 100px;
    animation-timing-function: ease-out;
  }

  75% {
    top: 75px;
    animation-timing-function: ease-in;
  }

  to {
    top: 100px;
  }

}

Five keyframes are specified for the animation named "bounce". Between the first and second keyframe (i.e., between 0% and 25%) an ease-out timing function is used. Between the second and third keyframe (i.e., between 25% and 50%) an ease-in timing function is used. And so on. The effect will appear as an element that moves up the page 50px, slowing down as it reaches its highest point then speeding up as it falls back to 100px. The second half of the animation behaves in a similar manner, but only moves the element 25px up the page.

A timing function specified on the to or 100% keyframe is ignored.

See the animation-timing-function property for more information.

4.2 The animation-name property

Name:animation-name
Value:<single-animation-name>#
Initial:none
Applies to:all elements, ::before and ::after pseudo-elements
Inherited:none
Media:visual
Computed value:As specified
Canonical order:per grammar
Percentages:N/A
Animatable:no

<single-animation-name> = none | <custom-ident>

The values of animation-name have the following meanings:

none
No keyframes are specified at all, so there will be no animation. Any other animations properties specified for this animation have no effect.
<custom-ident>
The animation will use the keyframes with the name specified by the <custom-ident>, if they exist. If no such keyframes exist, there is no animation.

4.3 The animation-duration property

The animation-duration property defines duration of a single animation cycle.

Name:animation-duration
Value:<time>#
Initial:0s
Applies to:all elements, ::before and ::after pseudo-elements
Inherited:no
Media:visual
Computed value:As specified
Canonical order:per grammar
Percentages:N/A
Animatable:no
<time>
The <time> specifies the length of time that an animation takes to complete one cycle. A negative <time> is invalid.

If the <time> is 0s, like the initial value, the keyframes of the animation have no effect, but the animation itself still occurs instantaneously. That is, animation-fill-mode applies as normal, filling backwards or forwards as appropriate, and animation events still fire.

4.4 The animation-timing-function property

The animation-timing-function property describes how the animation will progress over one cycle of its duration. See the transition-timing-function property [CSS3-TRANSITIONS] for a complete description of timing function calculation.

Name:animation-timing-function
Value:<single-timing-function>#
Initial:ease
Applies to:all elements, ::before and ::after pseudo-elements
Inherited:no
Media:visual
Computed value:As specified
Canonical order:per grammar
Percentages:N/A
Animatable:no

The values and meaning of <single-timing-function> are identical to those of <single-transition-timing-function> [CSS3-TRANSITIONS].

The timing function specified applies to each iteration of the animation, not the entire animation in full. For example, if an animation has animation-timing-function: ease-in-out; animation-iteration-count: 2;, it will ease in at the start, ease out as it approaches the end of its first iteration, ease in at the start of its second iteration, and ease out again as it approaches the end of the animation.

Note: Unlike other animation properties, animation-timing-function has an effect when specified on an individual keyframe. See for more detail on this.

4.5 The animation-iteration-count property

The animation-iteration-count property specifies the number of times an animation cycle is played. The initial value is 1, meaning the animation will play from beginning to end once. This property is often used in conjunction with an animation-direction value of alternate, which will cause the animation to play in reverse on alternate cycles.

Name:animation-iteration-count
Value:<single-animation-iteration-count>#
Initial:1
Applies to:all elements, ::before and ::after pseudo-elements
Inherited:no
Media:visual
Computed value:As specified
Canonical order:per grammar
Percentages:N/A
Animatable:no

<single-animation-iteration-count> = infinite | <number>

infinite
The animation will repeat forever.
<number>
The animation will repeat the specified number of times. If the number is not an integer, the animation will end partway through its last cycle. Negative numbers are invalid.

A value of 0 is valid and, similar to an animation-duration of 0s, causes the animation to occur instantaneously.

4.6 The animation-direction property

The animation-direction property defines whether or not the animation should play in reverse on some or all cycles. When an animation is played in reverse the timing functions are also reversed. For example, when played in reverse an ease-in animation would appear to be an ease-out animation.

Name:animation-direction
Value:<single-animation-direction>#
Initial:normal
Applies to:all elements, ::before and ::after pseudo-elements
Inherited:no
Media:visual
Computed value:As specified
Canonical order:per grammar
Percentages:N/A
Animatable:no

<single-animation-direction> = normal | reverse | alternate | alternate-reverse

normal
All iterations of the animation are played as specified.
reverse
All iterations of the animation are played in the reverse direction from the way they were specified.
alternate
The animation cycle iterations that are odd counts are played in the normal direction, and the animation cycle iterations that are even counts are played in a reverse direction.
alternate-reverse
The animation cycle iterations that are odd counts are played in the reverse direction, and the animation cycle iterations that are even counts are played in a normal direction.

Note: For the purpose of determining whether an iteration is even or odd, iterations start counting from 1.

4.7 The animation-play-state property

The animation-play-state property defines whether the animation is running or paused.

Name:animation-play-state
Value:<single-animation-play-state>#
Initial:running
Applies to:all elements, ::before and ::after pseudo-elements
Inherited:no
Media:visual
Computed value:As specified
Canonical order:per grammar
Percentages:N/A
Animatable:no

<single-animation-play-state> = running | paused

running
While this property is set to running, the animation proceeds as normal.
paused
While this property is set to paused, the animation is paused. The animation continues to apply to the element with the progress it had made before being paused. When unpaused (set back to running), it restarts from where it left off, as if the "clock" that controls the animation had stopped and started again.

4.8 The animation-delay property

The animation-delay property defines when the animation will start. It allows an animation to begin execution some time after it is applied, or to appear to have begun execution some time before it is applied.

Name:animation-delay
Value:<time>#
Initial:0s
Applies to:all elements, ::before and ::after pseudo-elements
Inherited:no
Media:visual
Computed value:As specified
Canonical order:per grammar
Percentages:N/A
Animatable:no
<time>
The <time> defines how long of a delay there is between the start of the animation (when the animation is applied to the element via these properties) and when it begins executing. A delay of 0s (the initial value) means that the animation will execute as soon as it is applied.

A negative delay is valid. Similar to a delay of 0s, it means that the animation executes immediately, but is automatically progressed by the absolute value of the delay, as if the animation had started the specified time in the past, and so it appears to start partway through its play-cycle already. If an animation’s keyframes have an implied starting value, the values are taken from the time the animation starts, not some time in the past.

4.9 The animation-fill-mode property

The animation-fill-mode property defines what values are applied by the animation outside the time it is executing. By default, an animation will not affect property values between the time it is applied (the ‘animation-name’ property is set on an element) and the time it begins execution (which is determined by the animation-delay property). Also, by default an animation does not affect property values after the animation ends (determined by the animation-duration and animation-iteration-count properties). The 'animation-fill-mode’property can override this behavior.

Name:animation-fill-mode
Value:<single-animation-fill-mode>#
Initial:none
Applies to:all elements, ::before and ::after pseudo-elements
Inherited:no
Media:visual
Computed value:As specified
Canonical order:per grammar
Percentages:N/A
Animatable:no

<single-animation-fill-mode> = none | forwards | backwards | both

none
The animation has no effect when it is applied but not executing.
forwards
After the animation is done executing (has played the number of times specified by its animation-iteration-count value) it continues to apply the values that it ended its last complete iteration with. This will be the values specified or implied for either its 100% or 0% keyframe, depending on the direction that the last complete iteration was executing in (per animation-direction). If the animation didn’t complete an entire iteration (if the iteration count was 0 or a value less than 1) the values specified or implied for its 0% keyframe are used.

Note: If animation-iteration-count is a non-integer value, the animation will stop executing partway through its animation cycle, but a forwards fill will still apply the values of the 100% keyframe, not whatever values were being applied at the time the animation stopped executing.

Issue: Why does it ignore the progress made by a non-integer iteration count?

Issue: What happens with animation-duration: 0; animation-iteration-count: infinite;? The animation is instantaneous, but there is no "last complete iteration". In particular, you can’t tell whether to use the 0% or 100% keyframe.

backwards
Before the animation has begun executing (during the period specified by animation-delay), the animation applies the values that it will start the first iteration with. If the animation-direction is normal or alternate, the values specified or implied for its 0% keyframe are used; if the animation-direction is reverse or alternate-reverse, the values specified or implied for its 100% keyframe are used.
both
The effects of both forwards and backwards fill apply.

4.10 The animation shorthand property

The animation shorthand property is a comma-separated list of animation definitions. Each item in the list gives one item of the value for all of the subproperties of the shorthand, which are known as the animation properties. (See the definition of animation-name for what happens when these properties have lists of different lengths, a problem that cannot occur when they are defined using only the animation shorthand.)

Name:animation
Value:<single-animation>#
Initial:see individual properties
Applies to:all elements, ::before and ::after pseudo-elements
Inherited:no
Media:visual
Computed value:As specified
Canonical order:per grammar
Percentages:N/A
Animatable:no

<single-animation> = <time> || <single-timing-function> || <time> || <single-animation-iteration-count> || <single-animation-direction> || <single-animation-fill-mode> || <single-animation-play-state> || <single-animation-name>

Note that order is important within each animation definition: the first value in each <single-animation> that can be parsed as a <time> is assigned to the animation-duration, and the second value in each <single-animation> that can be parsed as a <time> is assigned to animation-delay.

Note that order is also important within each animation definition for distinguishing <single-animation-name> values from other keywords. When parsing, keywords that are valid for properties other than animation-name must be accepted for those properties rather than for animation-name. Furthermore, when serializing, default values of other properties must be output in at least the cases necessary to distinguish an animation-name that could be a value of another property, and may be output in additional cases.

For example, a value parsed from animation: 3s none backwards (where animation-fill-mode is none and animation-name is backwards) must not be serialized as animation: 3s backwards (where animation-fill-mode is backwards and animation-name is none).

5 Animation Events

Several animation-related events are available through the DOM Event system. The start and end of an animation, and the end of each iteration of an animation, all generate DOM events. An element can have multiple properties being animated simultaneously. This can occur either with a single animation-name value with keyframes containing multiple properties, or with multiple animation-name values. For the purposes of events, each animation-name specifies a single animation. Therefore an event will be generated for each animation-name value and not necessarily for each property being animated.

Any animation for which both a valid keyframe rule and a non-zero duration are defined will run and generate events; this includes animations with empty keyframe rules.

Issue: This contradicts the animation-delay section, which says that a 0s duration animation still fires events.

The time the animation has been running is sent with each event generated. This allows the event handler to determine the current iteration of a looping animation or the current position of an alternating animation. This time does not include any time the animation was in the paused play state.

5.1 The AnimationEvent Interface

The AnimationEvent interface provides specific contextual information associated with Animation events.

5.1.1 IDL Definition

[Constructor(DOMString type, optional AnimationEventInit animationEventInitDict)]
  interface AnimationEvent : Event {
    readonly attribute DOMString animationName;
    readonly attribute float elapsedTime;
    readonly attribute DOMString pseudoElement;
  };
  dictionary AnimationEventInit : EventInit {
    DOMString animationName = "";
    float elapsedTime = 0.0;
    DOMString pseudoElement = "";
  };

5.1.2 Attributes

animationName, of type DOMString, readonly
The value of the animation-name property of the animation that fired the event.
elapsedTime, of type float, readonly
The amount of time the animation has been running, in seconds, when this event fired, excluding any time the animation was paused. For an animationstart event, the elapsedTime is zero unless there was a negative value for animation-delay, in which case the event will be fired with an elapsedTime of (-1 * delay).
pseudoElement, of type DOMString, readonly
The name (beginning with two colons) of the CSS pseudo-element on which the animation runs (in which case the target of the event is that pseudo-element’s corresponding element), or the empty string if the animation runs on an element (which means the target of the event is that element).

AnimationEvent(type, animationEventInitDict) is an event constructor.

5.2 Types of AnimationEvent

The different types of animation events that can occur are:

animationstart
The animationstart event occurs at the start of the animation. If there is an animation-delay then this event will fire once the delay period has expired. A negative delay will cause the event to fire with an elapsedTime equal to the absolute value of the delay.
animationend
The animationend event occurs when the animation finishes.
animationiteration
The animationiteration event occurs at the end of each iteration of an animation, except when an animationend event would fire at the same time. This means that this event does not occur for animations with an iteration count of one or less.

6 DOM Interfaces

CSS animations are exposed to the CSSOM through a pair of new interfaces describing the keyframes.

6.1 The CSSRule Interface

The following two rule types are added to the CSSRule interface. They provide identification for the new keyframe and keyframes rules.

6.1.1 IDL Definition

partial interface CSSRule {
    const unsigned short KEYFRAMES_RULE = 7;
    const unsigned short KEYFRAME_RULE = 8;
};

6.2 The CSSKeyframeRule Interface

The CSSKeyframeRule interface represents the style rule for a single key.

6.2.1 IDL Definition

interface CSSKeyframeRule : CSSRule {
           attribute DOMString           keyText;
  readonly attribute CSSStyleDeclaration style;
};

6.2.2 Attributes

keyText, of type DOMString
This attribute represents the keyframe selector as a comma-separated list of percentage values. The from and to keywords map to 0% and 100%, respectively.
style, of type CSSStyleDeclaration
This attribute represents the style associated with this keyframe.

6.3 The CSSKeyframesRule Interface

The CSSKeyframesRule interface represents a complete set of keyframes for a single animation.

6.3.1 IDL Definition

interface CSSKeyframesRule : CSSRule {
           attribute DOMString   name;
  readonly attribute CSSRuleList cssRules;

  void            appendRule(in DOMString rule);
  void            deleteRule(in DOMString key);
  CSSKeyframeRule findRule(in DOMString key);
};

6.3.2 Attributes

name, of type DOMString
This attribute is the name of the keyframes, used by the animation-name property.
cssRules, of type CSSRuleList
This attribute gives access to the keyframes in the list.

6.3.3 The appendRule method

The appendRule() method appends the passed CSSKeyframeRule into the list at the passed key.

Parameters:

rule of type DOMString
The rule to be appended, expressed in the same syntax as one entry in the @keyframes rule.

No Return Value

No Exceptions

6.3.4 The deleteRule method

The deleteRule() deletes the CSSKeyframeRule with the passed key. If a rule with this key does not exist, the method does nothing.

Parameters:

key of type DOMString
The key which describes the rule to be deleted. The key must resolve to a number between 0 and 1, or the rule is ignored.

No Return Value

No Exceptions

6.3.5 The findRule method

The findRule() returns the rule with a key matching the passed key. If no such rule exists, a null value is returned.

Parameters:

key of type DOMString
The key which described the rule to find. The key must resolve to a number between 0 and 1, or the rule is ignored.

Return Value:

CSSKeyframeRule
The found rule.

No Exceptions

7 Acknowledgements

Thanks especially to the feedback from Tab Atkins, Carine Bournez, Anne van Kesteren, Øyvind Stenhaug, Estelle Weyl, and all the rest of the www-style community.

8 Working Group Resolutions that are pending editing

This section is informative and temporary.

The editors are currently behind on editing this spec. The following working group resolutions still need to be edited in: