Search code examples
htmlcsscss-positiondropdown

Border-Bottom conflicts with my dropdown menu(?)


This is my first project using HTML and CSS so I'm pretty much a newbie for this language. I think my dropdown menu is conflicting with the bottom-border because it won't show up. This is both my HTML and CSS code for it.

* {
  margin: 0;
  padding: 0;
  background-color: white;
  overflow-x: hidden;
}

body {
  font-family: Arial, Helvetica, sans-serif;
}

header {
  background-color: rgb(255, 255, 255);
  height: 600px;
}

.navbar {
  width: 85%;
  margin: auto;
  padding: 60px 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.logo {
  width: 160px;
  cursor: pointer;
  margin-right: auto;
}

.navbar .dropdown {
  display: none;
}

.navbar ul li:hover .dropdown {
  display: block;
}

.navbar ul li {
  list-style: none;
  display: inline-block;
  margin: 0 20px;
  position: relative;
}

.navbar ul li a {
  text-decoration: none;
  color: black;
}

.navbar ul li::after {
  content: '';
  height: 3px;
  width: 0;
  background: teal;
  position: absolute;
  left: 0;
  bottom: 0px;
  transition: 0.5s;
}

.navbar ul li:hover::after {
  width: 100%;
}

button {
  list-style: none;
  border: none;
  background: teal;
  padding: 10px 20px;
  border-radius: 30px;
  color: white;
  font-size: 15px;
  transition: 0.4s;
}
<header>
  <div class="banner">
    <div class="navbar">
      <img src="icon.png" class="logo">
      <ul>
        <li><a href="#">Home</a></li>
        <li><a href="#">Products</a></li>
        <ul class="dropdown">
          <li><a href="#">Healthcare</a></li>
          <li><a href="#">Cosmetic</a></li>
          <li><a href="#">Misc.</a></li>
        </ul>
        <li><a href="#">About Us</a></li>
        <li><a href="#">Register</a></li>
      </ul>
      <button type="button"><a href="#"></a>Login</button>
    </div>
  </div>
</header>

I already tried many options from various sources and It still won't work. I think the problem lays somewhere between the code for the border-bottom but I'm not sure.


Solution

  • There's a lot to fix in your code. I fixed the essential things, among these:

    • The dropdown ul should be a parent of its main menu li

    • position: absolute and position: relative for the dropdown and its parent li, and according topand margin settings for the dropdown and its li children

    • No general overflow: hidden, since this will hide the submenu being an absolutely positioned child of another element

    • some other CSS details - see below

    * {
      margin: 0;
      padding: 0;
      background-color: white;
    }
    
    body {
      font-family: Arial, Helvetica, sans-serif;
    }
    
    header {
      background-color: rgb(255, 255, 255);
      height: 600px;
    }
    
    .navbar {
      width: 85%;
      margin: auto;
      padding: 60px 0;
      display: flex;
      align-items: center;
      justify-content: space-between;
    }
    
    .logo {
      width: 160px;
      cursor: pointer;
      margin-right: auto;
    }
    
    .navbar .dropdown {
      display: none;
    }
    
    .navbar ul li:hover .dropdown {
      display: block;
    }
    .navbar > ul > li {
      position: relative;
    }
    .navbar > ul > li > .dropdown {
      position: absolute;
      top: 1.4em;
    }
    .navbar .dropdown li {
      display: block;
      margin: 0;
    }
    .navbar ul li {
      list-style: none;
      display: inline-block;
      margin: 0 20px;
      position: relative;
    }
    
    .navbar ul li a {
      text-decoration: none;
      color: black;
    }
    
    .navbar ul li::after {
      content: '';
      height: 3px;
      width: 0;
      background: teal;
      position: absolute;
      left: 0;
      bottom: 0px;
      transition: 0.5s;
    }
    
    .navbar ul li:hover::after {
      width: 100%;
    }
    
    button {
      list-style: none;
      border: none;
      background: teal;
      padding: 10px 20px;
      border-radius: 30px;
      color: white;
      font-size: 15px;
      transition: 0.4s;
    }
    <header>
      <div class="banner">
        <div class="navbar">
          <img src="icon.png" class="logo">
          <ul>
            <li><a href="#">Home</a></li>
            <li><a href="#">Products</a>
              <ul class="dropdown">
                <li><a href="#">Healthcare</a></li>
                <li><a href="#">Cosmetic</a></li>
                <li><a href="#">Misc.</a></li>
              </ul>
            </li>
            <li><a href="#">About Us</a></li>
            <li><a href="#">Register</a></li>
          </ul>
          <button type="button"><a href="#"></a>Login</button>
        </div>
      </div>
    </header>