Jose Alcaraz
Jose Alcaraz

Reputation: 51

How do I get the navbar to close when I click on a nav button?

When I click on a nav button, my navbar stays open. I'm really new to coding and would appreciate some guidance! I can get the hamburger button to open and close just fine. but when I click on a link to navigate through the website the navbar stays opened after I click on one.

*{
  margin:0;
  padding:0;
  box-sizing: inherit;
}
.nav {
    height: 50px;
    width: 100%;
    background-color: #4d4d4d;
    /* position: relative; */
    position: fixed;
    z-index: 10;
}

.nav > .nav-header {
  display: inline;
}

.nav > .nav-header > .nav-title {
  display: inline-block;
  font-size: 22px;
  color: #fff;
  padding: 10px 10px 10px 10px;
}

.nav > .nav-btn {
  display: none;

}

.nav > .nav-links {
  display: inline;
  /* float: right; */
  font-size: 18px;
}

.nav > .nav-links > a {
  display: inline-block;
  padding: 13px 10px 13px 10px;
  text-decoration: none;
  color: #efefef;
}

.nav > .nav-links > a:hover {
  background-color: rgba(0, 0, 0, 0.3);
}

.nav > #nav-check {
  display: none;
}

@media (max-width:700px) {
  .nav > .nav-btn {
    display: inline-block;
    position: absolute;
    right: 0px;
    top: 0px;
  }
  .nav > .nav-btn > label {
    display: inline-block;
    width: 50px;
    height: 50px;
    padding: 13px;
  }
  .nav > .nav-btn > label:hover,.nav  #nav-check:checked ~ .nav-btn > label {
    background-color: rgba(0, 0, 0, 0.3);
  }
  .nav > .nav-btn > label > span {
    display: block;
    width: 25px;
    height: 10px;
    border-top: 2px solid #eee;
  }
  .nav > .nav-links {
    position: absolute;
    display: block;
    width: 100%;
    background-color: #333;
    height: 0px;
    transition: all 0.3s ease-in;
    overflow-y: hidden;
    top: 50px;
    left: 0px;
    z-index: 10;
  }
  .nav > .nav-links > a {
    display: block;
    width: 100%;
  }
  .nav > #nav-check:not(:checked) ~ .nav-links {
    height: 0px;
  }
  .nav > #nav-check:checked ~ .nav-links {
    height: calc(100vh - 50px);
    overflow-y: auto;
  }
}
<div class="nav">

    <input type="checkbox" id="nav-check">
    
    <div class="nav-header">
      <div class="nav-title">
        Gallegos Landscaping
      </div>
    </div>
    
    <div class="nav-btn">
      <label for="nav-check">
        <span></span>
        <span></span>
        <span></span>
      </label>
    </div>

    <div class="nav-links">
      <a href="#">Home</a>
      <a href="#about1">About</a>
      <a href="#services1">Services</a>
      <a href="#">Contact</a>
    </div>
    
</div>

Upvotes: 3

Views: 1161

Answers (3)

Vintage Coders
Vintage Coders

Reputation: 162

Here is the code that will solve your issue.

I have added a small script to your code which will close the navbar on click of menu item.

<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
*{
  margin:0;
  padding:0;
  box-sizing: inherit;
}
.nav {
    height: 50px;
    width: 100%;
    background-color: #4d4d4d;
    /* position: relative; */
    position: fixed;
    z-index: 10;
}

.nav > .nav-header {
  display: inline;
}

.nav > .nav-header > .nav-title {
  display: inline-block;
  font-size: 22px;
  color: #fff;
  padding: 10px 10px 10px 10px;
}

.nav > .nav-btn {
  display: none;

}

.nav > .nav-links {
  display: inline;
  /* float: right; */
  font-size: 18px;
}

.nav > .nav-links > a {
  display: inline-block;
  padding: 13px 10px 13px 10px;
  text-decoration: none;
  color: #efefef;
}

.nav > .nav-links > a:hover {
  background-color: rgba(0, 0, 0, 0.3);
}

.nav > #nav-check {
  display: none;
}

@media (max-width:700px) {
  .nav > .nav-btn {
    display: inline-block;
    position: absolute;
    right: 0px;
    top: 0px;
  }
  .nav > .nav-btn > label {
    display: inline-block;
    width: 50px;
    height: 50px;
    padding: 13px;
  }
  .nav > .nav-btn > label:hover,.nav  #nav-check:checked ~ .nav-btn > label {
    background-color: rgba(0, 0, 0, 0.3);
  }
  .nav > .nav-btn > label > span {
    display: block;
    width: 25px;
    height: 10px;
    border-top: 2px solid #eee;
  }
  .nav > .nav-links {
    position: absolute;
    display: block;
    width: 100%;
    background-color: #333;
    height: 0px;
    transition: all 0.3s ease-in;
    overflow-y: hidden;
    top: 50px;
    left: 0px;
    z-index: 10;
  }
  .nav > .nav-links > a {
    display: block;
    width: 100%;
  }
  .nav > #nav-check:not(:checked) ~ .nav-links {
    height: 0px;
  }
  .nav > #nav-check:checked ~ .nav-links {
    height: calc(100vh - 50px);
    overflow-y: auto;
  }
  .menu-open .nav-links {
    height: calc(100vh - 50px) !important;
    overflow-y: auto;
}  
 .nav-links {
    height: 0 !important;
}
}
</style>
<div class="nav">

    <input type="checkbox" id="nav-check">

    <div class="nav-header">
      <div class="nav-title">
        Gallegos Landscaping
      </div>
    </div>

    <div class="nav-btn">
      <label for="nav-check">
        <span></span>
        <span></span>
        <span></span>
      </label>
    </div>

    <div class="nav-links">
      <a href="#">Home</a>
      <a href="#about1">About</a>
      <a href="#services1">Services</a>
      <a href="#">Contact</a>
    </div>

</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<script>
$(document).ready(function(){

   $(".nav-btn").click(function(){
    $("body").toggleClass("menu-open");
  });
  $(".nav-links a").click(function(){
    $("body").toggleClass("menu-open");
  });
});
</script>
</html>

Upvotes: 0

joohong89
joohong89

Reputation: 1271

I dont think it is possible to achieve what you need by CSS alone. Thus I have added a jQuery solution.

What I have done here is to un-check the checkbox after you have click on the links. Thus this trigger the closure of the dropdown menu.

$('.nav-links > a').on('click', ()=>{

  $('#nav-check').prop('checked', false);
  
});
*{
  margin:0;
  padding:0;
  box-sizing: inherit;
}
.nav {
    height: 50px;
    width: 100%;
    background-color: #4d4d4d;
    /* position: relative; */
    position: fixed;
    z-index: 10;
}

.nav > .nav-header {
  display: inline;
}

.nav > .nav-header > .nav-title {
  display: inline-block;
  font-size: 22px;
  color: #fff;
  padding: 10px 10px 10px 10px;
}

.nav > .nav-btn {
  display: none;

}

.nav > .nav-links {
  display: inline;
  /* float: right; */
  font-size: 18px;
}

.nav > .nav-links > a {
  display: inline-block;
  padding: 13px 10px 13px 10px;
  text-decoration: none;
  color: #efefef;
}

.nav > .nav-links > a:hover {
  background-color: rgba(0, 0, 0, 0.3);
}

.nav > #nav-check {
  display: none;
}

@media (max-width:1000px) {
  .nav > .nav-btn {
    display: inline-block;
    position: absolute;
    right: 0px;
    top: 0px;
  }
  .nav > .nav-btn > label {
    display: inline-block;
    width: 50px;
    height: 50px;
    padding: 13px;
  }
  .nav > .nav-btn > label:hover,.nav  #nav-check:checked ~ .nav-btn > label {
    background-color: rgba(0, 0, 0, 0.3);
  }
  .nav > .nav-btn > label > span {
    display: block;
    width: 25px;
    height: 10px;
    border-top: 2px solid #eee;
  }
  .nav > .nav-links {
    position: absolute;
    display: block;
    width: 100%;
    background-color: #333;
    height: 0px;
    transition: all 0.3s ease-in;
    overflow-y: hidden;
    top: 50px;
    left: 0px;
    z-index: 10;
  }
  .nav > .nav-links > a {
    display: block;
    width: 100%;
  }
  .nav > #nav-check:not(:checked) ~ .nav-links {
    height: 0px;
  }
  .nav > #nav-check:checked ~ .nav-links {
    height: calc(100vh - 50px);
    overflow-y: auto;
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="nav">

    <input type="checkbox" id="nav-check">
    
    <div class="nav-header">
      <div class="nav-title">
        Gallegos Landscaping
      </div>
    </div>
    
    <div class="nav-btn">
      <label for="nav-check">
        <span></span>
        <span></span>
        <span></span>
      </label>
    </div>

    <div class="nav-links">
      <a href="#">Home</a>
      <a href="#about1">About</a>
      <a href="#services1">Services</a>
      <a href="#">Contact</a>
    </div>
    
</div>

Upvotes: 0

Peadar
Peadar

Reputation: 459

This is as you are using a checkbox to control visibility which is actually smart and I haven't seen before but it's somewhat limited in this use case.

I think an easier more dynamic approach would be to add some simple JavaScript. You can add an onClick handler to the menu button that will show/hide the menu by adding CSS.

So for example:

// JS
const menu = document.querySelector('.menu');
const menuToggle = document.querySelector('.menuToggle');

menuToggle.addEventListener('click', (e) => {
  e.preventDefault();

  if (window.getComputedStyle(menu).display === 'block') {
    menu.style.display = 'none';
  } else  {
    menu.style.display = 'block';
  }
});

That way you can be in more control of opening / closing the menu. Right now it's staying open as the checkbox is staying checked but not updating

Upvotes: 3

Related Questions