Search code examples
htmlcssdropdownnav

Dropdown menu is being displayed horizontally instead of being vertical


I already did some search on some questions here on stack overflow and tested some solutions mentioned but it did not fix my problem. I'm new in coding.

I also did some research online but haven't found answers that fixed my problem.

-FIRST PROBLEM SOLVED-
-NEW PROBLEM DESCRIBED BELOW-

Edit: Dropdown menu positioning fixed, it is now on vertical. But the new problem is dropdown menu items quickly disappears before I hover on it. And it seems my login form was not attached inside the nav. Please check if the is a conflict or problem with my css and code. Can't fix it

Here is my code:

/*MY CSS CODE*/

* {
  text-decoration: none;
  box-sizing: border-box;
}

main {
  padding-top: 100px;
}

header {
  position: fixed;
  top: 0;
  right: 0;
  left: 0;
  background-color: #ffcce6;
  width: 100%;
  height: 100px;
}

header .header-brand {
  font-family: Catamaran;
  font-size: 24px;
  font-weight: 900;
  color: #111;
  text-transform: uppercase;
  display: block;
  margin: 0 auto;
  text-align: center;
  padding: 20px 0px;
}

header nav ul {
  display: block;
  margin: 0 auto;
  width: fit-content;
}

header nav ul li {
  display: inline-block;
  float: left;
  list-style: none;
  padding: 0 16px;
}

header nav ul li a {
  font-family: Catamaran;
  font-size: 16px;
  color: #111;
  text-transform: uppercase;
}

.dropdown {
  float: left;
  overflow: hidden;
  font-family: Catamaran;
  font-size: 16px;
  color: #111;
}

.dropdown .dropbtn {
  font-size: 16px;
  border: none;
  outline: none;
  background-color: inherit;
  font-family: inherit;
  margin: 0;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #ffe6f3;
  flex-direction: column;
  /* <--- here add this line */
  padding: 0;
  /* <--- this will fix the alignment */
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  z-index: 1;
}

.dropdown-content a {
  float: none;
  padding: 12px 8px;
  display: block;
  text-align: left;
}

.dropdown-content a:hover {
  background-color: hotpink;
}

.dropdown:hover .dropdown-content {
  display: flex;
  /* <-- replace 'block' by 'flex' */
}


/*NAVBAR LOGIN FORM*/

header nav .login-container {
  float: right;
  padding-top: 20px;
}

header nav .login-container form {
  display: inline;
}

header nav input[type=text],
header nav input[type=password] {
  padding: 6px;
  margin-top: 8px;
  font-family: 'New Tegomin', serif;
  font-size: 17px;
  border: 1px solid #ddd;
  border-radius: 5px;
  width: 180px;
  color: #333;
}

header nav .login-container button {
  position: relative;
  float: right;
  padding: 6px 10px;
  margin-top: 8px;
  margin-left: 6px;
  margin-right: 50px;
  background-color: deeppink;
  color: white;
  font-family: 'New Tegomin', serif;
  font-size: 17px;
  border: none;
  border-radius: 5px;
  width: 80px;
  cursor: pointer;
}

header nav .login-container button:hover {
  background-color: hotpink;
}

header .header-brand {
  margin: 31px 0;
  text-align: left;
  line-height: 38px;
  padding: 0 20px 0 40px;
  border-right: 3px solid #111;
  float: left;
}

header nav ul {
  margin: 20px 0px 0px 20px;
  float: left;
}

header nav ul li a {
  line-height: 60px;
}
<body>
  <header>
    <a href="index.php" class="header-brand">Team-Rocket</a>
    <nav>
      <ul>
        <li><a href="index.php">Home</a></li>
        <li><a href="products.php">Products</a></li>
        <li class="dropdown">
          <a class="dropbtn">Legit Check
              <i class="fa fa-caret-down"></i>
            </a>
          <ul class="dropdown-content">
            <li><a href="legitcheck.php">Search by ID</a></li>
            <li><a href="memberCheck.php">Search by Username</a></li>
          </ul>
        </li>
        <li class="dropdown">
          <a class="dropbtn">Member List
              <i class="fa fa-caret-down"></i>
            </a>
          <ul class="dropdown-content">
            <li><a href="list-RD.php">Regional Distributors</a></li>
            <li><a href="list-PD.php">Provincial Distributors</a></li>
            <li><a href="list-CD.php">City Distributors</a></li>
            <li><a href="list-RS.php">Reseller</a></li>
            <li><a href="list-SRS.php">Sub-Reseller</a></li>
          </ul>
        </li>
      </ul>
      <div class="login-container">
        <form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post">
          <input type="text" placeholder="Username" name="username">
          <input type="password" placeholder="Password" name="pwd">
          <button type="submit">Login</button>
        </form>
      </div>
    </nav>
  </header>
</body>


Solution

  • edit your .dropdown-content like this:

    .dropdown-content {
      display: none;
      position: absolute;
      background-color: #f9f9f9;
      flex-direction: column; /* <--- here add this line */
      padding: 0; /* <--- this will fix the alignment */
      min-width: 160px;
      box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
      z-index: 1;
    }
    

    and edit .dropdown:hover .dropdown-content like this:

    .dropdown:hover .dropdown-content {
      display: flex; /* <-- replace 'block' by 'flex' */
    }
    

    This will work properly.

    Here is the snippet:

    * {
      text-decoration: none;
      box-sizing: border-box;
    }
    
    main {
      padding-top: 100px;
    }
    
    header {
      position: fixed;
      top: 0;
      right: 0;
      left: 0;
      background-color: #ffcce6;
      width: 100%;
      height: 100px;
    }
    
    header .header-brand {
      font-family: Catamaran;
      font-size: 24px;
      font-weight: 900;
      color: #111;
      text-transform: uppercase;
      display: block;
      margin: 0 auto;
      text-align: center;
      padding: 20px 0px;
    }
    
    header nav ul {
      display: block;
      margin: 0 auto;
      width: fit-content;
    }
    
    header nav ul li {
      display: inline-block;
      float: left;
      list-style: none;
      padding: 0 16px;
    }
    
    header nav ul li a {
      font-family: Catamaran;
      font-size: 16px;
      color: #111;
      text-transform: uppercase;
    }
    
    .dropdown {
      float: left;
      overflow: hidden;
      font-family: Catamaran;
      font-size: 16px;
      color: #111;
    }
    
    .dropdown .dropbtn {
      font-size: 16px;
      border: none;
      outline: none;
      background-color: inherit;
      font-family: inherit;
      margin: 0;
    }
    
    .dropdown-content {
      display: none;
      position: absolute;
      background-color: #ffe6f3;
      flex-direction: column;
      /* <--- here add this line */
      padding: 0;
      /* <--- this will fix the alignment */
      min-width: 160px;
      box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
      z-index: 1;
    }
    
    .dropdown-content a {
      float: none;
      padding: 12px 8px;
      display: block;
      text-align: left;
    }
    
    .dropdown-content a:hover {
      background-color: hotpink;
    }
    
    .dropdown:hover .dropdown-content {
      display: flex;
      /* <-- replace 'block' by 'flex' */
    }
    
    
    /*NAVBAR LOGIN FORM*/
    
    header nav .login-container {
      float: right;
      padding-top: 20px;
    }
    
    header nav .login-container form {
      display: inline;
    }
    
    header nav input[type=text],
    header nav input[type=password] {
      padding: 6px;
      margin-top: 8px;
      font-family: 'New Tegomin', serif;
      font-size: 17px;
      border: 1px solid #ddd;
      border-radius: 5px;
      width: 180px;
      color: #333;
    }
    
    header nav .login-container button {
      position: relative;
      float: right;
      padding: 6px 10px;
      margin-top: 8px;
      margin-left: 6px;
      margin-right: 50px;
      background-color: deeppink;
      color: white;
      font-family: 'New Tegomin', serif;
      font-size: 17px;
      border: none;
      border-radius: 5px;
      width: 80px;
      cursor: pointer;
    }
    
    header nav .login-container button:hover {
      background-color: hotpink;
    }
    
    header .header-brand {
      margin: 31px 0;
      text-align: left;
      line-height: 38px;
      padding: 0 20px 0 40px;
      border-right: 3px solid #111;
      float: left;
    }
    
    header nav > ul {
      margin: 20px 0px 0px 20px;
      float: left;
    }
    
    header nav ul li a {
      line-height: 60px;
    }
    <header>
        <a href="index.php" class="header-brand">Team-Rocket</a>
        <nav>
          <ul>
            <li><a href="index.php">Home</a></li>
            <li><a href="products.php">Products</a></li>
            <li class="dropdown">
              <a class="dropbtn">Legit Check
                  <i class="fa fa-caret-down"></i>
                </a>
              <ul class="dropdown-content">
                <li><a href="legitcheck.php">Search by ID</a></li>
                <li><a href="memberCheck.php">Search by Username</a></li>
              </ul>
            </li>
            <li class="dropdown">
              <a class="dropbtn">Member List
                  <i class="fa fa-caret-down"></i>
                </a>
              <ul class="dropdown-content">
                <li><a href="list-RD.php">Regional Distributors</a></li>
                <li><a href="list-PD.php">Provincial Distributors</a></li>
                <li><a href="list-CD.php">City Distributors</a></li>
                <li><a href="list-RS.php">Reseller</a></li>
                <li><a href="list-SRS.php">Sub-Reseller</a></li>
              </ul>
            </li>
          </ul>
          <div class="login-container">
            <form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post">
              <input type="text" placeholder="Username" name="username">
              <input type="password" placeholder="Password" name="pwd">
              <button type="submit">Login</button>
            </form>
          </div>
        </nav>
      </header>