- Published on
How To Create A simple nav & sidebar With Tailwind CSS In 5 Easy Steps

- What is Tailwind CSS?
- The description of simple nav & sidebar ui component
- Why use Tailwind CSS to create a simple nav & sidebar ui component?
- The preview of simple nav & sidebar ui component
- The source code of simple nav & sidebar ui component
- How to create a simple nav & sidebar with Tailwind CSS?
- Install tailwind css of verion 3.0.18
- All the unility class needed to create a simple nav & sidebar component
- 28 steps to create a simple nav & sidebar component with Tailwind CSS
- Conclusion
What is Tailwind CSS?
Tailwind CSS is a utility-first CSS framework that focuses on creating personalized user interfaces quickly. It can gives you all the building blocks you are able to create personalized designs without having to fight to override irritating opinionated styles. Also, Tailwind CSS is a highly configurable, low-level CSS framework.
The description of simple nav & sidebar ui component
Simple layout that have navigation and sidebar with a button to toggle sidebar. (note: it is not responsive)
Why use Tailwind CSS to create a simple nav & sidebar ui component?
- It can make the building process of simple nav & sidebar ui component faster and more easily.
- Enables building complex responsive layouts and components freely.
- Minimum lines of CSS code in simple nav & sidebar component file.
The preview of simple nav & sidebar ui component
Free download of the simple nav & sidebar's source code
The source code of simple nav & sidebar ui component
<script defer src="https://unpkg.com/[email protected]/dist/cdn.min.js"></script>
<link rel="stylesheet" href="https://unpkg.com/[email protected]/css/boxicons.min.css" />
<!-- page -->
<main class="min-h-screen w-full bg-gray-100 text-gray-700" x-data="layout">
<!-- header page -->
<header class="flex w-full items-center justify-between border-b-2 border-gray-200 bg-white p-2">
<!-- logo -->
<div class="flex items-center space-x-2">
<button type="button" class="text-3xl" @click="asideOpen = !asideOpen"><i class="bx bx-menu"></i></button>
<div>Logo</div>
</div>
<!-- button profile -->
<div>
<button type="button" @click="profileOpen = !profileOpen" @click.outside="profileOpen = false"
class="h-9 w-9 overflow-hidden rounded-full">
<img src="https://plchldr.co/i/40x40?bg=111111" alt="plchldr.co" />
</button>
<!-- dropdown profile -->
<div class="absolute right-2 mt-1 w-48 divide-y divide-gray-200 rounded-md border border-gray-200 bg-white shadow-md"
x-show="profileOpen" x-transition>
<div class="flex items-center space-x-2 p-2">
<img src="https://plchldr.co/i/40x40?bg=111111" alt="plchldr.co" class="h-9 w-9 rounded-full" />
<div class="font-medium">Hafiz Haziq</div>
</div>
<div class="flex flex-col space-y-3 p-2">
<a href="#" class="transition hover:text-blue-600">My Profile</a>
<a href="#" class="transition hover:text-blue-600">Edit Profile</a>
<a href="#" class="transition hover:text-blue-600">Settings</a>
</div>
<div class="p-2">
<button class="flex items-center space-x-2 transition hover:text-blue-600">
<svg class="h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"
xmlns="http://www.w3.org/2000/svg">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
d="M17 16l4-4m0 0l-4-4m4 4H7m6 4v1a3 3 0 01-3 3H6a3 3 0 01-3-3V7a3 3 0 013-3h4a3 3 0 013 3v1">
</path>
</svg>
<div>Log Out</div>
</button>
</div>
</div>
</div>
</header>
<div class="flex">
<!-- aside -->
<aside class="flex w-72 flex-col space-y-2 border-r-2 border-gray-200 bg-white p-2" style="height: 90.5vh"
x-show="asideOpen">
<a href="#" class="flex items-center space-x-1 rounded-md px-2 py-3 hover:bg-gray-100 hover:text-blue-600">
<span class="text-2xl"><i class="bx bx-home"></i></span>
<span>Dashboard</span>
</a>
<a href="#" class="flex items-center space-x-1 rounded-md px-2 py-3 hover:bg-gray-100 hover:text-blue-600">
<span class="text-2xl"><i class="bx bx-cart"></i></span>
<span>Cart</span>
</a>
<a href="#" class="flex items-center space-x-1 rounded-md px-2 py-3 hover:bg-gray-100 hover:text-blue-600">
<span class="text-2xl"><i class="bx bx-shopping-bag"></i></span>
<span>Shopping</span>
</a>
<a href="#" class="flex items-center space-x-1 rounded-md px-2 py-3 hover:bg-gray-100 hover:text-blue-600">
<span class="text-2xl"><i class="bx bx-heart"></i></span>
<span>My Favourite</span>
</a>
<a href="#" class="flex items-center space-x-1 rounded-md px-2 py-3 hover:bg-gray-100 hover:text-blue-600">
<span class="text-2xl"><i class="bx bx-user"></i></span>
<span>Profile</span>
</a>
</aside>
<!-- main content page -->
<div class="w-full p-4">Lorem ipsum dolor sit amet consectetur, adipisicing elit. Expedita quam odit officiis
magni doloribus ipsa dolore, dolores nihil accusantium labore, incidunt autem iure quae vitae voluptate,
esse asperiores aliquam repellat. Harum aliquid non officiis porro at cumque eaque inventore iure. Modi sunt
optio mollitia repellat sed ab quibusdam quos harum!</div>
</div>
</main>
<script>
document.addEventListener("alpine:init", () => {
Alpine.data("layout", () => ({
profileOpen: false,
asideOpen: true,
}));
});
</script>
How to create a simple nav & sidebar with Tailwind CSS?
Install tailwind css of verion 3.0.18
Use the script
html tag to import the script of Tailwind CSS of the version 3.0.18
<script src="https://cdn.tailwindcss.com"></script>
All the unility class needed to create a simple nav & sidebar component
min-h-screen
w-full
bg-gray-100
text-gray-700
flex
border-b-2
border-gray-200
bg-white
p-2
text-3xl
h-9
w-9
overflow-hidden
absolute
right-2
mt-1
w-48
flex-col
hover:text-blue-600
h-4
w-4
w-72
border-r-2
px-2
py-3
hover:bg-gray-100
text-2xl
p-4
28 steps to create a simple nav & sidebar component with Tailwind CSS
Set the minimum width/height of an element using the
min-h-screen
utilities.Use
w-full
to set an element to a 100% based width.Control the background color of an element to gray-100 using the
bg-gray-100
utilities.Control the text color of an element to gray-700 using the
text-gray-700
utilities.Use
flex
to create a block-level flex container.Control the border color of an element to b-2 using the
border-b-2
utilities.Control the border color of an element to gray-200 using the
border-gray-200
utilities.Control the background color of an element to white using the
bg-white
utilities.Control the padding on all sides of an element to 0.5rem using the
p-2
utilities.Control the text color of an element to 3xl using the
text-3xl
utilities.Use
h-9
to set an element to a fixed height(2.25rem).Use
w-9
to set an element to a fixed width(2.25rem).Use
overflow-hidden
to clip any content within an element that overflows the bounds of that element.Use
absolute
to position an element outside of the normal flow of the document, causing neighboring elements to act as if the element doesn’t exist.Use the
right-2
utilities to set the right position of a positioned element to 0.5rem.Control the margin on top side of an element to 0.25rem using the
mt-1
utilities.Use
w-48
to set an element to a fixed width(12rem).Use
flex
to create a block-level flex container.Control the text color of an element to blue-600 on hover using the
hover:text-blue-600
utilities.Use
h-4
to set an element to a fixed height(1rem).Use
w-4
to set an element to a fixed width(1rem).Use
w-72
to set an element to a fixed width(18rem).Control the border color of an element to r-2 using the
border-r-2
utilities.Control the horizontal padding of an element to 0.5rem using the
px-2
utilities.Control the vertical padding of an element to 0.75rem using the
py-3
utilities.Control the background color of an element to gray-100 using the
hover:bg-gray-100
utilities on hover.Control the text color of an element to 2xl using the
text-2xl
utilities.Control the padding on all sides of an element to 1rem using the
p-4
utilities.
Conclusion
The above is a step-by-step tutorial on how to use Tailwind CSS to create a simple nav & sidebar components, learn and follow along to implement your own components.