Search code examples
htmlcss

Why isn't my toggle button showing up for my responsive HTML menu?


I have this HTML menu and tried to make it responsive. I don't know why, but my toggle button just doesn't appear. I don't understand my mistake because I actually worked according to a tutorial and it worked there too.

/* 1. Style-Sheet */

body {
  font-family: 'Dosis';
  font-size: 1.2em;
  margin: 0;
  padding: 0;
  background-color: #f4f4f4;
}

h1 {
  font-size: 1.4em;
}

h2 {
  font-size: 1.2em;
}

h3 {
  font-size: 1.0em;
}


/*Standard Section*/

section.main {
  padding: 2em;
  margin-top: 4em;
  margin-bottom: 6em;
  min-height: calc(20em);
  /*Dieser Wert sollte mindestens so groß wie das vertikale Menü sein*/
}

.einrueckung {
  margin-left: 2em;
}

.button-container {
  display: flex;
  justify-content: center;
  margin-top: 2em;
}

.button-container button {
  background-color: #4CAF50;
  color: white;
  padding: 0.5em 0.5em;
  border: none;
  border-radius: 0.2em;
  cursor: pointer;
  transition: background-color 0.3s ease;
  font-family: 'Dosis', sans-serif;
  font-size: 1em;
  text-transform: uppercase;
}

.button-container button:hover {
  background-color: #45a049;
}

.container {
  display: flex;
  margin-top: 1em;
}

.left-section,
.right-section {
  flex: 1;
  padding: 2em;
}

.left-section {
  text-align: left;
}

.right-section {
  text-align: right;
}

.password-container {
  display: flex;
  justify-content: center;
  align-items: flex-start;
  height: 200px;
  padding-top: 20px;
}

.password-container h2 {
  font-family: 'Dosis', sans-serif;
  font-size: 2em;
  margin-bottom: 20px;
}

.password-container form {
  background-color: #f9f9f9;
  padding: 2em;
  border-radius: 1em;
  box-shadow: 0 0.2em 0.2em rgba(0, 0, 0, 0.1);
}

.password-container label {
  font-family: 'Dosis', sans-serif;
  font-size: 1.2em;
}

.password-container input[type="password"] {
  width: 100%;
  padding: 0.8em;
  margin: 0.8em 0;
  border-radius: 1em;
  border: 0.1em solid #ccc;
}

.password-container button {
  background-color: #4CAF50;
  color: white;
  padding: 1em 2em;
  border: none;
  border-radius: 1em;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

.password-container button:hover {
  background-color: #45a049;
}

.task_main {
  background-color: #f4f4f4;
  padding: 2em;
  border-radius: 1em;
}

.show-content-button {
  position: relative;
  top: 1em;
  right: 0;
  background-color: #51809b;
  color: white;
  border: none;
  padding: 0.6em 2em;
  margin-bottom: 0.8em;
  border-radius: 1em;
  font-family: 'Dosis';
  font-size: 1em;
  cursor: pointer;
}

.task_main {
  background-color: #f4f4f4;
  padding: 2em;
  border-radius: 1em;
  margin-top: 2em;
}

.img-links {
  float: left;
  margin-right: 15px;
  /* Abstand zwischen Bild und Text */
  margin-bottom: 15px;
  /* Optional: Abstand unterhalb des Bildes */
}

.img-zentriert {
  float: none;
  display: block;
  margin-left: auto;
  margin-right: auto;
}

.img-rechts {
  float: right;
  margin-right: 10px;
  /* Abstand zwischen Bild und Text */
  margin-bottom: 10px;
  /* Optional: Abstand unterhalb des Bildes */
}

.banner {
  position: relative;
  height: 500px;
  /* Höhe des Banners */
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  /* Verhindert, dass der Inhalt außerhalb der Box angezeigt wird */
}

.banner img {
  width: 100%;
  height: auto;
  object-fit: cover;
  /* Stellt sicher, dass das Bild vollständig angezeigt wird */
}


/* 2. CSS */


/* Basisstil für das Menü */

.menu {
  position: fixed;
  top: 1em;
  left: 50%;
  transform: translateX(-50%);
  width: 80%;
  max-width: 75rem;
  z-index: 1000;
  text-align: center;
  padding: 0.5em 0;
  background-color: rgb(255, 255, 255);
  border-radius: 5em;
}

.menu nav {
  display: flex;
  justify-content: center;
  align-items: center;
}

.menu nav a {
  font-family: 'Dosis';
  font-size: 1em;
  display: inline-block;
  padding: 0.7em 0.6em;
  text-decoration: none;
  color: #000000;
  transition: background-color 0.3s ease;
  white-space: nowrap;
}

.menu nav a:hover {
  background-color: #c0c2c5;
}

.menu nav .logo-link {
  margin-right: 1.25rem;
}

.menu img {
  margin-right: 0.8em;
  width: 5em;
  height: auto;
}

.menu .img:hover {
  background-color: transparent;
}

.menu nav .dropdown {
  position: relative;
  font-size: 1em;
}

.menu nav .dropdown-content {
  font-family: 'Dosis';
  font-size: 1em;
  display: none;
  position: absolute;
  background-color: rgb(255, 255, 255);
  width: auto;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
  z-index: 1;
  text-align: left;
  padding: 10px;
  border-radius: 5px;
  border: 1px solid #ccc;
}

.menu nav .dropdown:hover>.dropdown-content {
  display: block;
}

.menu nav .dropdown-content .dropdown-content {
  display: none;
  position: absolute;
  left: 100%;
  top: 0;
  background-color: rgb(255, 255, 255);
  width: auto;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
  z-index: 1;
  text-align: left;
  padding: 10px;
  border-radius: 5px;
  border: 1px solid #ccc;
}

.menu nav .dropdown-content:hover>.dropdown-content {
  display: block;
}

.menu nav .dropdown-content .dropdown-content a {
  color: #000000;
  padding: 10px 15px;
  text-decoration: none;
  display: block;
  transition: background-color 0.3s ease;
}

.menu nav .dropdown-content .dropdown-content a:hover {
  background-color: #c0c2c5;
}

.toggle_button {
  width: 30px;
  height: 23px;
  position: absolute;
  top: 25px;
  right: 25px;
  display: none;
  flex-direction: column;
  justify-content: space-between;
  cursor: pointer;
  background-color: #ffffff;
}

.bar {
  height: 4px;
  width: 100%;
  background-color: #000000;
  border-radius: 100px;
}

nav input[type="checkbox"] {
  display: none;
}

@media(max-width: 850px) {
  .toggle_button {
    display: flex;
  }
  .menu {
    width: 100%;
    background-color: #c0c2c5;
    display: block;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    padding: 0;
    transform: none;
  }
  .menu nav {
    flex-direction: column;
    display: none;
    width: 100%;
    padding: 0;
  }
  #toggle_button:checked+.toggle_button+.main-nav {
    display: flex;
  }
  .menu nav a {
    display: block;
    padding: 1em;
    text-align: center;
    border-top: 1px solid #ccc;
    .menu nav .dropdown {
      width: 100%;
    }
    .menu nav .dropdown-content {
      position: relative;
      width: 100%;
      box-shadow: none;
      border: none;
      background-color: #c0c2c5;
    }
    .menu nav .dropdown-content .dropdown-content {
      position: relative;
      left: 0;
      top: 0;
    }
  }
  .footer {
    background-color: #f9f9f9;
    color: #000000;
    padding: 20px;
    text-align: center;
    border-top-left-radius: 20px;
    border-top-right-radius: 20px;
    box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.1);
    font-family: 'Dosis';
    position: relative;
    bottom: 0;
    width: 100%;
  }
  .footer a {
    color: inherit;
    text-decoration: none;
  }
}
<!DOCTYPE html>
<html lang="de">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link href='https://fonts.googleapis.com/css?family=Dosis' rel='stylesheet'>
  <link rel="stylesheet" href="stylesheets/menu.css">
  <link rel="stylesheet" href="stylesheets/style.css">
  <title>Start Anleitungen</title>
</head>

<body>

  <section class="menu">
    <nav class="main-nav">
      <a href="index.html" class="logo-link"><img src="images/e4s-logo-2018_transparent.png" alt="Logo"></a>

      <input type="checkbox" id="toggle_button">
      <label for="toggle_button" class="toggle_button">
            <span class="bar"></span>
            <span class="bar"></span>
            <span class="bar"></span>
        </label>


      <div class="dropdown">
        <a href="#">Grundlagen</a>
        <div class="dropdown-content">
          <a href="grundlagen1.html">Einstieg</a>
        </div>
      </div>
      <div class="dropdown">
        <a href="#">Einkauf</a>
        <div class="dropdown-content">
          <a href="ek_einfach.html">einfacher EK-Prozess</a>
          <a href="ek_mit_dispo_vk.html">EK-Prozess mit Disposition</a>
        </div>
      </div>
      <div class="dropdown">
        <a href="#">Verkauf</a>
        <div class="dropdown-content">
          <a href="vk_einfach.html">einfacher Vk-Prozess</a>
        </div>
      </div>

      <div class="dropdown">
        <a href="#">Rechnungswesen</a>
        <div class="dropdown-content">
          <a href="rw_start.html">Einführung</a>
        </div>
      </div>

      <div class="dropdown">
        <a href="#">Personal</a>
        <div class="dropdown-content">
          <a href="hr_start.html">Übersicht HR</a>
          <div class="dropdown">
            <a href="#">Grundlagen &gt;&gt;</a>
            <div class="dropdown-content">
              <a href="hr_orgeinheiten.html">Organisationsstrukturen</a>
              <a href="hr_struktur.html">Unternehmensstrukturen</a>
              <a href="hr_planstelle.html">Planstellen</a>
            </div>
          </div>
          <div class="dropdown">
            <a href="#">Organisation &gt;&gt;</a>
            <div class="dropdown-content">
              <a href="hr_aufb_orgstruktur.html">Unternehmensstruktur</a>
            </div>
          </div>
          <div class="dropdown">
            <a href="#">Administration &gt;&gt;</a>
            <div class="dropdown-content">
              <a href="hr_einstellung.html">Personal einstellen</a>
            </div>
          </div>
        </div>
      </div>
      <div class="dropdown">
        <a href="start_anl.html" alt="Zum Menü Anleitungen" target="_blank">Anleitungen</a>
      </div>
      <a href="tutor_passwort.html" target="_blank">
        <img src="images/vorhangeschloss.png" style="width: 20px; height: auto; margin-right: 5px;"> Hinweise für Tutoren
      </a>
    </nav>

</body>

</html>

I thought the problem was the checkbox, which I set to "none" but this wasn't was the problem.


Solution

  • Your menu class has set display: none. Since you want to show the toggle button, I would suggest just moving the .toggle-button outside of menu.

    <body>
      <label for="toggle_button" class="toggle_button">
        <span class="bar"></span>
        <span class="bar"></span>
        <span class="bar"></span>
      </label>
    
      <section class="menu">
        REST OF YOUR CODE
      </section>
    </body>