Гамбургер для панели навигации в выпуске CSS - PullRequest
0 голосов
/ 19 февраля 2019

Я работаю на своем собственном сайте и в настоящее время у меня проблема с кнопкой переключения.Когда я нажимаю на меню гамбургера, панель навигации не отображается.Но тумблер работает.Я просто делаю то же самое, но я просто объединил два источника, поскольку концепция одна и та же.

Исходные коды:

https://www.youtube.com/watch?v=xMTs8tAapnQ

https://www.youtube.com/watch?v=H1eXpp4DAWQ&list=LLU3FO2sJDhxbIBRVc0fA34A&index=2&t=0s

* {
  margin: 0;
  padding: 0;
}

#toggle {
  display: none;
}

.hambuger {
  z-index: 1;
  /*keeps displaying the icon when it clicks*/
  position: fixed;
  width: 25px;
  /*The parents (hamburger) of siblings (span)*/
  top: 20px;
  left: 28px;
  cursor: pointer;
  /*to able to click it*/
}

.line {
  display: block;
  /*to show the three lines*/
  width: 100%;
  /*Maximize the width of the parents (hamburger: 25px width)*/
  height: 3px;
  margin-bottom: 3px;
  /*to separate and see the icons*/
  background-color: black;
  /*color of the icons*/
}

.menu {
  display: none;
  width: 70px;
  line-height: 1.7em;
  margin: 40px;
}

.menu a {
  display: block;
  text-decoration: none;
  color: black;
  border: 1px grey solid;
}

#toggle:checked~.menu {
  display: block;
  /*The toggle became the parents while menu is the sibling here*/
}
<header class="content">
  <label for="toggle" class="hambuger">
    <input type="checkbox" id="toggle">
    <span class="line"></span>
    <span class="line"></span>
    <span class="line"></span>
  </label>

  <nav class="menu">
    <a href="#" class="active">Home</a>
    <a href="#" class="active">About us</a>
    <a href="#" class="active">History</a>
    <a href="#" class="active">Contacts</a>
  </nav>
</header>

1 Ответ

0 голосов
/ 19 февраля 2019

Чтобы выбрать элемент .menu из входных данных, используя общий селектор брата ~, я переместил ввод в пределах одного и того же родителя (элемент заголовка), чтобы сделать их родственниками.

* {
  margin: 0;
  padding: 0;
}

#toggle {
  display: none;
}

.hambuger {
  z-index: 1;
  /*keeps displaying the icon when it clicks*/
  position: fixed;
  width: 25px;
  /*The parents (hamburger) of siblings (span)*/
  top: 20px;
  left: 28px;
  cursor: pointer;
  /*to able to click it*/
}

.line {
  display: block;
  /*to show the three lines*/
  width: 100%;
  /*Maximize the width of the parents (hamburger: 25px width)*/
  height: 3px;
  margin-bottom: 3px;
  /*to separate and see the icons*/
  background-color: black;
  /*color of the icons*/
}

.menu {
  display: none;
  width: 70px;
  line-height: 1.7em;
  margin: 40px;
}

.menu a {
  display: block;
  text-decoration: none;
  color: black;
  border: 1px grey solid;
}

#toggle:checked~.menu {
  display: block;
  /*The toggle became the parents while menu is the sibling here*/
}
<header class="content">
  <input type="checkbox" id="toggle">
  <label for="toggle" class="hambuger">
    <span class="line"></span>
    <span class="line"></span>
    <span class="line"></span>
  </label>
  <nav class="menu">
    <a href="#" class="active">Home</a>
    <a href="#" class="active">About us</a>
    <a href="#" class="active">History</a>
    <a href="#" class="active">Contacts</a>
  </nav>

</header>
...