Wojciech Mierzejewski
Wojciech Mierzejewski

Reputation: 121

hamburger menu with submentu

I am trying to implement hamburger menu with a submenu. So far seems quite ugly, specially regarding the last item. As it is obvoius, I have a problem with hover over Dropdown1. It looks like hover over Dropdown1 item is inproper and hamburger background coverage is also incorrect.

menu

function myFunction() {
  var x = document.getElementById("myTopnav");

  if (x.className === "topnav") {
    x.className += " responsive";
  } else {
    x.className = "topnav";
  }
}
.topnav {
  background-color: #333;
  overflow: visible;
}
.topnav a {
  float: left;
  display: block;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
}
.menuitems a {
  float: left;
  display: block;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
}
.topnav a:hover {
  background-color: #ddd;
  color: black;
}
.menuitems li a:hover {
  background-color: #ddd;
  color: black;
}
.topnav a.active {
  background-color: #808080;
  color: white;
}
.topnav .icon {
  display: none;
}
@media screen and (max-width: 600px) {
  .topnav a:not(:first-child) {display: none;}
  .topnav a.icon {
    float: right;
    display: block;
  }
}
@media screen and (max-width: 600px) {
    .topnav li:first-child > a {
    display: none;
  }
}
@media screen and (max-width: 600px) {
  .topnav.responsive {position: relative;}
  .topnav.responsive a.icon {
    position: absolute;
    right: 0;
    top: 0;
  }
  .topnav.responsive a {
    float: none;
    display: block;
    text-align: left;
  }
  .topnav.responsive  li > a {
    float: none;
    display: block;
    text-align: left;
  }
}
<div class="topnav" id="myTopnav">
  <a href="#home" class="active">Home</a>
    <a href="#news">News</a>
    <a href="#contact">Contact</a>
      <ul>
        <li>
          <a href="#Dropdown">Dropdown</a>
          <ul class="menuitems">
            <li>
              <a href="#Dropdown1">Dropdown1</a>
            </li>
          </ul>
        </li>
      </ul>
    <a href="javascript:void(0);" class="icon" onclick="myFunction()">
  </a>
</div>

Upvotes: 1

Views: 375

Answers (1)

Bjorn.B
Bjorn.B

Reputation: 1705

The background issue looked like it's because your floats weren't being cleared on .topnav a. I created a working example for you to compare your code to, and shuffled your code a little.

I switched floats to display: flex, and adjusted the dropdown nav. Sometimes floats are harder to keep track of so display: flex can simplify that a tad. The one thing I didn't do was setup the hover on mobile. Obviously you can't hover on mobile so the .menuitems would need to be adjusted using a click event to trigger show/hide.

function myFunction() {
    var x = document.getElementById("myTopnav");
        if (x.className === "topnav") {
            x.className += " responsive";
        } else {
            x.className = "topnav";
    }
}
.topnav {
    background-color: #333;
    overflow: visible;
}
.topnav a {
    padding: 14px 16px;
    text-decoration: none;
    color: #fff;
}
.topnav ul {
    margin: 0;
    padding: 0;
    display: flex;
}
.topnav li {
    vertical-align: top;
    list-style-type: none;
    font-size: 17px;
    position: relative;
    width: auto;
}
.topnav li > a {
    text-align: center;
    color: #f2f2f2;
    text-decoration: none;
    font-size: 17px;
    display: block;
}
.topnav li > a:hover {
    background-color: #ddd;
    color: black;
}
.topnav li ul {
    display: none;
    position: absolute;
    background-color: #333;
}
.topnav li:hover > ul {
    display: block;
}
.topnav li.menuitems li a:hover {
    background-color: #ddd;
    color: black;
}
.topnav a.active {
    background-color: #808080;
    color: white;
}
.topnav .icon {
    display: none;
}

@media screen and (max-width: 600px) {
    .topnav ul {
        flex-direction: column;
        text-align: right;
    }
    .topnav li {
        display: none;
    }
    .topnav .icon {
        display: block;
        text-align: right;
    }
    .topnav.responsive li {
        display: block;
    }
    .topnav.responsive > li > ul > li {
        display: block;
    }
    .topnav.responsive li > a {
        text-align: right;
    }
    .topnav.responsive li ul {
        width: 100%;
        right: 0;
        text-align: right;
    }
}
<div class="topnav" id="myTopnav">
  <a href="javascript:void(0);" class="icon" onclick="myFunction()">MENU</a>
  <ul>
      <li><a href="#home" class="active">Home</a></li>
      <li><a href="#news">News</a></li>
      <li><a href="#contact">Contact</a></li>
      <li>
          <a href="#Dropdown">Dropdown</a>
          <ul class="menuitems">
            <li>
                <a href="#Dropdown1">Dropdown1</a>
            </li>
          </ul>
      </li>
  </ul>
</div>

Upvotes: 3

Related Questions