<timing-function>

  • Revision slug: Web/CSS/timing-function
  • Revision title: timing-function
  • Revision id: 494119
  • Created:
  • Creator: kscarfone
  • Is current revision? No
  • Comment Updated tags

Revision Content

{{CSSRef}}

Summary

The <timing-function> CSS data type denotes a mathematical function that describes how fast one-dimensional values change during transitions or animations. This in essence lets you establish an acceleration curve, so that the speed of the animation can vary over its duration. These functions are often called easing functions.

It is a function linking the time with a ratio of the output value, expressed as a {{xref_cssnumber}}, with 0.0 representing the initial state, 1.0 the final state.

The output ratio can be greater than 1.0 (or smaller than 0.0). This causes the animation to go farther than the final state, then come back, in a kind of bouncing effect.

Nevertheless, if the output value goes outside of its possible range, such as a component of a color going greater than 255 or smaller than 0, the value is clipped to its closest allowed value (in the case of a color-component 255 and 0 respectively. Some cubic-bezier() curves exhibit this property.

Values

CSS supports two kinds of timing functions: the subset of the cubic Bézier curves that are functions and staircase functions. The most useful of these functions are given a keyword that allows to easily describe them.

The cubic-bezier() class of timing-functions

The cubic-bezier() functional notation defines a cubic Bézier curve. As these curves are continuous, they are often used to smooth down the start and end of the animation and are therefore sometimes called easing functions.

A cubic Bézier curve is defined by four points P0, P1, P2, and P3. P0 and P3 are the start and the end of the curve and, in CSS these points are fixed as the coordinates are ratios (the abscissa the ratio of time, the ordinate the ratio of the output range). P0 is (0, 0) and represents the initial time and the initial state, P3 is (1, 1) and represents the final time and the final state.

Not all cubic Bézier curves are suitable as timing functions as not all are mathematical functions, i.e. curves that for a given abscissa have zero or one value. With P0 and P3 fixed as defined by CSS, a cubic Bézier curve is a function, and is therefore valid, if and only if the abscissas of P1 and P2 are both in the [0, 1] range.

Cubic Bézier curves with the P1 or P2 ordinate outside the [0, 1] range may generate bouncing effects.

When you specify an invalid cubic-bezier curve, CSS ignores the whole property.

Syntax

cubic-bezier(x1, y1, x2, y2)

where :

x1, y1, x2, y2
Are {{xref_cssnumber}} values representing the abscissas and ordinates of the P1 and P2 points defining the cubic Bézier curve. x1 and x2 must be in the range [0, 1] or the value is invalid.

Examples

These cubic Bézier curves are valid for use in CSS :

cubic-bezier(0.1, 0.7, 1.0, 0.1)   The canonical Bézier curve with four <number> in the [0,1] range.
cubic-bezier(0, 0, 1, 1)           Using <integer> is valid as any <integer> is also a <number>.
cubic-bezier(0.1, -0.6, 0.2, 0)    Negative values for ordinates are valid, leading to bouncing effects.
cubic-bezier(0, 1.1, 0.8, 4)       Values > 1.0 for ordinates are also valid.

These cubic Bézier curves definitions are invalid :

cubic-bezier(0.1, red, 1.0, green) Though the animated output type may be a color, Bézier curves work w/ numerical ratios.
cubic-bezier(2.45, 0.6, 4, 0.1)    Abscissas must be in the [0, 1] range or the curve is not a function of time.
cubic-bezier(0.3, 2.1)             The two points must be defined, there is no default value.
cubic-bezier(-1.9, 0.3, -0.2, 2.1) Abscissas must be in the [0, 1] range or the curve is not a function of time.

The steps() class of timing-functions

The steps() functional notation defines a step function dividing the domain of output values in equidistant steps. This subclass of step functions are sometimes also called staircase functions.
steps(2, start) steps(4, end)

Syntax

steps(number_of_steps, direction)

where :

number_of_steps
Is a strictly positive {{xref_cssinteger}} representing the amount of equidistant treads composing the stepping function.
direction
Is a keyword indicating if it the function is left- or right-continuous:
  • start denotes a left-continuous function, so that the first step happens when the animation begins;
  • end denotes a right-continuous function, so that the last step happens when the animation ends.

Examples

These timing functions are valid :

steps(5, end)          There is 5 treads, the last one happens right before the end of the animation.
steps(2, start)        A two-step staircase, the first one happening at the start of the animation.

These timing function are invalid :

steps(2.0, end)        The first parameter must be an <integer> and cannot be a real value, even if it is equal to one.
steps(-3, start)       The amount of steps must be non-negative.
steps(0, end)          There must be at least one step.
steps(2)               The second parameter is not optional.
steps(start, 3)        Though of different types, the order of parameter is important.
step(1, end)           Even if there is one step, the function name is steps, with the plural 's'
steps(3 end)           The two parameters must be separated with a comma; one or several spaces is not enough.

Keywords for common timing-functions

linear

This keyword represents the timing function cubic-bezier(0.0, 0.0, 1.0, 1.0). Using this timing function, the animation goes from its initial state to its final one regularly, with a constant speed.

ease

This keyword represents the timing function cubic-bezier(0.25, 0.1, 0.25, 1.0). This function is similar to ease-in-out, though it accelerates more sharply at the beginning and the acceleration already starts to slow down near the middle of it.

ease-in

This keyword represents the timing function cubic-bezier(0.42, 0.0, 1.0, 1.0). The animation begins slowly, then progressively accelerates until the final state is reached and the animation stops abruptly.

ease-in-out

This keyword represents the timing function cubic-bezier(0.42, 0.0, 0.58, 1.0). With this timing function, the animation starts slowly, accelerates than slows down when approaching to its final state. At the begin, it behaves similarly to the ease-in function, at the end, it is similar to the ease-out function.

ease-out

This keyword represents the timing function cubic-bezier(0.0, 0.0, 0.58, 1.0). The animation starts quickly then slow progressively down when approaching to its final state.

step-start

This keyword represents the timing function steps(1, start). Using this timing function, the animation jumps immediately to the end state and stay in that position until the end of the animation.  

step-end

This keyword represents the timing function steps(1, end). Using this timing function, the animation stays in its initial state until the end, where it directly jumps to its final position.

Specifications

Specification Status Comment
{{SpecName('CSS3 Transitions', '#transition-timing-function', '<timing-function>')}} {{Spec2('CSS3 Transitions')}} Defined anonymously
{{SpecName('CSS3 Animations', '#animation-timing-function', '<timing-function>')}} {{Spec2('CSS3 Animations')}} Defined anonymously, says to see definition in the CSS Transitions Module

Browser compatibility

{{CompatibilityTable}}
Feature Firefox (Gecko) Chrome Internet Explorer Opera Safari
Basic support 4.0 (2.0) 4.0 10 10.5 3.1
cubic-bezier() w/ ordinate ∉ [0,1] 4.0 (2.0) 16.0 10 12.1 Nightly
steps() 4.0 (2.0) 8.0 10 12.1 5.1
Feature Firefox Mobile (Gecko) Android IE Phone Opera Mobile Safari Mobile
Basic support 4.0 (2.0) 4.0 {{CompatNo}} 10 2.0
cubic-bezier() w/ ordinate ∉ [0,1] 4.0 (2.0) {{CompatVersionUnknown}} {{CompatNo}} {{CompatNo}} {{CompatNo}}
steps() 4.0 (2.0) 4.0 {{CompatNo}} {{CompatNo}} 5.0

See also

  • The {{cssxref("transition-timing-function")}} and {{cssxref("animation-timing-function")}} needing a <timing-function> value.
  • CSS Reference
  • CSS Data Types : {{CSSDataTypes}}

Revision Source

<div>
 {{CSSRef}}</div>
<h2 id="Summary">Summary</h2>
<p>The <code>&lt;timing-function&gt;</code> <a href="/en-US/docs/CSS">CSS</a> data type denotes a mathematical function that describes how fast one-dimensional values change during transitions or animations. This in essence lets you establish an acceleration curve, so that the speed of the animation can vary over its duration. These functions are often called <em>easing functions<code>.</code></em></p>
<p>It is a function linking the time with a ratio of the output value, expressed as a {{xref_cssnumber}}, with <code>0.0</code> representing the initial state, <code>1.0</code> the final state.</p>
<p><img src="/files/3434/TF_with_output_gt_than_1.png" /><img src="/files/3435/TF_with_output_gt_than_1_clipped.png" style="margin-right:5px;" /></p>
<p>The output ratio can be greater than 1.0 (or smaller than 0.0). This causes the animation to go farther than the final state, then come back, in a kind of <em>bouncing</em> effect.</p>
<p>Nevertheless, if the output value goes outside of its possible range, such as a component of a color going greater than <code>255</code> or smaller than <code>0</code>, the value is clipped to its closest allowed value (in the case of a color-component <code>255</code> and <code>0</code> respectively. Some <code>cubic-bezier()</code> curves exhibit this property.</p>
<h2 class="cleared" id="Values">Values</h2>
<div style="width:100%">
 <p>CSS supports two kinds of timing functions: the subset of the cubic Bézier curves that are functions and staircase functions. The most useful of these functions are given a <em>keyword</em> that allows to easily describe them.</p>
 <h3 id="The_cubic-bezier()_class_of_timing-functions">The <code>cubic-bezier()</code> class of timing-functions</h3>
 <table class="withoutBorder">
  <tbody>
   <tr>
    <td><img src="/files/3433/cubic-bezier,%20example.png" style="float: left;" /></td>
    <td>
     <p>The <code>cubic-bezier()</code> functional notation defines a <a href="http://en.wikipedia.org/wiki/B%C3%A9zier_curve#Cubic_B.C3.A9zier_curves">cubic Bézier curve</a>. As these curves are continuous, they are often used to smooth down the start and end of the animation and are therefore sometimes called <em>easing functions</em>.</p>
     <p>A cubic Bézier curve is defined by four points P<sub>0</sub>, P<sub>1</sub>, P<sub>2</sub>, and P<sub>3</sub>. P<sub>0</sub> and P<sub>3</sub> are the start and the end of the curve and, in CSS these points are fixed as the coordinates are ratios (the abscissa the ratio of time, the ordinate the ratio of the output range). P<sub>0</sub> is <code>(0, 0)</code> and represents the initial time and the initial state, P<sub>3</sub> is <code>(1, 1)</code> and represents the final time and the final state.</p>
     <p>Not all cubic Bézier curves are suitable as timing functions as not all are <a href="http://en.wikipedia.org/wiki/Function_%28mathematics%29">mathematical functions</a>, i.e. curves that for a given abscissa have zero or one value. With P<sub>0</sub> and P<sub>3</sub> fixed as defined by CSS, a cubic Bézier curve is a function, and is therefore valid, if and only if the abscissas of P<sub>1</sub> and P<sub>2</sub> are both in the <code>[0, 1]</code> range.</p>
     <p>Cubic Bézier curves with the P<sub>1</sub> or P<sub>2</sub> ordinate outside the <code>[0, 1]</code> range may generate <em>bouncing</em> effects.</p>
     <p>When you specify an invalid <code>cubic-bezier</code> curve, CSS ignores the whole property.</p>
    </td>
   </tr>
  </tbody>
 </table>
</div>
<h4 id="Syntax">Syntax</h4>
<pre>
<code>cubic-bezier(<em>x<sub>1</sub></em>, <em>y<sub>1</sub></em>, <em>x<sub>2</sub></em>, <em>y<sub>2</sub></em>)</code>
</pre>
<p>where :</p>
<dl>
 <dt>
  <strong><em>x<sub>1</sub></em>, <em>y<sub>1</sub></em>, <em>x<sub>2</sub></em>, <em>y<sub>2</sub></em></strong></dt>
 <dd>
  Are {{xref_cssnumber}} values representing the abscissas and ordinates of the P<sub>1</sub> and P<sub>2</sub> points defining the cubic Bézier curve. x<sub>1</sub> and x<sub>2</sub> must be in the range [0, 1] or the value is invalid.</dd>
</dl>
<h4 id="Examples">Examples</h4>
<p>These cubic Bézier curves are valid for use in CSS :</p>
<pre>
cubic-bezier(0.1, 0.7, 1.0, 0.1)   The canonical Bézier curve with four &lt;number&gt; in the [0,1] range.
cubic-bezier(0, 0, 1, 1)           Using &lt;integer&gt; is valid as any &lt;integer&gt; is also a &lt;number&gt;.
cubic-bezier(0.1, -0.6, 0.2, 0)    Negative values for ordinates are valid, leading to bouncing effects.
cubic-bezier(0, 1.1, 0.8, 4)       Values &gt; 1.0 for ordinates are also valid.</pre>
<p>These cubic Bézier curves definitions are invalid :</p>
<pre>
cubic-bezier(0.1, red, 1.0, green) Though the animated output type may be a color, Bézier curves work w/ numerical ratios.
cubic-bezier(2.45, 0.6, 4, 0.1)    Abscissas must be in the [0, 1] range or the curve is not a function of time.
cubic-bezier(0.3, 2.1)             The two points must be defined, there is no default value.
cubic-bezier(-1.9, 0.3, -0.2, 2.1) Abscissas must be in the [0, 1] range or the curve is not a function of time.
</pre>
<h3 id="The_steps()_class_of_timing-functions">The <code>steps()</code> class of timing-functions</h3>
<table class="withoutBorder">
 <tbody>
  <tr>
   <td><img height="332" src="/files/3436/steps(2,start).png" width="248" /></td>
   <td><img height="332" src="/files/3437/steps(4,end).png" width="248" /></td>
   <td rowspan="2">The <code>steps()</code> functional notation defines a <a href="http://en.wikipedia.org/wiki/Step_function">step function</a> dividing the domain of output values in equidistant steps. This subclass of step functions are sometimes also called staircase functions.</td>
  </tr>
  <tr style="text-align: center;">
   <td style="white-space: nowrap;"><code>steps(2, start)</code></td>
   <td><code>steps(4, end)</code></td>
  </tr>
 </tbody>
</table>
<h4 id="Syntax">Syntax</h4>
<pre>
<code>steps(<em>number_of_steps</em>, <em>direction</em>)</code>
</pre>
<p>where :</p>
<dl>
 <dt>
  <strong><em>number_of_steps</em></strong></dt>
 <dd>
  Is a strictly positive {{xref_cssinteger}} representing the amount of equidistant treads composing the stepping function.</dd>
 <dt>
  <strong><em>direction</em></strong></dt>
 <dd>
  Is a keyword indicating if it the function is <a href="http://en.wikipedia.org/wiki/Left-continuous#Directional_and_semi-continuity">left- or right-continuous</a>:
  <ul>
   <li><code>start</code> denotes a left-continuous function, so that the first step happens when the animation begins;</li>
   <li><code>end</code> denotes a right-continuous function, so that the last step happens when the animation ends.</li>
  </ul>
 </dd>
</dl>
<h4 id="Examples">Examples</h4>
<p>These timing functions are valid :</p>
<pre>
steps(5, end)          There is 5 treads, the last one happens right before the end of the animation.
steps(2, start)        A two-step staircase, the first one happening at the start of the animation.
</pre>
<p>These timing function are invalid :</p>
<pre>
steps(2.0, end)        The first parameter must be an <a href="/en-US/docs/CSS/integer">&lt;integer&gt;</a> and cannot be a real value, even if it is equal to one.
steps(-3, start)       The amount of steps must be non-negative.
steps(0, end)          There must be at least one step.
steps(2)               The second parameter is not optional.
steps(start, 3)        Though of different types, the order of parameter is important.
step(1, end)           Even if there is one step, the function name is steps, with the plural 's'
steps(3 end)           The two parameters must be separated with a comma; one or several spaces is not enough.
</pre>
<h3 id="Keywords_for_common_timing-functions">Keywords for common timing-functions</h3>
<h4 id="linear"><code>linear</code></h4>
<table class="withoutBorder">
 <tbody>
  <tr style="vertical-align: top;">
   <td><img height="332" src="/files/3425/cubic-bezier,linear.png" width="249" /></td>
   <td>This keyword represents the timing function <code>cubic-bezier(</code><code>0.0, 0.0, 1.0, 1.0</code><code>)</code>. Using this timing function, the animation goes from its initial state to its final one regularly, with a constant speed.</td>
  </tr>
 </tbody>
</table>
<h4 id="ease"><code>ease</code></h4>
<table class="withoutBorder">
 <tbody>
  <tr style="vertical-align: top;">
   <td><img height="332" src="/files/3429/cubic-bezier,ease.png" width="244" /></td>
   <td>This keyword represents the timing function <code>cubic-bezier(0.25, 0.1, 0.25, 1.0)</code>. This function is similar to <a href="/en-US/docs/CSS/timing-function#ease-in-out"><code>ease-in-out</code></a>, though it accelerates more sharply at the beginning and the acceleration already starts to slow down near the middle of it.</td>
  </tr>
 </tbody>
</table>
<h4 id="ease-in"><code>ease-in</code></h4>
<table class="withoutBorder">
 <tbody>
  <tr style="vertical-align: top;">
   <td><img height="332" src="/files/3426/cubic-bezier,ease-in.png" width="249" /></td>
   <td>This keyword represents the timing function <code>cubic-bezier(0.42, 0.0, 1.0, 1.0)</code>. The animation begins slowly, then progressively accelerates until the final state is reached and the animation stops abruptly.</td>
  </tr>
 </tbody>
</table>
<h4 id="ease-in-out"><code>ease-in-out</code></h4>
<table class="withoutBorder">
 <tbody>
  <tr style="vertical-align: top;">
   <td><img height="332" src="/files/3428/cubic-bezier,ease-in-out.png" width="244" /></td>
   <td>This keyword represents the timing function <code>cubic-bezier(</code><code>0.42, 0.0, 0.58, 1.0</code><code>)</code>. With this timing function, the animation starts slowly, accelerates than slows down when approaching to its final state. At the begin, it behaves similarly to the <a href="/en-US/docs/CSS/timing-function#ease-in"><code>ease-in</code></a> function, at the end, it is similar to the <a href="/en-US/docs/CSS/timing-function#ease-out"><code>ease-out</code></a> function.</td>
  </tr>
 </tbody>
</table>
<h4 id="ease-out"><code>ease-out</code></h4>
<table class="withoutBorder">
 <tbody>
  <tr style="vertical-align: top;">
   <td><img height="332" src="/files/3427/cubic-bezer,ease-out.png" width="249" /></td>
   <td>This keyword represents the timing function <code>cubic-bezier(</code><code>0.0, 0.0, 0.58, 1.0</code><code>)</code>. The animation starts quickly then slow progressively down when approaching to its final state.</td>
  </tr>
 </tbody>
</table>
<h4 id="step-start"><code>step-start</code></h4>
<table class="withoutBorder">
 <tbody>
  <tr>
   <td><img height="332" src="/files/3423/steps(1,start).png" width="248" />This keyword represents the timing function <code>steps(1, start)</code>. Using this timing function, the animation jumps immediately to the end state and stay in that position until the end of the animation.</td>
   <td>&nbsp;</td>
  </tr>
 </tbody>
</table>
<h4 id="step-end"><code>step-end</code></h4>
<table class="withoutBorder">
 <tbody>
  <tr style="vertical-align: top;">
   <td><img height="332" src="/files/3424/steps(1,end).png" width="248" /></td>
   <td>This keyword represents the timing function <code>steps(1, end)</code>. Using this timing function, the animation stays in its initial state until the end, where it directly jumps to its final position.</td>
  </tr>
 </tbody>
</table>
<h2 id="Specifications">Specifications</h2>
<table class="standard-table">
 <thead>
  <tr>
   <th>Specification</th>
   <th>Status</th>
   <th>Comment</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('CSS3 Transitions', '#transition-timing-function', '&lt;timing-function&gt;')}}</td>
   <td>{{Spec2('CSS3 Transitions')}}</td>
   <td>Defined anonymously</td>
  </tr>
  <tr>
   <td>{{SpecName('CSS3 Animations', '#animation-timing-function', '&lt;timing-function&gt;')}}</td>
   <td>{{Spec2('CSS3 Animations')}}</td>
   <td>Defined anonymously, says to see definition in the CSS Transitions Module</td>
  </tr>
 </tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<div>
 {{CompatibilityTable}}</div>
<div id="compat-desktop">
 <table class="compat-table">
  <tbody>
   <tr>
    <th>Feature</th>
    <th>Firefox (Gecko)</th>
    <th>Chrome</th>
    <th>Internet Explorer</th>
    <th>Opera</th>
    <th>Safari</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>4.0 (2.0)</td>
    <td>4.0</td>
    <td>10</td>
    <td>10.5</td>
    <td>3.1</td>
   </tr>
   <tr>
    <td><code>cubic-bezier()</code> w/ ordinate ∉ [0,1]</td>
    <td>4.0 (2.0)</td>
    <td>16.0</td>
    <td>10</td>
    <td>12.1</td>
    <td>Nightly</td>
   </tr>
   <tr>
    <td><code>steps()</code></td>
    <td>4.0 (2.0)</td>
    <td>8.0</td>
    <td>10</td>
    <td>12.1</td>
    <td>5.1</td>
   </tr>
  </tbody>
 </table>
</div>
<div id="compat-mobile">
 <table class="compat-table">
  <tbody>
   <tr>
    <th>Feature</th>
    <th>Firefox Mobile (Gecko)</th>
    <th>Android</th>
    <th>IE Phone</th>
    <th>Opera Mobile</th>
    <th>Safari Mobile</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>4.0 (2.0)</td>
    <td>4.0</td>
    <td>{{CompatNo}}</td>
    <td>10</td>
    <td>2.0</td>
   </tr>
   <tr>
    <td><code>cubic-bezier()</code> w/ ordinate ∉ [0,1]</td>
    <td>4.0 (2.0)</td>
    <td>{{CompatVersionUnknown}}</td>
    <td>{{CompatNo}}</td>
    <td>{{CompatNo}}</td>
    <td>{{CompatNo}}</td>
   </tr>
   <tr>
    <td><code>steps()</code></td>
    <td>4.0 (2.0)</td>
    <td>4.0</td>
    <td>{{CompatNo}}</td>
    <td>{{CompatNo}}</td>
    <td>5.0</td>
   </tr>
  </tbody>
 </table>
</div>
<h2 id="See_also">See also</h2>
<ul>
 <li>The {{cssxref("transition-timing-function")}} and {{cssxref("animation-timing-function")}} needing a <code>&lt;timing-function&gt;</code> value.</li>
 <li><a href="/en-US/docs/Web/CSS/Reference">CSS Reference</a></li>
 <li>CSS Data Types : {{CSSDataTypes}}</li>
</ul>
Revert to this revision