DEV Community

joachim kliemann
joachim kliemann

Posted on

6

Toggling Mobile Navigation Visibility with CSS: The Checkbox Hack

In a mobile-first world, creating an intuitive and responsive navigation menu is crucial. The checkbox hack allows you to develop mobile flyout menus without JavaScript. This article will guide you through developing several styles of mobile navigation menus using the checkbox hack.

Basics of Checkbox Hack

The checkbox hack is based on three components:

  1. A <label> element.
  2. An associated <input type="checkbox">.
  3. A CSS rule that targets the :checked state of the checkbox.

The idea is to toggle the checkbox state by clicking on the label, and then using the :checked CSS pseudo-class to style or reveal a sibling element.

Basic HTML Structure

<input type="checkbox" id="menuToggle">
<label for="menuToggle">☰ Menu</label>
<nav>
  <ul>
    <li><a href="#">Home</a></li>
    <li><a href="#">Portfolio</a></li>
    <li><a href="#">About</a></li>
    <li><a href="#">Contact</a></li>
  </ul>
</nav>
Enter fullscreen mode Exit fullscreen mode

Different Styles of Menus

Side Menu (Left to Right)

nav {
    position: absolute;
    top: 0;
    left: -300px; /* width of the menu */
    width: 300px;
    height: 100vh;
    transition: 0.3s;
}

#menuToggle:checked + label + nav {
    left: 0;
}
Enter fullscreen mode Exit fullscreen mode

Sliding Menu (Top to Bottom)

nav {
    position: absolute;
    top: -100vh;
    left: 0;
    width: 100vw;
    height: 100vh;
    transition: 0.3s;
}

#menuToggle:checked + label + nav {
    top: 0;
}
Enter fullscreen mode Exit fullscreen mode

Splash Menu (Centered)

nav {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%) scale(0);
    width: 80vw;
    height: 80vh;
    transition: 0.3s;
    background-color: rgba(0,0,0,0.7);
    color: white;
    border-radius: 15px;
}

#menuToggle:checked + label + nav {
    transform: translate(-50%, -50%) scale(1);
}
Enter fullscreen mode Exit fullscreen mode

Enhancements and Notes

  • You can further style the label as a hamburger icon, and change it to a close icon when the menu is open using CSS.
  • For better accessibility, ensure to provide fallbacks or alternatives for users who may not be able to interact with this kind of menu.
  • Although the checkbox hack is clever and works in most modern browsers, for more complex interactions or larger projects, JavaScript frameworks or libraries might be more suitable.

Conclusion

The checkbox hack offers a lightweight method to create interactive components without relying on JavaScript. Whether you choose a sidemenu, a top-down sliding menu, or a splash menu, you can achieve a smooth user experience on mobile devices. Experiment with styles and transitions to fit the look and feel of your website!

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❀ or a friendly comment on this post if you found it helpful!

Okay