1

I'm making a responsive hamburger menu for a test app. I want the hamburger menu to occupy the entire screen (100% of the height) when it's opened.

I tried media queries: .header .menu height to 100% but it doesn't work:

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>
Alex
  • 8,875
  • 2
  • 27
  • 44

3 Answers3

3

For class .header .menu-btn:checked~.menu I set the height to 100vh, and also the max-height since it was only 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>
StefanBob
  • 4,857
  • 2
  • 32
  • 38
2

height: 100vh = 100% of the viewport height

height: 100% = 100% of the parent's element height

Something you have to know : if you use % for vertical margin or padding, % will be calculated on the width of the parent element, not the height.

See a nice page here for CSS units: http://css-tricks.com/the-lengths-of-css/

Copied from Styling HTML and BODY selector to height: 100%; vs using 100vh

leonheess
  • 16,068
  • 14
  • 77
  • 112
Julian H
  • 91
  • 1
  • 8
0

You can use calc in this case:

.header .menu-btn:checked~.menu {
    max-height: calc(100vh - 80px); 
}
leonheess
  • 16,068
  • 14
  • 77
  • 112
Serhiy
  • 1