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:

transitionShorthand for setting all four properties in one statement. transition:width 3s ease 1s
transition-propertySpecific property/ies for which transition will occur, or all (default) or none. transition-property: width, top
transition-durationLength, 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-functionDefines 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-delayDelay, 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:trans1:hover

Related Articles

  1. Learn the Very Basics of CSS in One Minute
  2. How to Create a CSS External Style Sheet
  3. How to Align Text with CSS
  4. How to Create a Horizontal Navigation Menu with CSS
  5. How to Create a Fixed-Width Layout with CSS
  6. How to Remove Spacing Between Table Borders with CSS
  7. How to Set a Background Image with CSS
  8. How to Set Text Spacing and Placement in CSS
  9. How to Style a Table with CSS
  10. How to Create Boxes with Rounded Corners in CSS
  11. How to Create a Vertical Navigation Menu with CSS
  12. How to Use the CSS Opacity Property
  13. How to Use Multiple Background Images with CSS
  14. Absolute Positioning with CSS
  15. How to Use the CSS Border Shorthand Property
  16. How to Create CSS Button Links
  17. How to Create a Fluid-Width Layout with CSS
  18. How to Set Text and Background Color with CSS
  19. How to Create a CSS Embedded Style Sheet
  20. How to Add Inline Styles to CSS
  21. How to Create a Border with CSS
  22. How to Use the CSS Padding Shorthand Property
  23. How to Create a Fly-Out Menu with CSS
  24. How to Use CSS Media Queries in Responsive Design
  25. How to Adjust Margins with CSS
  26. How to Use the CSS Background Shorthand Property
  27. How to Create a Form without Tables Using CSS
  28. How to Modify Fonts in CSS
  29. How to Create a Drop-Down Menu with CSS
  30. How to Apply Padding with CSS
  31. Fixed Positioning with CSS
  32. How to Use CSS Transitions (this article)
  33. How to Use the CSS list-style Shorthand Property
  34. How to Change Text Style in CSS
  35. How to Create CSS Sprites
  36. How to Use CSS with Different Media Types
  37. How to Import Style Sheets with @import in CSS
  38. How to Use the CSS White-Space Property
  39. How to Use the CSS Z-index Property
  40. How to Create Drop Shadows with the box-shadow Property in CSS3