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 to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

SurveyJS custom survey software

Simplify data collection in your JS app with a fully integrated form management platform. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more. Integrates with any backend system, giving you full control over your data and no user limits.

Learn more

Retry later