Search code examples
cssdrop-down-menucss-transitionsmegamenu

Want to add transition-delay on hovering my mega drop-down menu


I am trying to add transition-delay property on my dropdown menu but it's not working. I don't know why and I have also searched different questions on stackoverflow but none of them is working for me. Here is my code:

<div class="navbar">
        <a href="#home">Home</a>
        <a href="#news">News</a>
        <div class="dropdown">
          <button class="dropbtn">Dropdown
            <i class="fa fa-caret-down"></i>
          </button>
          <div class="dropdown-content">
            <div class="header">
              <h2>Mega Menu</h2>
            </div>
            <div class="row">
              <div class="column">
                <h3>Category 1</h3>
                <a href="#">Link 1</a>
                <a href="#">Link 2</a>
                <a href="#">Link 3</a>
              </div>
              <div class="column">
                <h3>Category 2</h3>
                <a href="#">Link 1</a>
                <a href="#">Link 2</a>
                <a href="#">Link 3</a>
              </div>
              <div class="column">
                <h3>Category 3</h3>
                <a href="#">Link 1</a>
                <a href="#">Link 2</a>
                <a href="#">Link 3</a>
              </div>
            </div>
          </div>
        </div>
  </div>

My css code here:

/* Show the dropdown menu on hover */

.dropdown:hover .dropdown-content {
  display: block;
  
}

JSbin for my code

I have also used the visibility property instead of display but it's not working for me. Your answer will be a great addition to my knowledge. Thanks in advance.


Solution

  • You don't have any animation/transition in your code. And as you know you can't animate the display property. Instead use animation:

    /* Navbar container */
    .navbar {
      overflow: hidden;
      background-color: #333;
      font-family: Arial;
    }
    
    /* Links inside the navbar */
    .navbar a {
      float: left;
      font-size: 16px;
      color: white;
      text-align: center;
      padding: 14px 16px;
      text-decoration: none;
    }
    
    /* The dropdown container */
    .dropdown {
      float: left;
      overflow: hidden;
    }
    
    /* Dropdown button */
    .dropdown .dropbtn {
      font-size: 16px;
      border: none;
      outline: none;
      color: white;
      padding: 14px 16px;
      background-color: inherit;
      font: inherit; /* Important for vertical align on mobile phones */
      margin: 0; /* Important for vertical align on mobile phones */
    }
    
    /* Add a red background color to navbar links on hover */
    .navbar a:hover, .dropdown:hover .dropbtn {
      background-color: red;
    }
    
    /* Dropdown content (hidden by default) */
    .dropdown-content {
      display: none;
      position: absolute;
      background-color: #f9f9f9;
      width: 100%;
      left: 0;
      box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
      z-index: 1;
      /* addition to your existing code */
      opacity: 0;
      visibility: hidden;
      transition: opacity 500ms, visibility 500ms;
    }
    
    /* Mega Menu header, if needed */
    .dropdown-content .header {
      background: red;
      padding: 16px;
      color: white;
    }
    
    /* Show the dropdown menu on hover */
    
    .dropdown:hover .dropdown-content {
      display: block;
      /* addition to your existing code */
      visibility: visible;
      opacity: 1;
      animation: fade 1s;
      
    }
    /* addition to your existing code */
    @keyframes fade {
        0% {
            opacity: 0;
        }
    
        100% {
            opacity: 1;
        }
    }
    /* Create three equal columns that floats next to each other */
    .column {
      float: left;
      width: 33.33%;
      padding: 10px;
      background-color: #ccc;
      height: 250px;
    
    }
    
    /* Style links inside the columns */
    .column a {
      float: none;
      color: black;
      padding: 16px;
      text-decoration: none;
      display: block;
      text-align: left;
    }
    
    /* Add a background color on hover */
    .column a:hover {
      background-color: #ddd;
    }
    
    /* Clear floats after the columns */
    .row:after {
      content: "";
      display: table;
      clear: both;
    }
    @media screen and (max-width: 600px) {
      .column {
        width: 100%;
        height: auto;
      }
    }
    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width">
      <title>JS Bin</title>
    </head>
    
    <body>
    <div class="navbar">
            <a href="#home">Home</a>
            <a href="#news">News</a>
            <div class="dropdown">
              <button class="dropbtn">Dropdown
                <i class="fa fa-caret-down"></i>
              </button>
              <div class="dropdown-content">
                <div class="header">
                  <h2>Mega Menu</h2>
                </div>
                <div class="row">
                  <div class="column">
                    <h3>Category 1</h3>
                    <a href="#">Link 1</a>
                    <a href="#">Link 2</a>
                    <a href="#">Link 3</a>
                  </div>
                  <div class="column">
                    <h3>Category 2</h3>
                    <a href="#">Link 1</a>
                    <a href="#">Link 2</a>
                    <a href="#">Link 3</a>
                  </div>
                  <div class="column">
                    <h3>Category 3</h3>
                    <a href="#">Link 1</a>
                    <a href="#">Link 2</a>
                    <a href="#">Link 3</a>
                  </div>
                </div>
              </div>
            </div>
          </div>
    </body>
    </html>

    To learn more about animations/transitions you can look at: transitions animations