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>
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>
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 anaria-labeland 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>
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)
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
@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...?
Hi @stankukucka, thank you for your feedback. It is also possible. I will make an example and add it to the tutorial later.
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.
Thanks @seppegadeyne it was a great experience to work together on such a "small project".
if i want to change the background color from black to white . I want it to be white before and black after click.
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-whiteand then black after click =before:peer-checked:bg-black peer-checked:bg-black after:peer-checked:bg-black. I hope this can help you.thanks, its what i deed
Thank you so much for sharing this, great post!
Very nice solution. I'm thinking of use similar not only for menu but to separate screens of content in mobile devices.
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.
Great! thanks for sharing.
hi
its amazing
but i want when i click on any links in menu, menu be closed
ty