DEV Community

Seppe Gadeyne
Seppe Gadeyne

Posted on Edited on

Crafting a mobile menu with Tailwind CSS without JavaScript

Welcome to this blog post, where I'll walk you through building a mobile menu using only Tailwind CSS and HTML, without JavaScript. This approach is excellent for enhancing website performance, as minimizing the use of JavaScript can help your site load faster. You can check out a live example of this technique on my website. Let's get started!

Using peer-* from Tailwind CSS for animations and state

To create the animations for our mobile menu, we'll utilize the peer-* utility from Tailwind CSS. This allows us to control the state of a sibling element, like an <input> element of the type checkbox. Every element that should react to the checkbox must come after it, as a sibling. We hide the <input> visually with sr-only (not hidden, because a display: none checkbox cannot receive keyboard focus) and let the <label> respond to touch or cursor. The <label> may only contain phrasing content, so it holds <span> elements, not <div> elements. Its accessible name comes from a screen-reader-only Menu text. peer-focus-visible: gives the label a visible focus outline when the checkbox is focused with the keyboard.

<div class="group">
  <input class="peer sr-only" type="checkbox" id="mobile-menu" />
  <label class="relative z-50 inline-block cursor-pointer px-3 py-6 peer-focus-visible:outline peer-focus-visible:outline-2 peer-focus-visible:outline-offset-2 peer-focus-visible:outline-black" for="mobile-menu">
    <span class="sr-only">Menu</span>
  </label>
</div>
Enter fullscreen mode Exit fullscreen mode

Creating the hamburger icon with before: and after: pseudo-elements

To craft the hamburger icon, we'll use the before: and after: pseudo-elements. We'll start with a <span> element with a height of 1px and a width equal to the w-7 value. The middle line is the element itself. When the menu opens, it becomes transparent, so only the rotated lines from before: and after: remain, forming a cross. Because the icon sits inside the <label>, it cannot be a peer sibling of the checkbox. It reacts through a group wrapper and the group-has-[:checked]: variant instead.

<div class="group">
  <input class="peer sr-only" type="checkbox" id="mobile-menu" />
  <label class="relative z-50 inline-block cursor-pointer px-3 py-6 peer-focus-visible:outline peer-focus-visible:outline-2 peer-focus-visible:outline-offset-2 peer-focus-visible:outline-black" for="mobile-menu">
    <span class="sr-only">Menu</span>
    <span
      class="relative block h-[1px] w-7 bg-black content-[''] before:absolute before:top-[-0.35rem] before:block before:h-full before:w-full before:bg-black before:transition-all before:duration-200 before:ease-out before:content-[''] after:absolute after:right-0 after:bottom-[-0.35rem] after:block after:h-full after:w-full after:bg-black after:transition-all after:duration-200 after:ease-out after:content-[''] group-has-[:checked]:bg-transparent before:group-has-[:checked]:top-0 before:group-has-[:checked]:rotate-45 after:group-has-[:checked]:bottom-0 after:group-has-[:checked]:-rotate-45"
    ></span>
  </label>
</div>
Enter fullscreen mode Exit fullscreen mode

Completing the mobile menu with additional elements

Now, let's add more elements to complete the mobile menu:

  • A full-screen background with a backdrop blur filter, which is a second <label> so a click on it closes the menu
  • A <div> element for the menu items with an animation, which scrolls the menu from right to left
  • The navigation itself, using a <nav> element with an aria-label and a <ul> list. The <menu> element is meant for a list of commands, so for navigation links <ul> is the right choice (see semantics in HTML)
<div class="group">
  <input class="peer sr-only" type="checkbox" id="mobile-menu" />
  <label class="relative z-50 inline-block cursor-pointer px-3 py-6 peer-focus-visible:outline peer-focus-visible:outline-2 peer-focus-visible:outline-offset-2 peer-focus-visible:outline-black" for="mobile-menu">
    <span class="sr-only">Menu</span>
    <span
      class="relative block h-[1px] w-7 bg-black content-[''] before:absolute before:top-[-0.35rem] before:block before:h-full before:w-full before:bg-black before:transition-all before:duration-200 before:ease-out before:content-[''] after:absolute after:right-0 after:bottom-[-0.35rem] after:block after:h-full after:w-full after:bg-black after:transition-all after:duration-200 after:ease-out after:content-[''] group-has-[:checked]:bg-transparent before:group-has-[:checked]:top-0 before:group-has-[:checked]:rotate-45 after:group-has-[:checked]:bottom-0 after:group-has-[:checked]:-rotate-45"
    ></span>
  </label>
  <label
    for="mobile-menu"
    class="fixed inset-0 z-30 hidden h-full w-full bg-black/50 backdrop-blur-sm peer-checked:block"
  ></label>
  <div
    class="fixed top-0 right-0 z-40 h-full w-full translate-x-full overflow-y-auto overscroll-y-none transition duration-500 peer-checked:translate-x-0"
  >
    <div class="float-right min-h-full w-[85%] bg-white px-6 pt-12 shadow-2xl">
      <nav aria-label="Main navigation">
        <ul>
          <li><a href="/">Home</a></li>
          <li><a href="/contact">Contact</a></li>
        </ul>
      </nav>
    </div>
  </div>
</div>
Enter fullscreen mode Exit fullscreen mode

Conclusion

In this blog post, we've successfully built a lightweight mobile menu using Tailwind CSS and HTML without JavaScript. Less JavaScript can help your website's load speed, which is one part of a higher search engine ranking. So try and experience the benefits of a more performant, JavaScript-free mobile menu.

Top comments (13)

Collapse
 
abhinandhand profile image
Abhinandhan Devadiga •

I encountered an issue with the recent version of TailwindCSS where the 'peer-checked' class only applies to sibling elements, not to nested elements or pseudo-elements. For those who face this issue, I recommend using a combination of 'group' and 'peer-checked' to achieve the desired effect

      <label
        class="sm:hidden relative z-40 cursor-pointer px-0 py-6"
        for="mobile-menu"
      >
        <input id="mobile-menu" class="peer hidden" type="checkbox" />
        <div class="relative z-50 block w-7 h-4 group">
          <div
            class="absolute top-0 left-0 w-full h-[3px] bg-green-500 transition-transform duration-200 ease-out group-peer-checked:rotate-45 group-peer-checked:top-1/2"
          ></div>
          <div
            class="absolute group-peer-checked:top-2 bottom-0 left-0 w-full h-[3px] bg-green-500 transition-transform duration-200 ease-out group-peer-checked:-rotate-45 group-peer-checked:bottom-1/2"
          ></div>
        </div>
      </label>
Enter fullscreen mode Exit fullscreen mode
Collapse
 
stankukucka profile image
Stan Kukučka •

@seppegadeyne great tutorial. I was looking for a non-js solution in Tailwind. Is it possible to modify the menu the way it will act the same as here: web3templates.com/preview/astroshi...?

Collapse
 
seppegadeyne profile image
Seppe Gadeyne •

Hi @stankukucka, thank you for your feedback. It is also possible. I will make an example and add it to the tutorial later.

Collapse
 
seppegadeyne profile image
Seppe Gadeyne •

Hi @stankukucka, Thanks for working together to create a vertical dropdown menu like on web3templates.com. You can view the demo and source on Github.

Thread Thread
 
stankukucka profile image
Stan Kukučka •

Thanks @seppegadeyne it was a great experience to work together on such a "small project".

Collapse
 
godfreymutebi profile image
Mutebi Godfrey •

if i want to change the background color from black to white . I want it to be white before and black after click.

Collapse
 
seppegadeyne profile image
Seppe Gadeyne •

Hi @godfreymutebi, If you have three lines, you can do so by adjusting the background color of the before and after content + background color. So before the click, white = before:bg-white bg-white after:bg-white and then black after click = before:peer-checked:bg-black peer-checked:bg-black after:peer-checked:bg-black. I hope this can help you.

Collapse
 
godfreymutebi profile image
Mutebi Godfrey •

thanks, its what i deed

Collapse
 
andberry profile image
Andrea Berardi •

Thank you so much for sharing this, great post!

Collapse
 
vtuz profile image
Valeriy •

Very nice solution. I'm thinking of use similar not only for menu but to separate screens of content in mobile devices.

Collapse
 
wade_thomas_8bf988797662d profile image
Wade Thomas • • Edited

Hi, I really love this approach but Im not getting the menu lines to rotate to form the close button. Everything else works fine. The code is exactly as you have it. I'm not sure what I missed. I'm using react router framework.

Collapse
 
thrasher profile image
Enrico Monzeglio •

Great! thanks for sharing.

Collapse
 
iliya1385 profile image
Blue boii •

hi
its amazing
but i want when i click on any links in menu, menu be closed
ty