CSS off canvas menu

by thanner

HTML

<body>

  <header class="header">
    <input class="header__checkbox" id="mainnav-toggle" type="checkbox" value="1" />

    <a class="header__logo" href="#">LOGO</a>

    <label class="header__toggle" for="mainnav-toggle"></label>

    <ul class="header__menu">
      <li>
        <a href="#">About</a>
      </li>
      <li>
        <a href="#">Work</a>
      </li>
      <li>
        <a href="#">Contact</a>
      </li>
      <li>
        <a href="#">More</a>
      </li>
    </ul>

  </header>

  <main>
    <p>
      Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
    </p>
  </main>

</body>

SCSS

// style header
.header {
  padding: 1em 0;
  border-bottom: 1px solid #222;
  margin-bottom: 2em;
  
  &:after {
    content: '';
    clear: both;
    display: block;
  }
}

// style logo
.header__logo {
  text-decoration: none;
  color: inherit;
}

// make checkbox invisible
.header__checkbox {
  position: absolute;
  width: 1px;
  height: 1px;
  clip-path: inset(50%);
  opacity: 0;
}

// style menu toggle
.header__toggle {
  display: block;
  position: relative;
  width: 2em;
  height: 1em;
  float: right;
  border-style: solid;
  border-color: #000;
  border-width: 2px 0;
  cursor: pointer;
  &:before,
  &:after {
    content: '';
    position: absolute;
    left: 0;
    top: 50%;
    height: 2px;
    width: 100%;
    margin-top: -1px;
    background: #000;
  }
}

// style menu
.header__menu {
  position: fixed;
  right: 100%;
  top: 0;
  width: 200px;
  height: 100%;
  background: #222;
  color: #fff;
  padding: 0.5em;
  margin: 0;
  font-weight: bold;
  font-size: 1.2em;
  box-sizing: border-box;
  transition: all 0.2s ease-in;
  
  
  li {
    list-style: none;
  }
  
  a {
    color: inherit;
    text-decoration: none;
    display: inline-block;
    padding: 0.5em;
    
    &:hover {
      text-decoration: underline;
    }
  }
}

// override styles, depends on the checkbox
.header__checkbox:checked {
  
  // change the layout of the toggle
  &~.header__toggle {
    border-color: transparent;
    &:before {
      transform: rotate(135deg);
    }
    &:after {
      transform: rotate(45deg);
    }
  } 
  
  // show the menu
  &~.header__menu {
    transform: translateX(200px);
    transition: all 0.4s ease-out;
  }
}

// style the page
html {
  font-size: 18px;
  background: #f3f5f6;
  color: #222;
}
body {
  padding: 0 0.5em;
  box-sizing: border-box;
  background: inherit;
  padding-bottom: 2em;
}