汉堡菜单高度100%

时间:2019-04-11 13:54:40

标签: html css media-queries

我正在为测试应用程序制作响应式汉堡菜单。我希望汉堡菜单在打开时占据整个屏幕(高度的100%)。

我尝试了媒体查询:.header .menu的高度达到100%,但不起作用:

body {
  margin: 0;
  font-family: Helvetica, sans-serif;
  background-color: #f4f4f4;
}

a {
  color: #8C2635;
}

a:hover {
  color: #8B8D33;
}


/* header */

.header {
  background-color: #fff;
  position: fixed;
  height: 80px;
  width: 100%;
  z-index: 3;
}

.header ul {
  margin: 0;
  padding: 0;
  list-style: none;
  overflow: hidden;
  background-color: #fff;
}

.header li a {
  display: block;
  padding: 20px 20px;
  text-decoration: none;
}

.header .logo {
  display: block;
  float: left;
  font-size: 2em;
  padding: 10px 20px;
  text-decoration: none;
}


/* menu */

.header .menu {
  clear: both;
  max-height: 0;
  text-align: center;
  transition: max-height .2s ease-out;
  position: relative;
  top: -7px;
}


/* menu icon */

.header .menu-icon {
  cursor: pointer;
  display: inline-block;
  float: right;
  padding: 40px 20px;
  position: relative;
  user-select: none;
}

.header .menu-icon .navicon {
  background: #333;
  display: block;
  height: 2px;
  position: relative;
  transition: background .2s ease-out;
  width: 18px;
}

.header .menu-icon .navicon:before,
.header .menu-icon .navicon:after {
  background: #333;
  content: '';
  display: block;
  height: 100%;
  position: absolute;
  transition: all .2s ease-out;
  width: 100%;
}

.header .menu-icon .navicon:before {
  top: 5px;
}

.header .menu-icon .navicon:after {
  top: -5px;
}


/* menu btn */

.header .menu-btn {
  display: none;
}

.header .menu-btn:checked~.menu {
  max-height: 300px;
}

.header .menu-btn:checked~.menu-icon .navicon {
  background: transparent;
}

.header .menu-btn:checked~.menu-icon .navicon:before {
  transform: rotate(-45deg);
}

.header .menu-btn:checked~.menu-icon .navicon:after {
  transform: rotate(45deg);
}

.header .menu-btn:checked~.menu-icon:not(.steps) .navicon:before,
.header .menu-btn:checked~.menu-icon:not(.steps) .navicon:after {
  top: 0;
}

@media (min-width: 64em) {
  .header li {
    float: left;
  }
  .header li a {
    padding: 30px 30px;
  }
  .header .menu {
    clear: none;
    float: right;
    max-height: none;
  }
  .header .menu-icon {
    display: none;
  }
}
<header class="header">

  <input class="menu-btn" type="checkbox" id="menu-btn" />
  <label class="menu-icon" for="menu-btn"><span class="navicon"></span></label>

  <ul class="menu">
    <li><a href="#work">HOME</a></li>
    <li><a href="#about">SOBRE NÓS</a></li>
    <li><a href="#careers">OBJETIVOS</a></li>
    <li><a href="#contact">PARCEIROS</a></li>
    <li><a href="#contact">CONTACTOS</a></li>
  </ul>

</header>

3 个答案:

答案 0 :(得分:2)

对于类.header .menu-btn:checked~.menu,我将height设置为100vh,并将max-height设置为300px

body {
  margin: 0;
  font-family: Helvetica, sans-serif;
  background-color: #f4f4f4;
}

a {
  color: #8C2635;
}

a:hover {
  color: #8B8D33;
}


/* header */

.header {
  background-color: #fff;
  position: fixed;
  height: 80px;
  width: 100%;
  z-index: 3;
}

.header ul {
  margin: 0;
  padding: 0;
  list-style: none;
  overflow: hidden;
  background-color: #fff;
}

.header li a {
  display: block;
  padding: 20px 20px;
  text-decoration: none;
}

.header .logo {
  display: block;
  float: left;
  font-size: 2em;
  padding: 10px 20px;
  text-decoration: none;
}


/* menu */

.header .menu {
  clear: both;
  max-height: 0;
  text-align: center;
  transition: max-height .2s ease-out;
  position: relative;
  top: -7px;
}


/* menu icon */

.header .menu-icon {
  cursor: pointer;
  display: inline-block;
  float: right;
  padding: 40px 20px;
  position: relative;
  user-select: none;
}

.header .menu-icon .navicon {
  background: #333;
  display: block;
  height: 2px;
  position: relative;
  transition: background .2s ease-out;
  width: 18px;
}

.header .menu-icon .navicon:before,
.header .menu-icon .navicon:after {
  background: #333;
  content: '';
  display: block;
  height: 100%;
  position: absolute;
  transition: all .2s ease-out;
  width: 100%;
}

.header .menu-icon .navicon:before {
  top: 5px;
}

.header .menu-icon .navicon:after {
  top: -5px;
}


/* menu btn */

.header .menu-btn {
  display: none;
}

.header .menu-btn:checked~.menu {
  max-height: 100vh;
  height: 100vh;
}

.header .menu-btn:checked~.menu-icon .navicon {
  background: transparent;
}

.header .menu-btn:checked~.menu-icon .navicon:before {
  transform: rotate(-45deg);
}

.header .menu-btn:checked~.menu-icon .navicon:after {
  transform: rotate(45deg);
}

.header .menu-btn:checked~.menu-icon:not(.steps) .navicon:before,
.header .menu-btn:checked~.menu-icon:not(.steps) .navicon:after {
  top: 0;
}

@media (min-width: 64em) {
  .header li {
    float: left;
  }
  .header li a {
    padding: 30px 30px;
  }
  .header .menu {
    clear: none;
    float: right;
    max-height: none;
  }
  .header .menu-icon {
    display: none;
  }
}
<header class="header">

  <input class="menu-btn" type="checkbox" id="menu-btn" />
  <label class="menu-icon" for="menu-btn"><span class="navicon"></span></label>

  <ul class="menu">
    <li><a href="#work">HOME</a></li>
    <li><a href="#about">SOBRE NÓS</a></li>
    <li><a href="#careers">OBJETIVOS</a></li>
    <li><a href="#contact">PARCEIROS</a></li>
    <li><a href="#contact">CONTACTOS</a></li>
  </ul>

</header>

答案 1 :(得分:1)

高度:100vh =视口高度的100%

高度:100%=父元素高度的100%

您必须知道的一点:如果将%用于垂直边距或填充,则%将根据父元素的宽度而不是高度进行计算。

在这里看到CSS单元的漂亮页面:http://css-tricks.com/the-lengths-of-css/

复制自:Styling HTML and BODY selector to height: 100%; vs using 100vh

答案 2 :(得分:0)

在这种情况下,您可以使用calc

.header .menu-btn:checked~.menu {
    max-height: calc(100vh - 80px); 
}