0

I am trying to apply transition to my dropdown but it is not working. Dropdown appears right after hover and effect is not visible.

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

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  padding: 12px 16px;
  z-index: 1;
  transform: translateY(-170px);
  transition: all 1s;
}

.dropdown:hover>.dropdown-content {
  display: block;
  background-color: yellowgreen;
  transform: translateY(20px);
}
<h2>Hoverable Dropdown</h2>
<p>Move the mouse over the text below to open the dropdown content.</p>
<div class="dropdown">
  <div class="dropdown-content">
    <p>Hello World!</p>
  </div>
  <span>Mouse over me</span>
</div>
biberman
  • 5,606
  • 4
  • 11
  • 35
devesh
  • 111
  • 2
  • 11

1 Answers1

0

display property is not animatable. You can animate opacity instead.
You can default the display to block.

<html>
  <head>
    <style>
      .dropdown {
        position: relative;
        display: inline-block;
      }
      .dropdown-content {
        display: block;
        position: absolute;
        background-color: #f9f9f9;
        min-width: 160px;
        box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
        padding: 12px 16px;
        z-index: 1;
        opacity: 0;
        transform: translateY(-170px);
        transition: all 1s;
      }
      .dropdown:hover > .dropdown-content {
        opacity: 1;
        background-color: yellowgreen;
        transform: translateY(20px);
      }
    </style>
  </head>
  <body>
    <h2>Hoverable Dropdown</h2>
    <p>Move the mouse over the text below to open the dropdown content.</p>
    <div class="dropdown">
      <div class="dropdown-content">
        <p>Hello World!</p>
      </div>
      <span>Mouse over me</span>
    </div>
  </body>
</html>
TechySharnav
  • 4,869
  • 2
  • 11
  • 29