Search code examples
htmlcsshamburger-menu

Pure HTML and CSS hamburger menu does not work


I'm making a hamburger menu right now with html and css (no js), and the :checked + .something does not work. I'm searching for solutions for about 3 hours but i can't find any. If you would help me that would be nice. Maybe I messed up somewhere because i watched it from a video, but i did the exact same thing like him but i doesn't work :( Here is the code:

* {
  margin: 0;
  padding: 0;
}

body {
  background-color: teal;
}

.navigations {
  right: 0;
  z-index: 10;
  display: block;
  position: fixed;
  top: 15px;
  padding-bottom: 20px;
  padding-left: 25px;
  padding-right: 150px;
  border-radius: 50px 10px 10px 50px;
  background: rgba(0, 0, 0, 0.5);
}

.navigations div {
  display: inline-block;
  font-size: 25px;
}

.navigations a {
  text-decoration: none;
  color: white;
}

.burger {
  z-index: 100;
  position: fixed;
  right: 25px;
  display: block;
  top: 25px;
  cursor: pointer;
}

.burger div {
  width: 45px;
  height: 5px;
  background-color: white;
  margin: 12px;
  border-radius: 10px;
}

#toggle {
  display: none;
  position: fixed;
}

#toggle:checked+.navigations {
  display: none;
}
<html>

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale = 1.0">
  <link rel="stylesheet" type="text/css" href="something.css">
</head>

<body>
  <div class="navigations">
    <div class="nav">
      <a href="">About us</a>
    </div>
    <div class="nav">
      <a href="">Tours</a>
    </div>
    <div class="nav">
      <a href="">Contacts</a>
    </div>
  </div>
  <label for="toggle">
      <div class="burger">
          <div class="burgerelem"></div>
          <div class="burgerelem"></div>
          <div class="burgerelem"></div>
      </div>
  </label>
</body>

</html>


Solution

  • It seems like you're actually missing the checkbox element. Since you're using the adjacent sibling selector, (+ in #toggle:checked + .navigations) you should put the checkbox with the .navigations div immediately before the #toggle input.

    * {
      margin: 0;
      padding: 0;
    }
    
    body {
      background-color: teal;
    }
    
    .navigations {
      right: 0;
      z-index: 10;
      display: block;
      position: fixed;
      top: 15px;
      padding-bottom: 20px;
      padding-left: 25px;
      padding-right: 150px;
      border-radius: 50px 10px 10px 50px;
      background: rgba(0, 0, 0, 0.5);
    }
    
    .navigations div {
      display: inline-block;
      font-size: 25px;
    }
    
    .navigations a {
      text-decoration: none;
      color: white;
    }
    
    .burger {
      z-index: 100;
      position: fixed;
      right: 25px;
      display: block;
      top: 25px;
      cursor: pointer;
    }
    
    .burger div {
      width: 45px;
      height: 5px;
      background-color: white;
      margin: 12px;
      border-radius: 10px;
    }
    
    #toggle {
      display: none;
      position: fixed;
    }
    
    /*
        Since the .navigations is the next sibling element after #toggle,
        the + selector works here
    */
    
    #toggle:checked+.navigations {
      display: none;
    }
    <input type="checkbox" id="toggle" /> <!-- Add this! -->
    <div class="navigations">
      <div class="nav">
        <a href="">About us</a>
      </div>
      <div class="nav">
        <a href="">Tours</a>
      </div>
      <div class="nav">
        <a href="">Contacts</a>
      </div>
    </div>
    <label for="toggle">
      <div class="burger">
        <div class="burgerelem"></div>
        <div class="burgerelem"></div>
        <div class="burgerelem"></div>
      </div>
    </label>