2

i have simple animation on div which change background-color on :hover, but, when mouse leave this div, the transition with initial color is not working

if someone has css or javascript solution it is welcome !

.arc_en_ciel{
    width: 300px;
    height: 300px;
    background-color: rgb(64, 137, 126);

    -webkit-transition: background-color 0.7s ease-in-out;
    -moz-transition: background-color 0.7s ease-in-out;
    -o-transition: background-color 0.7s ease-in-out;
    transition: background-color 0.7s ease-in-out;
}

.arc_en_ciel:hover{
    animation-name: animation;
    animation-duration: 4s;
    animation-timing-function: ease-in-out;
    animation-iteration-count: infinite;    
    animation-play-state: running;
}
@-webkit-keyframes animation{
    0%     {background-color: rgb(64, 137, 126);}
    50%  {background-color: #1f5e54;}
    100%  {background-color: rgb(64, 137, 126);}
}
@keyframes animation{
    0%     {background-color: rgb(64, 137, 126);}
    50%  {background-color: #1f5e54;}
    100%  {background-color: rgb(64, 137, 126);}
}
<div class="arc_en_ciel"></div>

3
  • Welcome to Stack Overflow! Without your HTML, CSS is useless to us. Please read how to create a minimal reproducible example. Commented May 19, 2021 at 22:01
  • 2
    animation is a reserved word in CSS, try renaming your animation something unique. Commented May 19, 2021 at 22:08
  • Do you mean transition doesn't work when color changing from default to color of animation and vise versa? (your color pallet is so subtle, very hard to see the difference, try more contrast colors) Commented May 20, 2021 at 2:00

1 Answer 1

2

The problem is that CSS animation animates the background color, but doesn't change the value.

There is no transition once you stop hovering because the color is the same.

You may get what you're looking for with a simple transition:

.arc_en_ciel{
    width: 300px;
    height: 300px;
    background-color: rgb(64, 137, 126);
    transition: background-color 0.7s ease-in-out;
}

.arc_en_ciel:hover{
    background-color: #1f5e54;
    transition: background-color 0.7s ease-in-out;
}
<div class="arc_en_ciel"></div>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.