How to Use CSS Transitions

  • google plus

In Brief...

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+.

Take our Introduction to CSS Training course for free.

See the Course Outline and Register


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>
	<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;
	<div id="main">
		<h1>CSS3 Transitions</h1>
		<div id="trans1">
			<p>Mouseover me - width changes</p>
		<div id="trans2">
			<div id="positioned">
				<p>I am a positioned</p>

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:
CSS Transitions

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:

Author: Chris Minnick

Chris is a prolific author and trainer, and the CEO of WatzThis?. His published books include Writing Computer Code, JavaScript for Kids, Coding with JavaScript For Dummies, Beginning HTML5 and CSS3 For Dummies, Webkit For Dummies, CIW eCommerce Certification Bible, and New Riders' XHTML.