20

Using CSS I'm trying to get a transition delay to work as follows. I want a 1s delay in but I want 0s delay out.

transition: width 0.3s ease-in 1s;
transition: width 0.3s ease-out 0s;

I have the following jsFiddle

ngplayground
  • 20,365
  • 36
  • 94
  • 173
  • Like this [DEMO](https://jsfiddle.net/lmgonzalves/58jbuxL5/2/)? Also to override `delay` you only need `transition-delay: 1s;`. – lmgonzalves Jul 17 '15 at 15:42
  • possible duplicate of [Different CSS transition-delays for hover and mouseout?](http://stackoverflow.com/questions/5921073/different-css-transition-delays-for-hover-and-mouseout) -- only difference is the hover, but the basic answer is the same and the trigger event is not defined in this question. – misterManSam Jul 17 '15 at 15:43

4 Answers4

28

Have the delay under your .active block, since the element has the active class when it is transitioning to green:

.sample {
    padding: 20px;
    background-color: #efefef;
    transition: background-color 0.3s ease-out 0s;
}
.sample.active {
    background-color: green;
    transition: background-color 0.3s ease-in 1s;
}

JSFiddle

George
  • 36,413
  • 9
  • 66
  • 103
5

slightly more concise and maintainable code, set the transition-delay property instead of rewriting the whole transition:

.sample {
    padding: 20px;
    background-color: #efefef;
    transition: background-color 0.3s ease-out 0s;
}
.sample.active {
    background-color: green;
    transition-delay: 1s;
}

demo

documentation: https://developer.mozilla.org/en/docs/Web/CSS/transition

Luca
  • 9,259
  • 5
  • 46
  • 59
2

The following worked for me:

.sample {
    padding: 20px;
    background-color: #efefef;
    transition: background-color 0.3s ease-in 0s;
}
.sample.active {
    background-color: green;
    transition-delay: 1s;
}

You don't need an !important declaration because the cascade automatically prefers the later rule when you overwrite a property.

You also don't need to rewrite the entire transition rule since you specifically want to use the same transition with a different delay rather than a different transition.

The reason it's this way around (with the 0s delay by default) is that when you remove the .active class you stop applying its colour as well as its transition delay so the delay in the .sample class is applied.

Drew Green
  • 51
  • 1
  • 4
1

Try this

CSS

.sample {
    padding: 20px;
    background-color: #efefef;
    -webkit-transition: background-color 0.3s ease-out 0s;
    transition: background-color 0.3s ease-out 0s;
}
.sample.active {
    background-color: green;
    -webkit-transition: background-color 0.3s ease-in 1s !important;
    transition: background-color 0.3s ease-in 1s !important;
}

DEMO HERE

Luís P. A.
  • 9,524
  • 2
  • 23
  • 36