Navbar
Tailwind CSS and Alpine JS Navbar
Requires Alpine JS
This component requires Alpine JS v3 to function properly. Some advanced features may require additional Alpine plugins (such as focus).
Tell Me MoreDefault navbar
HTML
<nav x-data="{ mobileMenuIsOpen: false }" @click.away="mobileMenuIsOpen = false" class="" aria-label="penguin ui menu">
<!-- Brand Logo -->
<a href="#" class="">
<span>Peng<span class="">ui</span>n</span>
<!-- <img src="./your-logo.svg" alt="brand logo" class="w-10" /> -->
</a>
<!-- Desktop Menu -->
<ul class="">
<li><a href="#" class="" aria-current="page">Products</a></li>
<li><a href="#" class="">Pricing</a></li>
<li><a href="#" class="">Blog</a></li>
<li><a href="#" class="">Login</a></li>
</ul>
<!-- Mobile Menu Button -->
<button @click="mobileMenuIsOpen = !mobileMenuIsOpen" :aria-expanded="mobileMenuIsOpen" :class="mobileMenuIsOpen ? 'fixed top-6 right-6 z-20' : null" type="button" class="" aria-label="mobile menu" aria-controls="mobileMenu">
<svg x-cloak x-show="!mobileMenuIsOpen" xmlns="http://www.w3.org/2000/svg" fill="none" aria-hidden="true" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" class="">
<path stroke-linecap="round" stroke-linejoin="round" d="M3.75 6.75h16.5M3.75 12h16.5m-16.5 5.25h16.5" />
</svg>
<svg x-cloak x-show="mobileMenuIsOpen" xmlns="http://www.w3.org/2000/svg" fill="none" aria-hidden="true" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" class="">
<path stroke-linecap="round" stroke-linejoin="round" d="M6 18 18 6M6 6l12 12" />
</svg>
</button>
<!-- Mobile Menu -->
<ul x-cloak x-show="mobileMenuIsOpen" x-transition:enter="transition motion-reduce:transition-none ease-out duration-300" x-transition:enter-start="-translate-y-full" x-transition:enter-end="translate-y-0" x-transition:leave="transition motion-reduce:transition-none ease-out duration-300" x-transition:leave-start="translate-y-0" x-transition:leave-end="-translate-y-full" id="mobileMenu" class="">
<li class=""><a href="#" class="" aria-current="page">Products</a></li>
<li class=""><a href="#" class="">Pricing</a></li>
<li class=""><a href="#" class="">Blog</a></li>
<li class=""><a href="#" class="">Login</a></li>
</ul>
</nav>
With a call to action
HTML
<nav x-data="{ mobileMenuIsOpen: false }" @click.away="mobileMenuIsOpen = false" class="" aria-label="penguin ui menu">
<!-- Brand Logo -->
<a href="#" class="">
<span>Peng<span class="">ui</span>n</span>
<!-- <img src="./your-logo.svg" alt="brand logo" class="w-10" /> -->
</a>
<!-- Desktop Menu -->
<ul class="">
<li><a href="#" class="" aria-current="page">Products</a></li>
<li><a href="#" class="">Pricing</a></li>
<li><a href="#" class="">Blog</a></li>
<!-- CTA Button -->
<li><a href="#" class="">Sign Up</a></li>
</ul>
<!-- Mobile Menu Button -->
<button @click="mobileMenuIsOpen = !mobileMenuIsOpen" :aria-expanded="mobileMenuIsOpen" :class="mobileMenuIsOpen ? 'fixed top-6 right-6 z-20' : null" type="button" class="" aria-label="mobile menu" aria-controls="mobileMenu">
<svg x-cloak x-show="!mobileMenuIsOpen" xmlns="http://www.w3.org/2000/svg" fill="none" aria-hidden="true" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" class="">
<path stroke-linecap="round" stroke-linejoin="round" d="M3.75 6.75h16.5M3.75 12h16.5m-16.5 5.25h16.5" />
</svg>
<svg x-cloak x-show="mobileMenuIsOpen" xmlns="http://www.w3.org/2000/svg" fill="none" aria-hidden="true" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" class="">
<path stroke-linecap="round" stroke-linejoin="round" d="M6 18 18 6M6 6l12 12" />
</svg>
</button>
<!-- Mobile Menu -->
<ul x-cloak x-show="mobileMenuIsOpen" x-transition:enter="transition motion-reduce:transition-none ease-out duration-300" x-transition:enter-start="-translate-y-full" x-transition:enter-end="translate-y-0" x-transition:leave="transition motion-reduce:transition-none ease-out duration-300" x-transition:leave-start="translate-y-0" x-transition:leave-end="-translate-y-full" id="mobileMenu" class="">
<li class=""><a href="#" class="" aria-current="page">Products</a></li>
<li class=""><a href="#" class="">Pricing</a></li>
<li class=""><a href="#" class="">Blog</a></li>
<!-- CTA Button -->
<li class=""><a href="#" class="">Sign Up</a></li>
</ul>
</nav>
With a user profile
HTML
<nav x-data="{ mobileMenuIsOpen: false }" @click.away="mobileMenuIsOpen = false" class="" aria-label="penguin ui menu">
<!-- Brand Logo -->
<a href="#" class="">
<span>Peng<span class="">ui</span>n</span>
<!-- <img src="./your-logo.svg" alt="brand logo" class="w-10" /> -->
</a>
<!-- Desktop Menu -->
<ul class="">
<li><a href="#" class="" aria-current="page">Products</a></li>
<li><a href="#" class="">Pricing</a></li>
<li><a href="#" class="">Blog</a></li>
<!-- User Pic -->
<li x-data="{ userDropDownIsOpen: false, openWithKeyboard: false }" @keydown.esc.window="userDropDownIsOpen = false, openWithKeyboard = false" class="">
<button @click="userDropDownIsOpen = ! userDropDownIsOpen" :aria-expanded="userDropDownIsOpen" @keydown.space.prevent="openWithKeyboard = true" @keydown.enter.prevent="openWithKeyboard = true" @keydown.down.prevent="openWithKeyboard = true" class="" aria-controls="userMenu">
<img src="https://penguinui.s3.amazonaws.com/component-assets/avatar-8.webp" alt="User Profile" class="" />
</button>
<!-- User Dropdown -->
<ul x-cloak x-show="userDropDownIsOpen || openWithKeyboard" x-transition.opacity x-trap="openWithKeyboard" @click.outside="userDropDownIsOpen = false, openWithKeyboard = false" @keydown.down.prevent="$focus.wrap().next()" @keydown.up.prevent="$focus.wrap().previous()" id="userMenu" class="">
<li class="">
<div class="">
<span class="">Alice Brown</span>
<p class="">alice.brown@gmail.com</p>
</div>
</li>
<li><a href="#" class="">Dashboard</a></li>
<li><a href="#" class="">Subscription</a></li>
<li><a href="#" class="">Settings</a></li>
<li><a href="#" class="">Sign Out</a></li>
</ul>
</li>
</ul>
<!-- Mobile Menu Button -->
<button @click="mobileMenuIsOpen = !mobileMenuIsOpen" :aria-expanded="mobileMenuIsOpen" :class="mobileMenuIsOpen ? 'fixed top-6 right-6 z-20' : null" type="button" class="" aria-label="mobile menu" aria-controls="mobileMenu">
<svg x-cloak x-show="!mobileMenuIsOpen" xmlns="http://www.w3.org/2000/svg" fill="none" aria-hidden="true" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" class="">
<path stroke-linecap="round" stroke-linejoin="round" d="M3.75 6.75h16.5M3.75 12h16.5m-16.5 5.25h16.5" />
</svg>
<svg x-cloak x-show="mobileMenuIsOpen" xmlns="http://www.w3.org/2000/svg" fill="none" aria-hidden="true" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" class="">
<path stroke-linecap="round" stroke-linejoin="round" d="M6 18 18 6M6 6l12 12" />
</svg>
</button>
<!-- Mobile Menu -->
<ul x-cloak x-show="mobileMenuIsOpen" x-transition:enter="transition motion-reduce:transition-none ease-out duration-300" x-transition:enter-start="-translate-y-full" x-transition:enter-end="translate-y-0" x-transition:leave="transition motion-reduce:transition-none ease-out duration-300" x-transition:leave-start="translate-y-0" x-transition:leave-end="-translate-y-full" class="">
<li class="">
<div class="">
<img src="https://penguinui.s3.amazonaws.com/component-assets/avatar-8.webp" alt="User Profile" class="" />
<div>
<span class="">Alice Brown</span>
<p class="">alice.brown@gmail.com</p>
</div>
</div>
</li>
<li class=""><a href="#" class="" aria-current="page">Products</a></li>
<li class=""><a href="#" class="">Pricing</a></li>
<li class=""><a href="#" class="">Blog</a></li>
<hr role="none" class="">
<li class=""><a href="#" class="">Dashboard</a></li>
<li class=""><a href="#" class="">Subscription</a></li>
<li class=""><a href="#" class="">Settings</a></li>
<!-- CTA Button -->
<li class=""><a href="#" class="">Sign Out</a></li>
</ul>
</nav>
With a search
HTML
<nav x-data="{ mobileMenuIsOpen: false }" @click.away="mobileMenuIsOpen = false" class="" aria-label="penguin ui menu">
<!-- Brand Logo -->
<a href="#" class="">
<span>Peng<span class="">ui</span>n</span>
<!-- <img src="./your-logo.svg" alt="brand logo" class="w-10" /> -->
</a>
<!-- Search -->
<div class="">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" aria-hidden="true" class="">
<path stroke-linecap="round" stroke-linejoin="round" d="m21 21-5.197-5.197m0 0A7.5 7.5 0 1 0 5.196 5.196a7.5 7.5 0 0 0 10.607 10.607Z" />
</svg>
<input type="search" name="search" placeholder="Search" aria-label="search" class="" />
</div>
<!-- Desktop Menu -->
<ul class="">
<li><a href="#" class="" aria-current="page">Products</a></li>
<li><a href="#" class="">Pricing</a></li>
<li><a href="#" class="">Blog</a></li>
<!-- User Pic -->
<li x-data="{ userDropDownIsOpen: false, openWithKeyboard: false }" @keydown.esc.window="userDropDownIsOpen = false, openWithKeyboard = false" class="">
<button @click="userDropDownIsOpen = ! userDropDownIsOpen" :aria-expanded="userDropDownIsOpen" @keydown.space.prevent="openWithKeyboard = true" @keydown.enter.prevent="openWithKeyboard = true" @keydown.down.prevent="openWithKeyboard = true" class="" aria-controls="userMenu">
<img src="https://penguinui.s3.amazonaws.com/component-assets/avatar-8.webp" alt="User Profile" class="" />
</button>
<!-- User Dropdown -->
<ul x-cloak x-show="userDropDownIsOpen || openWithKeyboard" x-transition.opacity x-trap="openWithKeyboard" @click.outside="userDropDownIsOpen = false, openWithKeyboard = false" @keydown.down.prevent="$focus.wrap().next()" @keydown.up.prevent="$focus.wrap().previous()" id="userMenu" class="">
<li class="">
<div class="">
<span class="">Alice Brown</span>
<p class="">alice.brown@gmail.com</p>
</div>
</li>
<li><a href="#" class="">Dashboard</a></li>
<li><a href="#" class="">Subscription</a></li>
<li><a href="#" class="">Settings</a></li>
<li><a href="#" class="">Sign Out</a></li>
</ul>
</li>
</ul>
<!-- Mobile Menu Button -->
<button @click="mobileMenuIsOpen = !mobileMenuIsOpen" :aria-expanded="mobileMenuIsOpen" :class="mobileMenuIsOpen ? 'fixed top-6 right-6 z-20' : null" type="button" class="" aria-label="mobile menu" aria-controls="mobileMenu">
<svg x-cloak x-show="!mobileMenuIsOpen" xmlns="http://www.w3.org/2000/svg" fill="none" aria-hidden="true" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" class="">
<path stroke-linecap="round" stroke-linejoin="round" d="M3.75 6.75h16.5M3.75 12h16.5m-16.5 5.25h16.5" />
</svg>
<svg x-cloak x-show="mobileMenuIsOpen" xmlns="http://www.w3.org/2000/svg" fill="none" aria-hidden="true" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" class="">
<path stroke-linecap="round" stroke-linejoin="round" d="M6 18 18 6M6 6l12 12" />
</svg>
</button>
<!-- Mobile Menu -->
<ul x-cloak x-show="mobileMenuIsOpen" x-transition:enter="transition motion-reduce:transition-none ease-out duration-300" x-transition:enter-start="-translate-y-full" x-transition:enter-end="translate-y-0" x-transition:leave="transition motion-reduce:transition-none ease-out duration-300" x-transition:leave-start="translate-y-0" x-transition:leave-end="-translate-y-full" class="">
<li class="">
<div class="">
<img src="https://penguinui.s3.amazonaws.com/component-assets/avatar-8.webp" alt="User Profile" class="" />
<div>
<span class="">Alice Brown</span>
<p class="">alice.brown@gmail.com</p>
</div>
</div>
</li>
<li class=""><a href="#" class="" aria-current="page">Products</a></li>
<li class=""><a href="#" class="">Pricing</a></li>
<li class=""><a href="#" class="">Blog</a></li>
<hr role="none" class="">
<li class=""><a href="#" class="">Dashboard</a></li>
<li class=""><a href="#" class="">Subscription</a></li>
<li class=""><a href="#" class="">Settings</a></li>
<!-- CTA Button -->
<li class=""><a href="#" class="">Sign Out</a></li>
</ul>
</nav>
Data
List of all Alpine JS data used in this component.
Property | Description |
---|---|
mobileMenuIsOpen |
Boolean - Indicates whether the menu is open or closed |
userDropDownIsOpen |
Boolean - Indicates whether the user dropdown menu is open or closed |
openWithKeyboard |
Boolean - Indicates whether the user dropdown menu is opened using keyboard |
Keyboard Navigation
Key | Action |
---|---|
Tab |
Next item gets the focus |
Enter Space | Focused item gets selected |
Dropdown: Focus moves to next/previous item | |
Esc |
Dropdown open: dropdown closes |