Using CSS Transitions - Web Developer Guide - MDN
Using CSS Transitions - Web Developer Guide - MDN
CSS transitions let you decide which properties to animate (by listing them explicitly), when the
animation will start (by setting a delay), how long the transition will last (by setting a duration), and
how the transition will run (by dening a timing function, e.g. linearly or quick at the beginning,
slow at the end).
Note: CSS transition properties can be used without any prex provider, but since the
specication has only recently achieved stability, the vendor prexes can still be necessary for
browsers based on WebKit. They are also necessary for compatibility with older browser versions
(e.g.: Firefox 15 and earlier, Opera 12 and earlier). A compatibility table is available at the
bottom of this page with more information.
Note: The set of properties that can be animated is subject to change. Developers should
proceed with caution.
Also the auto value is often a very complex case. The specication asks not to animate from and
https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Using_CSS_transitions
Page 1 of 8
to such a value. Some user agents, like those based on Gecko, implement this requirement and
others, like those based on WebKit, are less strict. Using animations with auto may lead to
unpredictable results, depending of the browser and its version, and should be avoided.
Care should also be taken when using a transition immediately after adding the element to the
DOM using .appendChild() or removing its display: none; property. This is seen as if the
initial state had never occurred and the element was always in its nal state. The easy way to
overcome this limitation is to apply a window.setTimeout() of a handful of milliseconds before
changing the CSS property you intend to transition to.
3
4
<body>
<p>The box below combines transitions for: width, height, background-co
<div class="box"></div>
</body>
CSS Content
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
.box {
border-style: solid;
border-width: 1px;
display: block;
width: 100px;
height: 100px;
background-color: #0000FF;
-webkit-transition:width 2s, height 2s, background-color 2s, -webkit-tr
transition:width 2s, height 2s, background-color 2s, transform 2s;
}
.box:hover {
background-color: #FFCCCC;
width:200px;
height:200px;
-webkit-transform:rotate(180deg);
transform:rotate(180deg);
}
The box below combines transitions for: width, height, background-color, transform. Hover
over the box to see these properties animated.
https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Using_CSS_transitions
Page 2 of 8
Lorem
transition-duration: 2s
Lorem
transition-duration: 4s
Lorem
Lorem
Species the duration over which transitions should occur. You can specify a single duration
that applies to all properties during the transition, or multiple values to allow each property to
transition over a dierent period of time.
transition-duration: 0.5s
transition-duration: 1s
transition-timing-function
Species a function to dene how intermediate values for properties are
computed. Timing functions determine how intermediate values of the
transition are calculated. Most timing functions can be specied by
providing the graph of the corresponding function, as dened by four
points dening a cubic bezier. You can also choose easing from ! Easing
Functions Cheat Sheet.
https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Using_CSS_transitions
Page 3 of 8
transition-timing-function:
transition-timing-function:
ease
linear
Lorem
Lorem
transition-timing-function:
transition-timing-function:
step-end
steps(4, end)
Lorem
Lorem
transition-delay
Denes how long to wait between the time a property is changed and the transition actually
begins.
transition-delay: 0.5s
transition-delay: 1s
Lorem
transition-delay: 2s
Lorem
transition-delay: 4s
Lorem
Lorem
1
2
3
div {
transition: <property> <duration> <timing-function> <delay>;
}
https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Using_CSS_transitions
Page 4 of 8
Note: The transitionend event doesn't re if the transition is aborted because the animating
property's value is changed before the transition is completed.
1
2
3
4
div {
transition-property: opacity, left, top, height;
transition-duration: 3s, 5s;
}
1
2
3
4
div {
transition-property: opacity, left, top, height;
transition-duration: 3s, 5s, 3s, 5s;
}
Similarly, if any property's value list is longer than that for transition-property, it's truncated,
so if you have the following CSS:
1
2
3
4
div {
transition-property: opacity, left;
transition-duration: 3s, 5s, 2s, 1s;
}
https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Using_CSS_transitions
Page 5 of 8
1
2
3
4
div {
transition-property: opacity, left;
transition-duration: 3s, 5s;
}
A simple example
This example performs a four-second font size transition with a two-second delay between the
time the user mouses over the element and the beginning of the animation eect:
1
2
3
4
5
6
7
#delay1 {
position: relative;
transition-property: font-size;
transition-duration: 4s;
transition-delay: 2s;
font-size: 14px;
}
8
9
10
11
12
13
14
#delay1:hover {
transition-property: font-size;
transition-duration: 4s;
transition-delay: 2s;
font-size: 36px;
}
1
2
3
4
5
6
<div class="sidebar">
<p><a class="menuButton"
<p><a class="menuButton"
<p><a class="menuButton"
<p><a class="menuButton"
</div>
href="home">Home</a></p>
href="about">About</a></p>
href="contact">Contact Us</a></p>
href="links">Links</a></p>
Then we build the CSS to implement the look and feel of our menu. The relevant portions are
shown here:
1
2
3
.menuButton {
position: relative;
transition-property: background-color, color;
https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Using_CSS_transitions
Page 6 of 8
transition-duration: 1s;
transition-timing-function: ease-out;
text-align: left;
background-color: grey;
left: 5px;
top: 5px;
height: 26px;
color: white;
border-color: black;
font-family: sans-serif;
font-size: 20px;
text-decoration: none;
box-shadow: 2px 2px 1px black;
padding: 2px 4px;
border: solid 1px black;
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
}
.menuButton:hover {
position: relative;
transition-property: background-color, color;
transition-duration: 1s;
transition-timing-function: ease-out;
background-color:white;
color:black;
box-shadow: 2px 2px 1px black;
}
This CSS establishes the look of the menu, with the background and text colors both changing
when the element is in its :hover state.
Instead of describing the eect at length, you can take a look at the live sample if your ! browser
has transitions support.
1
2
Using JavaScript you can make the eect of moving the ball to a certain position happen:
1
2
3
4
5
var f = document.getElementById('foo');
document.addEventListener('click', function(ev){
f.style.left = (ev.clientX-25)+'px';
f.style.top = (ev.clientY-25)+'px';
},false);
With CSS you can make it smooth without any extra eort. Simply add a transition to the element
and any change will happen smoothly:
https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Using_CSS_transitions
Page 7 of 8
p {
padding-left: 60px;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#foo {
border-radius: 50px;
width: 50px;
height: 50px;
background: #c00;
position: absolute;
top: 0;
left: 0;
transition: all 1s;
}
Specications
Specication
Status
CSS Transitions
WD
Working Draft
Comment
Initial specication.
Browser compatibility
Desktop
Mobile
Feature
Chrome
Firefox Internet
Opera
(Gecko) Explorer
Basic support
1.0
4.0 (2.0)
-w e bk i t
26.0
10
10.5
Safari (WebKit)
-o
3.2
- w eb ki t
12.10
- m oz
16.0
(16.0)
transitionend 1.0 as
4.0 (2.0)
webkitTransitionEnd
event
26.0? as
transitionend
10
10.5 as
3.2 as
oTransitionEnd webkitTransitionEnd
12 as
6.0? as transitionend
otransitionend
12.10 as
transitionend
See also
!!
https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Using_CSS_transitions
Page 8 of 8