Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mobile Nav Javascript Not Working

I'm a bit of a beginner here, and doing exercises on FreeCodeCamp. While working on the Portfolio Site project, I tried adding a Javascript responsive mobile nav to the site from some code at ws3schools.

The nav is showing up fine, but clicking it does nothing. Here's my CodePen, any ideas what I did that's preventing it from popping up the nav links on click? https://codepen.io/colum1225/pen/xLwMJG

Here's the nav bar code:

<!-- HTML -->

<div class="topnav" id="myTopnav">
  <h3>Brandon Ray</h3>
  <a href="#contact">Contact</a>
  <a href="#about">About</a>
  <a href="#portfolio">Portfolio</a>
  <a href="javascript:void(0);" class="icon" onclick="myFunction()">&#9776;</a>
</div>

<!-- CSS -->

.topnav {
  background-color: #2C8597;
  overflow: hidden;
  width: 100%;
  position: fixed;
  z-index: 9;
}

.topnav h3 {
  float: left;
  color: #f2f2f2;
  font-family: sans-serif;
  font-size: 1.5em;
  padding-left: 30px;
  letter-spacing: .04em;
  font-weight: 500;
}

.topnav a {
  float: right;
  display: block;
  color: #f2f2f2;
  text-align: center;
  padding: 27px 30px 27px 30px;
  text-decoration: none;
  font-size: 1.25em;
  font-family: sans-serif;
}

.topnav a:hover {
  background-color: #ddd;
  color: black;
  transition: .5s;
}

.topnav a:active {
  background-color: #ddd;
  color: black;
}

.topnav .icon {
  display: none;
  font-size: 1.3em;
}


@media screen and (max-width: 640px) {
  .topnav a {display: none;}
  .topnav .icon {
    float: right;
    display: block;
    color: white;
    margin-right: 30px;
  }
}

@media screen and (max-width: 640px) {
  topnav.responsive {position: relative;}
  topnav.responsive a.icon {
    position: absolute;
    right: 0;
    top: 0;
  }

  topnav.responsive a {
    float: none;
    display: block;
    text-align: right;
  }
}

<!-- JS -->

function myFunction() {
  var x = document.getElementById("myTopnav");
    if (x.className === "topnav") {
        x.className += " responsive";
    } else {
        x.className = "topnav";
    }

}

like image 229
Brandon Avatar asked Feb 26 '26 08:02

Brandon


1 Answers

Great work so far.

You're missing a dot in-front of your topnav class selector for your .responsive rules. See the fixed CSS below,

@media screen and (max-width: 640px) {
  .topnav.responsive {position: relative;}
  .topnav.responsive a.icon {
    position: absolute;
    right: 0;
    top: 0;
  }

  .topnav.responsive a {
    float: none;
    display: block;
    text-align: right;
  }
}

Fixing Overlap

To fix the overlap of your first mobile navigation dropdown menu item with your menu icon, try adding a margin-top to the first menu item. A good margin size would be the menu's height of (82px).

This rule would look as follows,

  .topnav.responsive a:first-of-type {
    margin-top: 82px;
  }

Include this rule with your other .topnav.responsive rules (line ~77).

like image 198
Trent Avatar answered Feb 27 '26 22:02

Trent



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!