Home > OS >  CSS: Reverse :checked functionality
CSS: Reverse :checked functionality

Time:11-12

Hello fellow developers, I'm building a sidebar component with view and am struggling to reverse the :checked functionality. The issue is somewhere here:

  #menu__toggle:checked ~ .menu__box {
  left: 0 !important;
}

Expected result: I want the sidebar menu to be open by default. I want it to toggle -> close when I clicked.

The code snippet is pure html and css. I hope someone has a good idea how to fix this.

Thank you in advance.

#menu__toggle {
  opacity: 0;
}
#menu__toggle:checked   .menu__btn > span {
  transform: rotate(45deg);
}
#menu__toggle:checked   .menu__btn > span::before {
  top: 0;
  transform: rotate(0deg);
}
#menu__toggle:checked   .menu__btn > span::after {
  top: 0;
  transform: rotate(90deg);
}
#menu__toggle:checked ~ .menu__box {
  left: 0 !important;
}
.menu__btn {
  position: fixed;
  top: 20px;
  left: 20px;
  width: 26px;
  height: 26px;
  cursor: pointer;
  z-index: 1;
}
.menu__btn > span,
.menu__btn > span::before,
.menu__btn > span::after {
  display: block;
  position: absolute;
  width: 100%;
  height: 2px;
  background-color: #616161;
  transition-duration: .25s;
}
.menu__btn > span::before {
  content: '';
  top: -8px;
}
.menu__btn > span::after {
  content: '';
  top: 8px;
}
.menu__box {
  display: block;
  position: fixed;
  top: 0;
  left: -100%;
  width: 300px;
  height: 100%;
  margin: 0;
  padding: 80px 0;
  list-style: none;
  background-color: #ECEFF1;
  box-shadow: 2px 2px 6px rgba(0, 0, 0, .4);
  transition-duration: .25s;
}
.menu__item {
  display: block;
  padding: 12px 24px;
  color: #333;
  font-family: 'Roboto', sans-serif;
  font-size: 20px;
  font-weight: 600;
  text-decoration: none;
  transition-duration: .25s;
}
.menu__item:hover {
  background-color: #CFD8DC;
}
<div >
    <input id="menu__toggle" type="checkbox" />
    <label  for="menu__toggle">
      <span></span>
    </label>

    <ul >
      <li><a  href="#">Home</a></li>
            <li><a  href="#">About</a></li>
            <li><a  href="#">Team</a></li>
            <li><a  href="#">Contact</a></li>
            <li><a  href="#">Twitter</a></li>
    </ul>
  </div>

CodePudding user response:

You can reverse selectors with the :not pseudo selector. In your case simply combine it with the :checked selector.

#menu__toggle:not(:checked)
  • Related