Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different speeds for CSS transition

I am using css transitions to animate the height of a div. What I would like to do is have a slow transition in and a fast transition out.

It is for a dropdown menu and although the drop down works great when you hover over a menu item initially, when you move along to the next item I want the previous dropdown to snap back almost immediately.

div {
  width: 100px;
  height: 100px;
  background: red;
  -webkit-transition: width 2s;
  /* For Safari 3.1 to 6.0 */
  transition: width 2s;
}
div:hover {
  width: 300px;
}
<div></div>
<p>Hover over the div element above, to see the transition effect.</p>

Is there a way to set a different duration for the in and out transitions?

like image 366
fightstarr20 Avatar asked Sep 18 '25 23:09

fightstarr20


1 Answers

Place the slower transition on the div:hover and the faster transition on the div itself. When the :hover state is left, the divs transition will take over.

Example

div {
  width: 100px;
  height: 100px;
  background: red;
  transition: width 0.2s;
}
div:hover {
  width: 300px;
  transition: width 2s;
}
<div></div>

<p>Hover over the div element above, to see the transition effect.</p>
like image 187
misterManSam Avatar answered Sep 20 '25 16:09

misterManSam