Search code examples
cssmenunested-listsoverlapoverlapping

li overlapping in a nested list (Navigation menu)


I'm trying to create a navigation menu, I've been following this tutorial: https://www.youtube.com/watch?v=vb_u7mj84Tc&ab_channel=TheWebShala I'm stuck at minute 28:56 where the submenu should push down the menu's li but instead it is overlapping (At max-width: 960px) . I did make a few modifications to the video's code but nothing that should interfere with this, mostly aesthetic changes.

Here's the code that I have so far:

https://codepen.io/Daniel-Haro-the-styleful/pen/RwEjrNG

:root {
  --color1: 115, 2, 12;
  /*rgb(115, 2, 12) #73020C Principal*/
  --color2: 217, 74, 86;
  /*rgb(217, 74, 86) #D94A56 Secundario*/
  --color3: 191, 4, 38;
  /*rgb(191, 4, 38) #BF0426 tres*/
  --color4: 242, 235, 220;
  /*rgb(242, 235, 220) #F2EBDC cuatro*/
  --color5: 13, 13, 13;
  /*rgb(13, 13, 13) #0D0D0D cinco*/
  --background: 88, 5, 12;
  /*rgb(88, 5, 12)*/
  --black: 13, 13, 13;
  /*rgb(13, 13, 13)  #0D0D0D*/
  --white: 242, 235, 220;
  /*rgb(242, 235, 220) #F2EBDC*/
}

body {
  min-height: 100vh;
  max-width: 100%;
  color: rgb(var(--black));
  background-color: rgb(var(--background));
  font-family: 'Anton', sans-serif;
  font-weight: 400;
  font-size: 1.125rem;
}

img {
  display: block;
}

a {
  text-decoration: none;
  color: inherit;
}

ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
}

.title {
  font-family: 'SweetDreams', serif;
  font-weight: 700;
}

.table,
.header h1,
.main h2 {
  color: rgb(var(--white));
}

.nav-menu {
  font-size: .8rem;
  color: rgb(var(--color4));
}


/*Inicio tutorial*/

.header {
  background-color: rgb(var(--color5));
  position: absolute;
  width: 100%;
  left: 0;
  top: 0;
  z-index: 99;
}

.header h1 {
  margin: 0;
  width: 105px;
}

.header-main {
  max-width: 1600px;
  margin: auto;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 5px 20px;
}

.logo {
  padding: 0 15px;
}

.nav-menu {
  padding: 0 15px;
}

.menu-item {
  display: inline-block;
  margin-left: 30px;
  position: relative;
  height: 40px;
}

.plus {
  display: inline-block;
  height: 10px;
  width: 10px;
  position: relative;
  margin-left: 5px;
  pointer-events: none;
}

.menu-item>a {
  display: block;
  padding: 12px 0;
  transition: all 0.3s ease;
}

.plus::before,
.plus::after {
  content: '';
  position: absolute;
  left: 50%;
  top: 50%;
  background-color: rgb(var(--color4));
  height: 2px;
  width: 100%;
  transform: translate(-50%, -50%);
  transition: all 0.3s ease;
}

.plus::after {
  transform: translate(-50%, -50%) rotate(-90deg);
}

.menu-item:hover>a .plus::before,
.menu-item:hover>a .plus::after {
  background-color: green;
}

.menu-item:hover>a {
  color: green;
}

.sub-menu {
  display: none;
  width: 105px;
  position: absolute;
  left: 0;
  top: 100%;
  background-color: rgb(var(--color5));
  border-top: 3px solid green;
  transition: all 0.3s ease;
}

.sub-menu--level2 {
  display: none;
  background-color: rgb(var(--color5));
  position: absolute;
  top: 0;
  right: 100%;
  transform: translateY(-3px);
  width: 108px;
}

.sub-menu--level2>.menu-item:hover {
  border-right: 3px solid green;
}

@media(min-width: 961px) {
  .menu-item--has-children:hover>.sub-menu,
  .menu-item--has-children:hover>.sub-menu--level2 {
    display: initial;
  }
  .menu-item--has-children:hover>a .plus::after {
    transform: translate(-50%, -50%) rotate(0deg);
  }
}

.sub-menu>.menu-item,
.sub-menu--level2>.menu-item {
  margin-left: 0;
  display: block;
}

.sub-menu>.menu-item>a,
.sub-menu--level2>.menu-item>a {
  display: block;
  padding: 15px 10px;
}

.open-nav-menu {
  height: 34px;
  width: 40px;
  position: relative;
  display: none;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.open-nav-menu span {
  display: block;
  height: 3px;
  width: 24px;
  background-color: rgb(var(--color4));
  position: relative;
}

.open-nav-menu span::before,
.open-nav-menu span::after {
  content: '';
  position: absolute;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgb(var(--color4));
  top: -7px;
}

.open-nav-menu span::after {
  top: 7px;
}

.close-nav-menu {
  height: 40px;
  width: 40px;
  background-color: rgb(var(--color4));
  margin: 0 0 15px 15px;
  cursor: pointer;
  display: none;
  align-items: center;
  justify-content: center;
}

.close-nav-menu img {
  width: 30px;
}

@media(max-width: 960px) {
  .nav-menu {
    position: fixed;
    top: 0;
    right: 0;
    width: 280px;
    height: 100%;
    background-color: rgb(var(--color5));
    overflow-y: auto;
    z-index: 1000;
    padding: 15px 0;
    transition: all 0.5s ease;
  }
  .menu-item {
    display: block;
    margin: 0;
  }
  .menu-item>a {
    padding: 12px 15px;
    border-bottom: 1px solid rgba(var(--color4), .3);
  }
  .menu-item:first-child>a {
    border-top: 1px solid rgba(var(--color4), .3);
  }
  .menu-item--has-children>a {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .sub-menu {
    width: 100%;
    display: initial;
    position: relative;
    left: auto;
    top: auto;
    border: none;
  }
  .open-nav-menu,
  .close-nav-menu {
    display: flex;
  }
}
<!-- Header Start -->
<header class="header">
  <div class="header__container">
    <div class="header-main">
      <h1 class="title logo"><a href="https://www.proximaestacion.lat">Próxima Estación</a></h1>
      <div class="open-nav-menu">
        <span></span>
      </div>
      <!-- navigation menu start -->
      <nav class="nav-menu">
        <div class="close-nav-menu">
          <img src="./assets/icons/cross.png" alt="exit icon">
        </div>
        <ul class="menu">
          <li class="menu-item menu-item--has-children"><a href="#">Variedades <i class="plus"></i></a>
            <ul class="sub-menu">
              <li class="menu-item menu-item--has-children"><a href="#">Item 1 <i class="plus"></i></a>
                <ul class="sub-menu--level2">
                  <li class="menu-item"><a href="#GT">sub-item 1 </a></li>
                  <li class="menu-item"><a href="#TRK">sub-item 2</a></li>
                  <li class="menu-item"><a href="#AA+">sub-item 3</a></li>
                  <li class="menu-item"><a href="#JF">sub-item 4</a></li>
                </ul>
              </li>
            </ul>
          </li>
          <li class="menu-item"><a href="#tablaDePrecios">Tabla de precios</a></li>
          <li class="menu-item menu-item--has-children"><a href="#">Contactos <i class="plus"></i></a>
            <ul class="sub-menu">
              <li class="menu-item"><a href='#' target="_blank">Instagram</a></li>
              <li class="menu-item"><a href="#" target="_blank">Whatsapp</a></li>
            </ul>
          </li>
        </ul>
      </nav>
      <!-- navigation menu end -->
    </div>
  </div>
</header>
<!-- Header end -->

There's also a sub-sub-menu with the same issue

I'm sure the answer is right on my face but I've been at it for hours and I can't figure it out. Any help would be greatly appreciated.

TIA

I thought it had to be a position problem, so I triple checked every instance where position was being used but saw nothing wrong with that. I added and removed all of this position: relative/absolute but the problem persisted.

In the video they use opacity and visibility to hide the submenus I instead used display: none/initial. However I already changed it to opacity and visibility like in the video but the problem persists so that ain't it.

I read similar questions and it's usually the position that's causing the issue but as I said I already tried that and can't figure it out.


Solution

  • Try removing the height: 40px; from .menu-item.

    .menu-item {
        display: inline-block;
        margin-left: 30px;
        position: relative;
        height: 40px; /* Remove me */
    }