Published on

Advanced Guide: Build A Tailwind CSS Sidebar With Tailwind CSS

Tailwind CSS 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 Tailwind CSS Sidebar ui component

Use this sidebar component on either side of the page to show a list of menu items and links flowbite.com/docs/components/sidebar/

Why use Tailwind CSS to create a Tailwind CSS Sidebar ui component?

  • It can make the building process of Tailwind CSS Sidebar ui component faster and more easily.
  • Enables building complex responsive layouts and components freely.
  • Minimum lines of CSS code in Tailwind CSS Sidebar component file.

The preview of Tailwind CSS Sidebar ui component

Free download of the Tailwind CSS Sidebar's source code

The source code of Tailwind CSS Sidebar ui component

<!-- This is an example component -->
<div class="max-w-2xl mx-auto">

	<aside class="w-64" aria-label="Sidebar">
		<div class="px-3 py-4 overflow-y-auto rounded bg-gray-50 dark:bg-gray-800">
			<ul class="space-y-2">
				<li>
					<a href="#"
						class="flex items-center p-2 text-base font-normal text-gray-900 rounded-lg dark:text-white hover:bg-gray-100 dark:hover:bg-gray-700">
						<svg class="w-6 h-6 text-gray-500 transition duration-75 dark:text-gray-400 group-hover:text-gray-900 dark:group-hover:text-white"
							fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg">
							<path d="M2 10a8 8 0 018-8v8h8a8 8 0 11-16 0z"></path>
							<path d="M12 2.252A8.014 8.014 0 0117.748 8H12V2.252z"></path>
						</svg>
						<span class="ml-3">Dashboard</span>
					</a>
				</li>
				<li>
					<a href="#" target="_blank"
						class="flex items-center p-2 text-base font-normal text-gray-900 rounded-lg dark:text-white hover:bg-gray-100 dark:hover:bg-gray-700">
						<svg class="flex-shrink-0 w-6 h-6 text-gray-500 transition duration-75 dark:text-gray-400 group-hover:text-gray-900 dark:group-hover:text-white"
							fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg">
							<path
								d="M5 3a2 2 0 00-2 2v2a2 2 0 002 2h2a2 2 0 002-2V5a2 2 0 00-2-2H5zM5 11a2 2 0 00-2 2v2a2 2 0 002 2h2a2 2 0 002-2v-2a2 2 0 00-2-2H5zM11 5a2 2 0 012-2h2a2 2 0 012 2v2a2 2 0 01-2 2h-2a2 2 0 01-2-2V5zM11 13a2 2 0 012-2h2a2 2 0 012 2v2a2 2 0 01-2 2h-2a2 2 0 01-2-2v-2z">
							</path>
						</svg>
						<span class="flex-1 ml-3 whitespace-nowrap">Kanban</span>
						<span class="inline-flex items-center justify-center px-2 ml-3 text-sm font-medium text-gray-800 bg-gray-200 rounded-full dark:bg-gray-700 dark:text-gray-300">Pro</span>
					</a>
				</li>
				<li>
					<a href="#" target="_blank"
						class="flex items-center p-2 text-base font-normal text-gray-900 rounded-lg dark:text-white hover:bg-gray-100 dark:hover:bg-gray-700">
						<svg class="flex-shrink-0 w-6 h-6 text-gray-500 transition duration-75 dark:text-gray-400 group-hover:text-gray-900 dark:group-hover:text-white"
							fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg">
							<path
								d="M8.707 7.293a1 1 0 00-1.414 1.414l2 2a1 1 0 001.414 0l2-2a1 1 0 00-1.414-1.414L11 7.586V3a1 1 0 10-2 0v4.586l-.293-.293z">
							</path>
							<path
								d="M3 5a2 2 0 012-2h1a1 1 0 010 2H5v7h2l1 2h4l1-2h2V5h-1a1 1 0 110-2h1a2 2 0 012 2v10a2 2 0 01-2 2H5a2 2 0 01-2-2V5z">
							</path>
						</svg>
						<span class="flex-1 ml-3 whitespace-nowrap">Inbox</span>
						<span class="inline-flex items-center justify-center w-3 h-3 p-3 ml-3 text-sm font-medium text-blue-600 bg-blue-200 rounded-full dark:bg-blue-900 dark:text-blue-200">3</span>
					</a>
				</li>
				<li>
					<a href="#"
						class="flex items-center p-2 text-base font-normal text-gray-900 rounded-lg dark:text-white hover:bg-gray-100 dark:hover:bg-gray-700">
						<svg class="flex-shrink-0 w-6 h-6 text-gray-500 transition duration-75 dark:text-gray-400 group-hover:text-gray-900 dark:group-hover:text-white"
							fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg">
							<path fill-rule="evenodd" d="M10 9a3 3 0 100-6 3 3 0 000 6zm-7 9a7 7 0 1114 0H3z"
								clip-rule="evenodd"></path>
						</svg>
						<span class="flex-1 ml-3 whitespace-nowrap">Users</span>
					</a>
				</li>
				<li>
					<a href="#"
						class="flex items-center p-2 text-base font-normal text-gray-900 rounded-lg dark:text-white hover:bg-gray-100 dark:hover:bg-gray-700">
						<svg class="flex-shrink-0 w-6 h-6 text-gray-500 transition duration-75 dark:text-gray-400 group-hover:text-gray-900 dark:group-hover:text-white"
							fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg">
							<path fill-rule="evenodd"
								d="M10 2a4 4 0 00-4 4v1H5a1 1 0 00-.994.89l-1 9A1 1 0 004 18h12a1 1 0 00.994-1.11l-1-9A1 1 0 0015 7h-1V6a4 4 0 00-4-4zm2 5V6a2 2 0 10-4 0v1h4zm-6 3a1 1 0 112 0 1 1 0 01-2 0zm7-1a1 1 0 100 2 1 1 0 000-2z"
								clip-rule="evenodd"></path>
						</svg>
						<span class="flex-1 ml-3 whitespace-nowrap">Products</span>
					</a>
				</li>
				<li>
					<a href="#"
						class="flex items-center p-2 text-base font-normal text-gray-900 rounded-lg dark:text-white hover:bg-gray-100 dark:hover:bg-gray-700">
						<svg class="flex-shrink-0 w-6 h-6 text-gray-500 transition duration-75 dark:text-gray-400 group-hover:text-gray-900 dark:group-hover:text-white"
							fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg">
							<path fill-rule="evenodd"
								d="M3 3a1 1 0 00-1 1v12a1 1 0 102 0V4a1 1 0 00-1-1zm10.293 9.293a1 1 0 001.414 1.414l3-3a1 1 0 000-1.414l-3-3a1 1 0 10-1.414 1.414L14.586 9H7a1 1 0 100 2h7.586l-1.293 1.293z"
								clip-rule="evenodd"></path>
						</svg>
						<span class="flex-1 ml-3 whitespace-nowrap">Sign In</span>
					</a>
				</li>
				<li>
					<a href="#"
						class="flex items-center p-2 text-base font-normal text-gray-900 rounded-lg dark:text-white hover:bg-gray-100 dark:hover:bg-gray-700">
						<svg class="flex-shrink-0 w-6 h-6 text-gray-500 transition duration-75 dark:text-gray-400 group-hover:text-gray-900 dark:group-hover:text-white"
							fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg">
							<path fill-rule="evenodd"
								d="M5 4a3 3 0 00-3 3v6a3 3 0 003 3h10a3 3 0 003-3V7a3 3 0 00-3-3H5zm-1 9v-1h5v2H5a1 1 0 01-1-1zm7 1h4a1 1 0 001-1v-1h-5v2zm0-4h5V8h-5v2zM9 8H4v2h5V8z"
								clip-rule="evenodd"></path>
						</svg>
						<span class="flex-1 ml-3 whitespace-nowrap">Sign Up</span>
					</a>
				</li>
			</ul>
		</div>
	</aside>

	<p class="mt-5">This sidebar component is part of a larger, open-source library of Tailwind CSS components. Learn
		more
		by going to the official <a class="text-blue-600 hover:underline"
			href="https://flowbite.com/docs/getting-started/introduction/" target="_blank">Flowbite Documentation</a>.
	</p>
</div>

How to create a Tailwind CSS 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 Tailwind CSS Sidebar component

  • max-w-2xl
  • mx-auto
  • w-64
  • px-3
  • py-4
  • overflow-y-auto
  • bg-gray-50
  • dark:bg-gray-800
  • flex
  • p-2
  • text-base
  • text-gray-900
  • dark:text-white
  • hover:bg-gray-100
  • w-6
  • h-6
  • text-gray-500
  • dark:text-gray-400
  • group-hover:text-gray-900
  • ml-3
  • flex-shrink-0
  • flex-1
  • inline-flex
  • px-2
  • text-sm
  • text-gray-800
  • bg-gray-200
  • dark:bg-gray-700
  • dark:text-gray-300
  • w-3
  • h-3
  • p-3
  • text-blue-600
  • bg-blue-200
  • dark:bg-blue-900
  • dark:text-blue-200
  • mt-5

37 steps to create a Tailwind CSS Sidebar component with Tailwind CSS

  1. Set the maximum width/height of an element using the max-w-2xl utilities.

  2. Control the horizontal margin of an element to auto using the mx-auto utilities.

  3. Use w-64 to set an element to a fixed width(16rem).

  4. Control the horizontal padding of an element to 0.75rem using the px-3 utilities.

  5. Control the vertical padding of an element to 1rem using the py-4 utilities.

  6. Use overflow-y-auto to allow vertical scrolling if needed.

  7. Control the background color of an element to gray-50 using the bg-gray-50 utilities.

  8. Control the background color of an element to gray-800 using the dark:bg-gray-800 utilities in dark theme.

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

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

  11. Control the text color of an element to base using the text-base utilities.

  12. Control the text color of an element to gray-900 using the text-gray-900 utilities.

  13. Control the text color of an element to white in dark theme using the dark:text-white utilities.

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

  15. Use w-6 to set an element to a fixed width(1.5rem).

  16. Use h-6 to set an element to a fixed height(1.5rem).

  17. Control the text color of an element to gray-500 using the text-gray-500 utilities.

  18. Control the text color of an element to gray-400 in dark theme using the dark:text-gray-400 utilities.

  19. Control the text color of an element to gray-900undefined using the group-hover:text-gray-900 utilities.

  20. Control the margin on left side of an element to 0.75rem using the ml-3 utilities.

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

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

  23. Use inline-flex to create an inline flex container that flows with text.

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

  25. Control the text color of an element to sm using the text-sm utilities.

  26. Control the text color of an element to gray-800 using the text-gray-800 utilities.

  27. Control the background color of an element to gray-200 using the bg-gray-200 utilities.

  28. Control the background color of an element to gray-700 using the dark:bg-gray-700 utilities in dark theme.

  29. Control the text color of an element to gray-300 in dark theme using the dark:text-gray-300 utilities.

  30. Use w-3 to set an element to a fixed width(0.75rem).

  31. Use h-3 to set an element to a fixed height(0.75rem).

  32. Control the padding on all sides of an element to 0.75rem using the p-3 utilities.

  33. Control the text color of an element to blue-600 using the text-blue-600 utilities.

  34. Control the background color of an element to blue-200 using the bg-blue-200 utilities.

  35. Control the background color of an element to blue-900 using the dark:bg-blue-900 utilities in dark theme.

  36. Control the text color of an element to blue-200 in dark theme using the dark:text-blue-200 utilities.

  37. Control the margin on top side of an element to 1.25rem using the mt-5 utilities.

Conclusion

The above is a step-by-step tutorial on how to use Tailwind CSS to create a Tailwind CSS Sidebar components, learn and follow along to implement your own components.