CSS Dropdown animation delay is not working

Viewed 701

I'm trying to make a dropdown fade animation with 2s delay when hovering on an element, but adding animation-delay doesn't make things work, here is the html and css code:

@keyframes fade {
  from {
    opacity: 0;
    visibility: hidden;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    visibility: visible;
    transform: translateY(0);
  }
}

.dropdown {
    position: absolute;
    display: inline-block;
}
.dropdown-content {
    display: none;
    position: absolute;
    top: 40px;
    background-color: #f9f9f9;
    min-width: 330px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    padding: 12px 16px;
    z-index: 1;
    animation: fade 1s ease forwards;
    animation-delay: 2s;
}
.dropdown:hover .dropdown-content {
     display: block;
     margin-top: 0;
}
<div class="dropdown">
  <a href="www.google.com">
    some text
  </a>
  <div class="dropdown-content">   
    <p style="color: rgb(120, 120, 120);">
      more text
    </p>
  </div>
</div>

The fiddle is here: http://jsfiddle.net/ncr9xLbd/11/

After adding animation-delay, the original fade animation is not working anymore either. I'm new to CSS and couldn't figure out why this doesn't work, any help would be appreciated:)

2 Answers

You can't animate from display: none to display: block.

But that shouldn't be an issue, because you never need to apply display: none to .dropdown-content.

Instead, opacity: 0 should be sufficient.

Working Example:

@keyframes fade {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.dropdown {
    position: absolute;
    display: inline-block;
}

.dropdown .dropdown-content {
    position: absolute;
    top: 40px;
    background-color: #f9f9f9;
    min-width: 330px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    padding: 12px 16px;
    opacity: 0;
}

.dropdown:hover .dropdown-content {
    margin-top: 0;
    animation: fade 1s ease 2s forwards;
}
<div class="dropdown">
<a href="www.google.com">some text</a>
<div class="dropdown-content">   
<p style="color: rgb(120, 120, 120);">more text</p>
</div>
</div>

In your CSS Under the .dropdown-content You need to add as "transition-delay:5s;" and remove the "animation-delay: 2s;" It does not need your code.

Definition and Usage
The transition-delay property specifies when the transition effect will start.

The transition-delay value is defined in seconds (s) or milliseconds (ms).

Update Your CSS like this:

@keyframes fade {
  from {
    opacity: 0;
    visibility: hidden;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    visibility: visible;
    transform: translateY(0);
  }
}

.dropdown {
    position: absolute;
    display: inline-block;
}
.dropdown-content {
    display: none;
    position: absolute;
    top: 40px;
    background-color: #f9f9f9;
    min-width: 330px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    padding: 12px 16px;
    z-index: 1;
    animation: fade 1s ease forwards;
    transition-delay:1s;
}
.dropdown:hover .dropdown-content {
     display: block;
     margin-top: 0;
     transition-delay:1s;
}
<div class="dropdown">
            <a href="www.google.com">
                some text
            </a>
            <div class="dropdown-content">   
            <p style="color: rgb(120, 120, 120);">
              more text
            </p>
            </div>
</div>

Happy coding!

Related