Published on

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

simple nav & sidebar

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

  1. Set the minimum width/height of an element using the min-h-screen utilities.

  2. Use w-full to set an element to a 100% based width.

  3. Control the background color of an element to gray-100 using the bg-gray-100 utilities.

  4. Control the text color of an element to gray-700 using the text-gray-700 utilities.

  5. Use flex to create a block-level flex container.

  6. Control the border color of an element to b-2 using the border-b-2 utilities.

  7. Control the border color of an element to gray-200 using the border-gray-200 utilities.

  8. Control the background color of an element to white using the bg-white utilities.

  9. Control the padding on all sides of an element to 0.5rem using the p-2 utilities.

  10. Control the text color of an element to 3xl using the text-3xl utilities.

  11. Use h-9 to set an element to a fixed height(2.25rem).

  12. Use w-9 to set an element to a fixed width(2.25rem).

  13. Use overflow-hidden to clip any content within an element that overflows the bounds of that element.

  14. 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.

  15. Use the right-2 utilities to set the right position of a positioned element to 0.5rem.

  16. Control the margin on top side of an element to 0.25rem using the mt-1 utilities.

  17. Use w-48 to set an element to a fixed width(12rem).

  18. Use flex to create a block-level flex container.

  19. Control the text color of an element to blue-600 on hover using the hover:text-blue-600 utilities.

  20. Use h-4 to set an element to a fixed height(1rem).

  21. Use w-4 to set an element to a fixed width(1rem).

  22. Use w-72 to set an element to a fixed width(18rem).

  23. Control the border color of an element to r-2 using the border-r-2 utilities.

  24. Control the horizontal padding of an element to 0.5rem using the px-2 utilities.

  25. Control the vertical padding of an element to 0.75rem using the py-3 utilities.

  26. Control the background color of an element to gray-100 using the hover:bg-gray-100 utilities on hover.

  27. Control the text color of an element to 2xl using the text-2xl utilities.

  28. 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.