Trying to create toggle for icon in NavBar using a checkbox

Viewed 328

I am trying to use a checkbox to make my font awesome icon trigger the menu icon to open and close the menu (which is how I want it to be design wise) when the screen is in mobile view.

Thanks in advance for any help.

/*** General ***/

* {
  font-family: 'Noto Serif SC', serif;
  margin: 0;
  padding: 0;
}

.bluepic {
  padding-top: 10px;
  display: block;
  margin-left: auto;
  margin-right: auto;
  width: 100%;
  max-width: 400px;
  height: auto;
}

.contents1 {
  margin-top: 5vh;
  margin-bottom: 10vh;
}

a {
  text-decoration: none;
  color: black;
  margin: none;
  padding: none;
}


/*** Header ***/

.header {
  margin-top: 20px;
  margin-bottom: 20px;
}

.logo {
  text-align: center;
  font-size: 30px;
}


/*** Navigation ***/

.fa-bars {
  display: none;
}

.nav-links {
  width: 100%;
  float: left;
  position: sticky;
  align-items: center;
  justify-content: center;
  padding: 0;
  background-color: #f7f7f7;
  border-bottom: 1px solid rgb(228, 224, 224);
  display: flex;
}

.nav-links li {
  list-style: none;
  text-align: center;
  font-size: 18px;
  float: left;
  padding: 14px 16px;
}

.nav-links li a {
  text-decoration: none;
  text-align: center;
  float: left;
}


/*** Footer ***/

.footer {
  display: block;
  margin-left: auto;
  margin-right: auto;
  width: 50%;
  text-align: center;
}

.footer2 {
  display: block;
  margin-left: auto;
  margin-right: auto;
  width: 50%;
  text-align: center;
}


/*** Media Queries ***/

@media only screen and (max-width: 800px) {
  .bluepic {
    padding-top: 10px;
    display: block;
    margin-left: auto;
    margin-right: auto;
    width: 100%;
    max-width: 375px;
    height: auto;
  }
}

@media only screen and (max-width: 700px) {
  .bluepic {
    padding-top: 10px;
    display: block;
    margin-left: auto;
    margin-right: auto;
    width: 100%;
    max-width: 350px;
    height: auto;
  }
}

@media only screen and (max-width: 600px) {
  .fa-bars {
    font-size: 20px;
    display: block;
    float: right;
    margin-top: 10px;
    margin-right: 40px;
    cursor: pointer;
  }
  .logo {
    text-align: left;
    font-size: 25px;
  }
  .nav-toggle {
    display: none;
  }
  .nav-links,
  ul {
    position: fixed;
    width: 100%;
    height: 100vh;
    text-align: center;
    background-color: #f7f7f7;
    border-bottom: 1px solid rgb(107, 76, 76);
    top: 80px;
    left: 0;
    flex-direction: column;
    display: none;
  }
  .nav-toggle:checked+.nav-links,
  ul {
    display: flex;
  }
  .nav-links li {
    display: block;
  }
  .contents1 {
    margin-top: 5vh;
  }
}
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@200&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">

<div class="header">
  <div>
    <div class="logo">
      <h4>The Blue Lion <i class="fas fa-bars" id="nav-toggle"></i></h4>
        <input type="checkbox" id="nav-toggle" class="nav-toggle">
    </div>
  </div>

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

<div class="navbar">
  <div class="nav-links" id="myTopnav">
    <ul>
      <li a href="#">Food</li>
      <li a href="#">Drink</li>
      <li a href="#">Covid-19</li>
      <li a href="#">News</li>
      <li a href="#">Join the Team</li>
    </ul>
  </div>
</div>

<div class="contents1">
  <img src="https://mcdn.wallpapersafari.com/medium/84/3/pQuUz4.jpg" class="bluepic" alt="Blue">
</div>

<div class="footer">
  <div class="info">
    Leave us a review:
    <i class="fab fa-tripadvisor"></i>
  </div>

  <div class="socialiconsf">
    <i class="fab fa-facebook-f"></i>
    <i class="fab fa-twitter"></i>
    <i class="fab fa-instagram"></i>
  </div>
</div>

<div class="footer2">
  <p>
    © The Blue Lion 2021
  </p>
</div>

3 Answers

Do this:

* {
  font-family: 'Noto Serif SC', serif;
  margin: 0;
  padding: 0;
}

.bluepic {
  padding-top: 10px;
  display: block;
  margin-left: auto;
  margin-right: auto;
  width: 100%;
  max-width: 400px;
  height: auto;
}

.contents1 {
  margin-top: 5vh;
  margin-bottom: 10vh;
}

a {
  text-decoration: none;
  color: black;
  margin: none;
  padding: none;
}


/*** Header ***/

.header {
  margin-top: 20px;
  margin-bottom: 20px;
}

.logo {
  text-align: center;
  font-size: 30px;
}


/*** Navigation ***/

.fa-bars {
  display: none;
}

.nav-links {
  width: 100%;
  float: left;
  position: sticky;
  align-items: center;
  justify-content: center;
  padding: 0;
  background-color: #f7f7f7;
  border-bottom: 1px solid rgb(228, 224, 224);
  display: flex;
}

.nav-links li {
  list-style: none;
  text-align: center;
  font-size: 18px;
  float: left;
  padding: 14px 16px;
}

.nav-links li a {
  text-decoration: none;
  text-align: center;
  float: left;
}


/*** Footer ***/

.footer {
  display: block;
  margin-left: auto;
  margin-right: auto;
  width: 50%;
  text-align: center;
}

.footer2 {
  display: block;
  margin-left: auto;
  margin-right: auto;
  width: 50%;
  text-align: center;
}


/*** Media Queries ***/

@media only screen and (max-width: 800px) {
  .bluepic {
    padding-top: 10px;
    display: block;
    margin-left: auto;
    margin-right: auto;
    width: 100%;
    max-width: 375px;
    height: auto;
  }
}

@media only screen and (max-width: 700px) {
  .bluepic {
    padding-top: 10px;
    display: block;
    margin-left: auto;
    margin-right: auto;
    width: 100%;
    max-width: 350px;
    height: auto;
  }
}

@media only screen and (max-width: 600px) {
  .fa-bars {
    font-size: 20px;
    display: block;
    float: right;
    margin-top: 10px;
    margin-right: 40px;
    cursor: pointer;
  }
  .logo {
    text-align: left;
    font-size: 25px;
  }
  .nav-toggle {
    display: none;
    
  }
  .nav-links,
  ul {
    width: 100%;
    height: 100vh;
    text-align: center;
    background-color: #f7f7f7;
    border-bottom: 1px solid rgb(107, 76, 76);
    top: 80px;
    left: 0;
    flex-direction: column;
    display: none;
  }
  .nav-toggle:checked+.nav-links,
  ul {
    display: flex;
  }
  .nav-links li {
    display: block;
  }
  .contents1 {
    margin-top: 5vh;
  }
}
@media (max-width:600px){
    .hamburger-menu{
    
    position: relative;
}
    .checkbox:checked~ .nav-links{
        display: block;
    }
}
<div class="header">
  <div>
    <div class="logo">
      <h4>The Blue Lion 
        
      
            </div>
        
      </h4>
    </div>
    
    <label for="nav-toggle" class="nav-toggle-label">
      <span></span>
    </label>
  </div>
</div>

    <div class="navbar">
        <input type="checkbox" id="check" class="checkbox" hidden>
        <label for="check" class="fas fa-bars"></label>
        <div class="nav-links" id="myTopnav">
          <ul>
            <li a href="#">Food</li>
            <li a href="#">Drink</li>
            <li a href="#">Covid-19</li>
            <li a href="#">News</li>
            <li a href="#">Join the Team</li>
          </ul>
        </div>
      </div>


<div class="contents1">
  <img src="bluepic1.jpg" class="bluepic" alt="Blue">
</div>

<div class="footer">
  <div class="info">
    Leave us a review:
    <i class="fab fa-tripadvisor"></i>
  </div>
  
  <div class="socialiconsf">
    <i class="fab fa-facebook-f"></i>
    <i class="fab fa-twitter"></i>
    <i class="fab fa-instagram"></i>
  </div>
</div>

<div class="footer2">
  <p>
    © The Blue Lion 2021
  </p>
</div>

CSS modification:

The font-awesome library overrides your CSS. That is why your menu-icon (.fa-bars) wouldn't hide when not in mobile mode.

I made following corrections to your CSS:

  1. Changing .fa-bars to .fas.fa-bars
  2. Adding !important in mobile mode
    (to show always when media break-point (... and (max-width: 600px))
/*** Navigation ***/
.fas.fa-bars {
  display: none;
}

@media only screen and (max-width: 600px) {
  .fas.fa-bars {
    font-size: 20px;
    display: block !important;
    float: right;
    margin-top: 10px;
    margin-right: 40px;
    cursor: pointer;
  }
}

Click event handler on checkBox:

Also, if you want functionality to your checkbox, then you would have to add an event-handler in JS and add the onClick=function() in your HTML (and correct the issue of the multiple assigned id as mentioned in comments: I replaced id="nav-toggle" with id="nav-toggle-bars" for the <i class="fas fa-bars" element).

Following complete code for you to Run code snippet and comment if this is your desired outcome (please advise).

Expand to "full-page" and change your browser width to see effect
below media break-point (... and (max-width: 600px))

const menuToggle = document.getElementById("nav-toggle-bars");
let menuCheckBox;

function showHideNavToggle(event) {
  menuCheckBox = event.target;
  if (menuCheckBox.checked) {
    menuToggle.style.display = "block";
  } else {
    menuToggle.style.display = "none";
  }
}
/*** General ***/

* {
  font-family: 'Noto Serif SC', serif;
  margin: 0;
  padding: 0;
}

.bluepic {
  padding-top: 10px;
  display: block;
  margin-left: auto;
  margin-right: auto;
  width: 100%;
  max-width: 400px;
  height: auto;
}

.contents1 {
  margin-top: 5vh;
  margin-bottom: 10vh;
}

a {
  text-decoration: none;
  color: black;
  margin: none;
  padding: none;
}


/*** Header ***/

.header {
  margin-top: 20px;
  margin-bottom: 20px;
}

.logo {
  text-align: center;
  font-size: 30px;
}


/*** Navigation ***/

.fas.fa-bars {
  display: none;
}

.nav-links {
  width: 100%;
  float: left;
  position: sticky;
  align-items: center;
  justify-content: center;
  padding: 0;
  background-color: #f7f7f7;
  border-bottom: 1px solid rgb(228, 224, 224);
  display: flex;
}

.nav-links li {
  list-style: none;
  text-align: center;
  font-size: 18px;
  float: left;
  padding: 14px 16px;
}

.nav-links li a {
  text-decoration: none;
  text-align: center;
  float: left;
}


/*** Footer ***/

.footer {
  display: block;
  margin-left: auto;
  margin-right: auto;
  width: 50%;
  text-align: center;
}

.footer2 {
  display: block;
  margin-left: auto;
  margin-right: auto;
  width: 50%;
  text-align: center;
}


/*** Media Queries ***/

@media only screen and (max-width: 800px) {
  .bluepic {
    padding-top: 10px;
    display: block;
    margin-left: auto;
    margin-right: auto;
    width: 100%;
    max-width: 375px;
    height: auto;
  }
}

@media only screen and (max-width: 700px) {
  .bluepic {
    padding-top: 10px;
    display: block;
    margin-left: auto;
    margin-right: auto;
    width: 100%;
    max-width: 350px;
    height: auto;
  }
}

@media only screen and (max-width: 600px) {
  .fas.fa-bars {
    font-size: 20px;
    display: block !important;
    float: right;
    margin-top: 10px;
    margin-right: 40px;
    cursor: pointer;
  }
  .logo {
    text-align: left;
    font-size: 25px;
  }
  .nav-toggle {
    display: none;
  }
  .nav-links,
  ul {
    position: fixed;
    width: 100%;
    height: 100vh;
    text-align: center;
    background-color: #f7f7f7;
    border-bottom: 1px solid rgb(107, 76, 76);
    top: 80px;
    left: 0;
    flex-direction: column;
    display: none;
  }
  .nav-toggle:checked+.nav-links,
  ul {
    display: flex;
  }
  .nav-links li {
    display: block;
  }
  .contents1 {
    margin-top: 5vh;
  }
}
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@200&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">

<div class="header">
  <div>
    <div class="logo">
      <h4>The Blue Lion <i class="fas fa-bars" id="nav-toggle-bars"></i></h4>
        <input type="checkbox" onclick="showHideNavToggle(event)" id="nav-toggle" class="nav-toggle">
    </div>
  </div>

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

<div class="navbar">
  <div class="nav-links" id="myTopnav">
    <ul>
      <li a href="#">Food</li>
      <li a href="#">Drink</li>
      <li a href="#">Covid-19</li>
      <li a href="#">News</li>
      <li a href="#">Join the Team</li>
    </ul>
  </div>
</div>

<div class="contents1">
  <img src="https://mcdn.wallpapersafari.com/medium/84/3/pQuUz4.jpg" class="bluepic" alt="Blue">
</div>

<div class="footer">
  <div class="info">
    Leave us a review:
    <i class="fab fa-tripadvisor"></i>
  </div>

  <div class="socialiconsf">
    <i class="fab fa-facebook-f"></i>
    <i class="fab fa-twitter"></i>
    <i class="fab fa-instagram"></i>
  </div>
</div>

<div class="footer2">
  <p>
    © The Blue Lion 2021
  </p>
</div>

Note:
I also corrected 2 html closing tags (one </div> eliminated and </h4> closing tag corrected and replaced img source with a link to a generic blue background)

See this response in codepen. https://codepen.io/threefingers/pen/PojZMpx

Add in your ::before element icon, the code icons in your states.

.mydiv{
  background-color: blue;
  height: 50px;
  width: 50px;
}

input + .mydiv::before{
  content: 'code icon 1';
}

input:checked + .mydiv::before{
  content: 'code icon 2';
}
<input type="checkbox" value="Car"> I have a car 
<div class="mydiv"></div>

Related