How to Use CSS Transitions
See CSS: Tips and Tricks for similar articles.The CSS3 transition property defines how an element changes from one state to another. With transition—or the specific properties listed below, for which transition serves as a shorthand—we can stretch out the change on an element's width, background color, or other property over a specified duration. You have to include appropriate vendor prefixes for transitions: -moz- for older version of Firefox, webkit for older versions of Chrome/Safari, and -o- for older versions of Opera. Internet Explorer supports CSS transitions, unprefixed, from version 10+.
The transition properties are as follows:
Property | Description/Example | |
transition | Shorthand for setting all four properties in one statement.
transition:width 3s ease 1s | |
transition-property | Specific property/ies for which transition will occur, or all (default) or none.
transition-property: width, top | |
transition-duration | Length, in specified time units, of the transition. With duration specified as "0" (or if not specified, and thus defaulting to "0"), no visible effect will take place.
transition-duration: 1s | |
transition-timing-function | Defines the easing function of the transition; how the speed of the transition will change over the course of the duration. Possible values are linear , ease (default), ease-in , ease-out , ease-in-out , cubic-bezier(n,n,n,n) .
transition-timing-function: easeout | |
transition-delay | Delay, in specified time units, after which transition will occur; default is 0.
transition-delay: 3s |
Let's look at an example that illustrates the use of the transition property:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>CSS3 Transitions</title>
<style type="text/css">
#trans1 {
width: 160px;
height: 160px;
padding: 20px;
float: left;
background-color: hsl(20, 50%, 20%);
color: hsl(0, 100%, 100%);
-moz-transition: width 2s;
-webkit-transition: width 2s;
-o-transition: width 2s;
transition: width 2s;
}
#trans1:hover {
width: 100px;
height: 100px;
background-color: hsl(30, 50%, 90%);
color: hsl(0, 0%, 0%);
}
#trans2 {
margin-left: 20px;
width: 400px;
height: 160px;
padding: 20px;
float: left;
background-color: hsl(0, 0%, 0%);
color: hsl(0, 100%, 100%);
position: relative;
}
#positioned {
width: 80px;
height: 80px;
padding: 20px;
background-color: hsl(0, 68%, 32%);
font-size: 12px;
position: absolute;
left: 10px;
-moz-transition-duration: 2s;
-webkit-transition-duration: 2s;
-o-transition-duration: 2s;
transition-duration: 2s;
-moz-transition-timing-function: ease-in;
-webkit-transition-timing-function: ease-in;
-o-transition-timing-function: ease-in;
transition-timing-function: ease-in;
-moz-transition-delay: 1s;
-webkit-transition-delay: 1s;
-o-transition-delay: 1s;
transition-delay: 1s;
}
#trans2:hover #positioned {
left: 310px;
}
</style>
</head>
<body>
<div id="main">
<h1>CSS3 Transitions</h1>
<div id="trans1">
<p>Mouseover me - width changes</p>
</div>
<div id="trans2">
<div id="positioned">
<p>I am a positioned</p>
</div>
</div>
</div>
</body>
</html>
The page presents two main elements: a brown square on the left with white text "Mouseover me – width changes", and a black rectangle on the right containing a smaller red rectangle. The red rectangle is positioned absolutely, 10 pixels from the left edge of the black rectangle:
You used #trans1:hover
to change the width, height, background color, and text color of the left (brown) square when the user mouses over the element. Note that the CSS transition property is set on #trans1, not on #trans1:hover. In this code, use the shorthand property to state that the transition will apply only to the width change—not for the height, background color, or text color changes—and that it will occur over a duration of two seconds.
#trans1:hover {
width: 100px;
height: 100px;
background-color: hsl(30, 50%, 90%);
color: hsl(0, 0%, 0%);
The page looks like this after the transition finishes:
Related Articles
- Learn the Very Basics of CSS in One Minute
- How to Create a CSS External Style Sheet
- How to Align Text with CSS
- How to Create a Horizontal Navigation Menu with CSS
- How to Create a Fixed-Width Layout with CSS
- How to Remove Spacing Between Table Borders with CSS
- How to Set a Background Image with CSS
- How to Set Text Spacing and Placement in CSS
- How to Style a Table with CSS
- How to Create Boxes with Rounded Corners in CSS
- How to Create a Vertical Navigation Menu with CSS
- How to Use the CSS Opacity Property
- How to Use Multiple Background Images with CSS
- Absolute Positioning with CSS
- How to Use the CSS Border Shorthand Property
- How to Create CSS Button Links
- How to Create a Fluid-Width Layout with CSS
- How to Set Text and Background Color with CSS
- How to Create a CSS Embedded Style Sheet
- How to Add Inline Styles to CSS
- How to Create a Border with CSS
- How to Use the CSS Padding Shorthand Property
- How to Create a Fly-Out Menu with CSS
- How to Use CSS Media Queries in Responsive Design
- How to Adjust Margins with CSS
- How to Use the CSS Background Shorthand Property
- How to Create a Form without Tables Using CSS
- How to Modify Fonts in CSS
- How to Create a Drop-Down Menu with CSS
- How to Apply Padding with CSS
- Fixed Positioning with CSS
- How to Use CSS Transitions (this article)
- How to Use the CSS list-style Shorthand Property
- How to Change Text Style in CSS
- How to Create CSS Sprites
- How to Use CSS with Different Media Types
- How to Import Style Sheets with @import in CSS
- How to Use the CSS White-Space Property
- How to Use the CSS Z-index Property
- How to Create Drop Shadows with the box-shadow Property in CSS3