search

Home  >  Q&A  >  body text

Show hidden side panels: checked

I would like to have a navigation menu in the side panel with the ability to switch to views via an animated hamburger menu. I want to create it using only CSS and not any JS.

:checked Pseudo classes seem to be the way to go, but I can't get it to work. Code I have so far:

.site-navigation {
  z-index: 99;
  background-color: rgba(0, 0, 0, 0.8);
  position: fixed;
  top: 0;
  left: 0;
  width: 40%;
  height: 100%;
  opacity: 0;
  visibility: hidden;
  transition: all 0.3s ease-in-out;
}

.toggle-btn {
  display: block;
  position: fixed;
  z-index: 10;
  right: 10px;
  top: 10px;
  cursor: pointer;
}

.toggle-btn .bar {
  width: 30px;
  height: 2px;
  margin: 7px auto;
  background-color: #fff;
  transition: all 0.3s ease-in-out;
  box-shadow: 0 0 3px 1px rgba(0, 0, 0, 0.3);
}

.toggle-btn .bar:nth-child(2) {
  width: 28px;
}

#toggle {
  display: none;
}

#toggle:checked~.site-navigation {
  display: block;
  opacity: 1;
  visibility: visible;
}

#toggle:checked~nav ul {
  top: 70px;
}

#toggle:checked~nav ul li {
  transform: translateY(0px);
  opacity: 1;
}

#toggle:checked+label.toggle-btn .bar {
  background-color: #efefef;
}

#toggle:checked+label.toggle-btn .bar:nth-child(2) {
  opacity: 0;
}

#toggle:checked+label.toggle-btn .bar:nth-child(1) {
  transform: translateY(10px) rotate(45deg);
}

#toggle:checked+label.toggle-btn .bar:nth-child(3) {
  transform: translateY(-8px) rotate(-45deg);
}
<header id="masthead" class="site-header" role="banner">
  <div class="container">

    <div id="brand">
      <h1 class="site-title"><a href="#">Nice site</a></h1>
    </div>

    <div id="menu">
      <input type="checkbox" id="toggle">
      <label class="toggle-btn toggle-btn__cross" for="toggle">
                <div class="bar"></div>
                <div class="bar"></div>
                <div class="bar"></div>
              </label>
    </div>

    <div class="clear"></div>
  </div>
  <!--/container -->

  <!-- .site-navigation .main-navigation -->
  <nav role="navigation" id="navigation" class="site-navigation main-navigation">
    <span class="menuLabel">menu</span>
    <ul>
      <li><a href="#">Home</a></li>
      <li><a href="#">About</a></li>
      <li><a href="#">News</a></li>
      <li><a href="#">Contact</a></li>
    </ul>
  </nav>

Any help and/or advice would be greatly appreciated!

P粉647504283P粉647504283229 days ago456

reply all(1)I'll reply

  • P粉064448449

    P粉0644484492024-04-07 00:23:59

    Fix typo in CSS and move INPUT element outside #menu

    To make this line of CSS meaningful

    #toggle:checked ~ .site-navigation {
    

    There is another way, which is to use :has() but I will stick with this simpler solution.

    reply
    0
  • Cancelreply