DigitalOcean provides cloud products for every stage of your journey. Get started with $200 in free credit!
The transition
property is a shorthand property used to represent up to four transition-related longhand properties:
.element {
transition: background-color 0.5s ease;
}
Syntax
transition: [transition-property] [transition-duration] [transition-timing-function] [transition-delay];
Demo
transition
properties allow elements to change values over a specified duration, animating the property changes, rather than having them occur immediately. So, if we have, say, a box with a red background that we want to change to a green background when it is hovered, we can reach right for the transition
property to move between background colors:
Specifying which properties to transition
Notice that we’ve called out the background-color
property in the transition
declaration. That tells the browser that we’re planning to transition background colors and nothing else when a transition occurs between hovered and non-hovered states. Notice, too, that we told the browser that the transition should take two seconds and follow and ease-out
timing function, which means the transition starts fast, then slows down towards the end.
You can specify a particular property as we have above, or use a value of all
to refer to transition properties:
.element {
transition: all 0.5s ease;
background: red;
padding: 10px;
}
.element:hover {
background: green;
padding: 20px;
}
all
specified for the transition-property
portion of the shorthand. You may also comma separate value sets to do different transitions on different properties:
.element {
transition: background 0.2s ease,
padding 0.8s linear;
}
For the most part, the order of the values does not matter — unless a delay is specified. If you specify a delay, you must first specify a duration. The first value that the browser recognizes as a valid time value will always represent the duration. Any subsequent valid time value will be parsed as the delay.
Some properties cannot be transitioned because they are not animatable properties. See the spec for a full list of which properties are animatable.
By specifying the transition on the element itself, you define the transition to occur in both directions. That is, when the styles are changed (e.g. on hover on), they properties will transition, and when the styles change back (e.g. on hover off) they will transition. For example, the following demo transitions on hover, but not on hover off:
Browser support
This browser support data is from Caniuse, which has more detail. A number indicates that browser supports the feature at that version and up.
Desktop
Chrome | Firefox | IE | Edge | Safari |
---|---|---|---|---|
4* | 5* | 10 | 12 | 5.1* |
Mobile / Tablet
Android Chrome | Android Firefox | Android | iOS Safari |
---|---|---|---|
127 | 127 | 2.1* | 6.0-6.1* |
transition
CSS tricks that use 4 Ways to Animate the Color of a Text Link on Hover
6 Creative Ideas for CSS Link Hover Effects
Adam Argyle’s Sick Mouse-Out CSS Hover Effect
All You Need to Know About CSS Transitions
Animated Media Queries
Animating Border
Build Complex CSS Transitions using Custom Properties and cubic-bezier()
Controlling CSS Animations and Transitions with JavaScript
Cool Hover Effects That Use Background Properties
Cool Hover Effects That Use CSS Text Shadow
CSS Animations and Transitions in Email
CSS Grid Can Do Auto Height Transitions
CSS Transition Timing
Different Transitions for Hover On / Hover Off
How to Create a Realistic Motion Blur with CSS Transitions
Maintaining CSS Style States using “Infinite” Transition Delays
Link Underlines That Animate Into Block Backgrounds
Moving Backgrounds With Mouse Position
Recreating Netlify’s Neat-o Sliding Button Effect
Shared Element Transitions
Staggered CSS Transitions
Transition Delay Delays
The State of Changing Gradients with CSS Transitions and Animations
Transitioning Gradients
Transitions and Animations on CSS Generated Content
Using CSS Transitions on Auto Dimensions
Using Multi-Step Animations and Transitions
You Can Transition z-index
More information
- CSS Transitions Specification (W3C)
- CSS Transition Shorthand Property (DigitalOcean)
- An Interactive Guide to CSS Transitions (Josh Comeau)
- CSS
will-change
Property: When and When Not to Use It (DigitalOcean)
http://www.css3.info/preview/css3-transitions/
Thanks for all the goodies, Chris…I return to the site often for so many great tips and, well, “tricks.”
tricks it is :) Thanks !
Thanks for this Chris. Your link to Adobe’s comprehensive guide tries to go to a subdirectory of your site. Might want to fix it.
Hi guys. This didn’t work for me in Firefox when using div image backgrounds. It works fine on Webkit browsers though. Any ideas?
HJ, do you have the “-moz-transition:” in your CSS? Just making sure… And what version of FF? What OS?
Hi HJ, yes i do have the -moz-transition. I’m running Firfox 23.0.1 and Mac OS X Mavericks.
I can confirm this. For some reason Firefox in OS X will apply both transitions on the hover over and hover out states, even when there is no transition information in the hover state.
Hi Guys,
This didn’t work while changing css3 background gradients and background images as well. How do i fix that? Please help me.
Here is the code I used.
It looks like you’re using two separate elements – the first line targets the LI while the second targets the A:HOVER
Try this instead:
Although I’m not sure how the change between non repeating and repeating backgrounds will be handled.
“background-image 3s ease;”
Yeah, linear gradient does not work for me either
I found the tutorial useful and used some of the technique in my own website. Thanks.
thanks Chris for all your
help. ;->
This is very good article for freshers
i used in my applicaiton
http://oldc.in
Hi, I have a problem! The css works, but unlike the examples I’ve seen, mine automatically plays when the page loads:
The problem here is that when the page loads, it does an animation from no shadow to shadow, and then on hover does what it has to (black to blue sh.). Is there any way to disable the first animation?
box
box:hover
I think you need the “-in” or “-out” portion for the easing, no?
Nope, “ease”, “ease-in”, “ease-out” and “ease-in-out” are four different properties, and they cannot be the attribute of “box-shadow”.
Oops, I meant it would only go to the ‘transition’ attribute. Not on the ‘box-shadow’. So you don’t want the effect when the page loads, then remove the ‘transition’ from #box to #box:hover. Unless I am missing something.
Do you have a link to a page we could see?
Okay, I got it, some of the elements in <form> was conflicting, like input type=”email”, and it caused a it to render improperly. I dunno why maybe just html5 bugs.
Oh, okay, now it’s working fine. Because I removed the “border-image” property O.O. I have no idea why XD.
Oh, wait no. Not that property. The original still doesn’ work properly, but I made an other copy of the css and the index file, where I only included that div element and it’s formatting in the css, and it worked fine. So it must be conflicting with other elements, so I guess I can’t get more help :/.
Hi guys. My transitions work fine in IE and Chrome but not FF. I am running 28.0, OS Mavericks.
I borrowed this code from https://css-tricks.com/downloads/css-stuff/ but I cant get it to work in FF. IE & Chrome all good.
Very much learning stage for me.
Thank you for the help.
#banner {
width: 600px;
height: 200px;
margin: 0px 10px 30px 50px;
border: 8px solid #eee;
-webkit-box-shadow: 1px 1px 3px rgba(0,0,0,0.75);
border-radius:5px;
}
#banner div {
position: absolute;
}
#banner div:nth-child(2) {
}
@-webkit-keyframes wipe {
0% {
-webkit-mask-position: 0 0;
}
100% {
-webkit-mask-position: -1000px -1000px;
}
}
#rollover {
width: 250px;
height: 250px;
position: relative;
border: 8px solid #eee;
-webkit-box-shadow: 1px 1px 3px rgba(0,0,0,0.75);
}
#rollover div {
position: absolute;
top: 0;
left: 0;
}
#rollover div:nth-child(2) {
}
#rollover div:nth-child(2):hover {
-webkit-mask-position: -300px -300px;
}
prefix -moz- to all your css3 codes and make another set of it as -webkit- is added
like:-
@-webkit-keyframes wipe {
0% {
-webkit-mask-position: 0 0;
}
100% {
-webkit-mask-position: -1000px -1000px;
}
}
/for firefox/
@-moz-keyframes wipe {
0% {
-moz-mask-position: 0 0;
}
100% {
-moz-mask-position: -1000px -1000px;
}
}
Hello,
Would you please let me know how to change the
rotateX
to simple sliding effect, in this javascript code.I appreciate your help.
thanks
thank you i love css3 and i like the tutoriel (y)
So, I’ve yet to see this really documented anywhere… however extremely useful in my opinion..
As I’m sure many other’s agree, being confined to transitions being triggered via hovering, can be quite a pain, and leaving this awesome feature nearly USELESS at times! (and then forcing the use of javascript in cases we should NOT have to) ..
HOWEVER, I’ve just discovered by tinkering around with STYLE ELEMENT’s ‘MEDIA’ Attribute in combination with CSS3’s transition effects, we are VERY MUCH ABLE to trigger the transitions via a LIST of other events, WITHOUT the alternate use of ANY javascript/jquery what-so-ever for animations/fades/element-moving on window-resize, etc!
(ex: [ STYLE MEDIA=”all and (min-width: 600px)” ] )
Leaving us actually quite a large number of new options, such as screen orientation: landscape/portrait; useful for mobile devices, etc! — find the full list on w3schools!
I thought this was pretty awesome myself… thought I’d share.
I’m sure Chris already new of this though…(lol) — He’s always had everything rearranging quite nicely upon window shrinking etc … (although I never actually LOOKED at the css-tricks.com code to be POSITIVE if this IS actually his method or not; but it definitely seems like it could’ve been, as the transitions are equally as smooth, etc..however, I must say, now that I just checked, the OLD css-tricks design USED-TO demonstrate this MUCH more than it does currently).
Enjoy :)
sdfsaf
Can we change the text color on page load without mouse hover. Just like a gif animation?
yes you can by using @keyframes rule. please read http://www.w3schools.com/css/css3_animations.asp and practice examples.
I’m still confusing with:
-webkit-transition-timing-function:
‘s value…
Nice article. Please write about transitions using svg. You can check the thumbnails at this 3D printer site
css:
.my-gallery .my-icon-gallery
{
position: absolute;
z-index: 10000;
top: 50%;
left: 50%;
right: 0;
-webkit-transform: translate(-50%, -50%);
-moz-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
width: 80px;
height: 80px;
display: inline-block;
fill: rgba(255, 255, 255, 0);
stroke: rgba(255, 255, 255, 1);
stroke-width: .05em;
stroke-opacity: 1;
stroke-linecap: butt;
stroke-linejoin: bevel;
stroke-dasharray: 0, 150;
-webkit-transition: fill 1.5s, stroke-dasharray .75s, stroke-opacity 1.5s ease;
-moz-transition: fill 1.5s, stroke-dasharray .75s, stroke-opacity 1.5s ease;
-ms-transition: fill 1.5s, stroke-dasharray .75s, stroke-opacity 1.5s ease;
-o-transition: fill 1.5s, stroke-dasharray .75s, stroke-opacity 1.5s ease;
transition: fill 1.5s, stroke-dasharray .75s, stroke-opacity 1.5s ease;
}
js: (when doc is ready)
var url =’/css/simpleline-icons.svg’;
var c=new XMLHttpRequest(); c.open(‘GET’, url, false); c.setRequestHeader(‘Content-Type’, ‘text/xml’); c.send();
document.body.insertBefore(c.responseXML.firstChild, document.body.firstChild)
.my-gallery:hover > .my-icon-gallery
{
fill: rgba(255, 255, 255, 1);
stroke-dasharray: 150, 1;
stroke-opacity: 0;
}
.my-icon-gallery:after
{
content: ”;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
html:
Looks good :)
Such a simple thing but it can make a huge difference to how a site feels.
For those who want their transition go from red to green through orange, Just set the
background-color
usingHSL
(Hue, Saturation, Lightness). For example : http://codepen.io/anon/pen/xbzGeothanks for your time
To add a separate transitions to hover-over and hover-off, include the -off transition on the element style and the -on transition on the :hover style. I forked the CodePen above as an example.
I got a doubt. Can I add a overlay text when hovering my picture div?
Please help me.
yes You can Do That
CSS
.container {
position:relative;
overflow:hidden;
}
.container .textbox {
width:200px;
height:150px;
position:absolute;
top:0;
left:0;
margin-top:-160px;
border-radius:5px;
background-color: rgba(0,0,0,0.75);
-webkit-box-shadow: inset 0px 0px 5px 2px rgba(255,255,255,.75);
box-shadow: inset 0px 0px 5px 2px rgba(255,255,255,.75);
}
.container:hover .textbox {
margin-top:0;
}
.text {
padding-top: 50px;
}
.textbox {
-webkit-transition: all 0.7s ease;
transition: all 0.7s ease;
}
HTML
TEXT
I am currently having an issue with the transitioning. I am not sure whether I have too many going on, or if I have missed something. The hover CSS and transition stuff is below. I am trying to get an image to, on hover, move to the left.
#hoverpage {
width: 500px;
height: 334px;
position: relative;
overflow-x: hidden;
-webkit-transition: all 1s ease;
-moz-transition: all 1s ease;
-o-transition: all 1s ease;
}
.insidehover {
-webkit-transition: all 1s ease;
-moz-transition: all 1s ease;
-o-transition: all 1s ease;
position: absolute;
width: 500px;
height: 334px;
}
#hoverpage:hover .insidehover {
-webkit-transition: all 1s ease;
-moz-transition: all 1s ease;
-o-transition: all 1s ease;
left: -500px;
}
Seriulsly, thanks a lot. I was looking everywhere how to return to the normal state of a link whit the ease transition.
Thaks a lot, it´s was very helpful
Thank you. really need
How I just zoom the image? Which transition I use?
I just love css-tricks.com <3
Nice tutorial as usual, but I wanted to let you know that the red/green transition is not the best color choice. As you probably know red/green color blindness is quite common, and I personally can’t see any change in color at all.
What’s the point of this transition effect if it doesn’t work on iOs ?
This tutorial actually explained transitions better than w3schools and most of the tutorials out there that I visited to understand this concept.
Great post!
Was having difficulties trying to animate “background-color”.
Aftering coming here and changed to just “background” and it worked!!
body {
font-family: “Helvetica Neue”,Helvetica,Arial,sans-serif;
font-size: 18px;
/* line-height:22px;*/
}
div {
display:block;
}
h2 {
color:blue;
text-align:center;
}
header img {
float: left;
clear:right;
margin-bottom:.5%;
margin-left:20px;
margin-top:18px;
margin-right:20px;
}
header h4 {
font-size:30px;
float:right;
clear:left;
font-family:verdana;
color:blue;
clear:right;
margin-top:8px;
margin-right:2%;
}
header ul {
float:right;
clear:left;
list-style-type:none;
padding:0;
margin:0;
margin-top:-3%;
margin-right:2%;
}
header ul li a {
display: block;
padding: 0;
}
header ul li {
float:left;
border: 2px solid #ffffff;
border-radius:.4em;
margin: .5em;
}
#section {
margin-left:10%;
margin-top:2%;
text-align:center;
}
#main {
margin-left:3%;
float:left;
clear:right;
width:20%;
text-align:center;
}
in this after transition ends it jumps back can i end it slowly???
Hi all. Anybody know what is the default time of transition?
Or default time of responding on hover? Because a on my page won’t respond on quick moving mouse. The question is how long mouse should hover before onhover fires?
hi.
i was wondering is there a way to use :
.finger3:hover {
height: 200px;
transition: 1s ease-in-out;
}
in only one direction for ex just upward, so the bottom stay still and transition happen to the top of the element.
Prefixes -webkit, -moz, -o, is required?
-webkit-transition: background-color 500ms ease-out 1s;
-moz-transition: background-color 500ms ease-out 1s;
-o-transition: background-color 500ms ease-out 1s;
transition: background-color 500ms ease-out 1s;
List of animatable properties
https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_animated_properties
aha! the ‘all’ value of transition is now understood. thanks.
Amazing, this explained all.
A list of properties that explicity cannot be animated would be great (somewhere, not necessarily on ‘Tricks).
Anyone successfully animated an
img
element fromposition:unset;
display:none;
to
position:absolute;
display:block;
along with different
top
,right
values to get the img to actually move?Struggling with that myself.
position
is not included on the MDN list of properties that can be animated, howeverright
,left
,top
,bottom
are listed! sighWould you please give a best-practices example for transitioning between two background-color states using CSS variables?
Thank you!
Hey! Here’s a hover transition between two colors: