DEV Community

Cover image for How to Build a Dark Mode Admin Dashboard with Tailwind CSS
Hitesh Chauhan
Hitesh Chauhan

Posted on

How to Build a Dark Mode Admin Dashboard with Tailwind CSS

In today's world of web development, user experience is key. A major design trend that has emerged in recent years is dark mode. Not only does dark mode reduce eye strain, but it also adds a modern aesthetic to your project. If you're building an admin dashboard, implementing a dark mode using Tailwind CSS can significantly improve the user experience, especially for users who spend long hours interacting with your interface. In this blog, we'll explore how to build a sleek dark mode admin dashboard using Tailwind CSS and then highlight a few templates that will help you get started quickly.

Why Choose Dark Mode for Your Admin Dashboard?

Dark mode is known for several benefits:

  • Reduced Eye Strain: It reduces blue light exposure and is easier on the eyes during long working hours.
  • Battery Saving: On OLED screens, dark mode uses less power.
  • Aesthetic Appeal: It provides a modern, clean, and visually appealing look.
  • Focus: Dark mode can help users focus on the content more effectively, especially with a minimalistic UI.

Step-by-Step Guide to Building a Dark Mode Admin Dashboard

Here’s a simple breakdown of how you can create a dark mode admin dashboard using Tailwind CSS.

1. Setting Up the Project

Before you start building your admin dashboard, make sure to have Tailwind CSS installed in your project. If you're working with a fresh Next.js project, follow these steps to set up Tailwind CSS:

  1. Install Tailwind CSS: Run the following commands to install Tailwind CSS:
   npm install -D tailwindcss postcss autoprefixer
   npx tailwindcss init
Enter fullscreen mode Exit fullscreen mode
  1. Configure Tailwind: In the tailwind.config.js file, make sure to enable JIT mode for faster builds:
   module.exports = {
     mode: 'jit',
     purge: ['./pages/**/*.{js,ts,jsx,tsx}', './components/**/*.{js,ts,jsx,tsx}'],
     darkMode: 'class', // Enable dark mode
     theme: {
       extend: {},
     },
     plugins: [],
   }
Enter fullscreen mode Exit fullscreen mode
  1. Include Tailwind in Your CSS: In your globals.css or the relevant CSS file, add the following to include Tailwind's base, components, and utilities:
   @tailwind base;
   @tailwind components;
   @tailwind utilities;
Enter fullscreen mode Exit fullscreen mode
2. Creating the Basic Layout

Next, you need to create the basic layout for your admin dashboard. Here’s a simple layout using Tailwind's utility classes:

<div class="min-h-screen bg-gray-900 text-white">
  <!-- Sidebar -->
  <div class="w-64 bg-gray-800 p-6">
    <h1 class="text-2xl font-semibold">Admin Dashboard</h1>
    <ul class="mt-8">
      <li><a href="#" class="text-gray-400 hover:text-white">Dashboard</a></li>
      <li><a href="#" class="text-gray-400 hover:text-white">Users</a></li>
      <li><a href="#" class="text-gray-400 hover:text-white">Settings</a></li>
    </ul>
  </div>

  <!-- Main Content -->
  <div class="flex-1 p-8">
    <h2 class="text-3xl font-semibold">Welcome to the Dashboard</h2>
    <div class="mt-4">
      <div class="grid grid-cols-3 gap-6">
        <div class="bg-gray-800 p-6 rounded-lg shadow-md">
          <h3 class="text-xl font-semibold">Total Users</h3>
          <p class="text-3xl font-bold">1,500</p>
        </div>
        <div class="bg-gray-800 p-6 rounded-lg shadow-md">
          <h3 class="text-xl font-semibold">Revenue</h3>
          <p class="text-3xl font-bold">$25,000</p>
        </div>
        <div class="bg-gray-800 p-6 rounded-lg shadow-md">
          <h3 class="text-xl font-semibold">Pending Orders</h3>
          <p class="text-3xl font-bold">43</p>
        </div>
      </div>
    </div>
  </div>
</div>
Enter fullscreen mode Exit fullscreen mode

In this code, we're using Tailwind CSS classes to create a dark background (bg-gray-900), light text (text-white), and other components like the sidebar and content areas.

3. Enabling Dark Mode Toggle

To give users the ability to toggle between dark and light modes, you can add a button to switch between themes. This can be achieved with a simple JavaScript function:

<button onclick="document.documentElement.classList.toggle('dark')" class="px-4 py-2 bg-blue-500 text-white rounded-lg">Toggle Dark Mode</button>
Enter fullscreen mode Exit fullscreen mode

In your CSS, Tailwind automatically supports dark variants of all utilities when you use darkMode: 'class' in the config file. This allows the whole app to switch themes when the dark mode class is toggled.

4. Improving the UI with Tailwind’s Dark Mode Utilities

Tailwind CSS provides dark variant utilities to style elements differently in dark mode. For example, here’s how you can adjust the sidebar color in dark mode:

<div class="w-64 bg-gray-800 dark:bg-gray-900 p-6">
  <h1 class="text-2xl font-semibold">Admin Dashboard</h1>
  <ul class="mt-8">
    <li><a href="#" class="text-gray-400 hover:text-white">Dashboard</a></li>
    <li><a href="#" class="text-gray-400 hover:text-white">Users</a></li>
    <li><a href="#" class="text-gray-400 hover:text-white">Settings</a></li>
  </ul>
</div>
Enter fullscreen mode Exit fullscreen mode

In this code, the sidebar background color changes depending on whether the dark mode is enabled (dark:bg-gray-900).

5. Responsive Design

With Tailwind CSS, responsive design is easy to implement. You can use Tailwind’s responsive utilities to ensure your dashboard works on all screen sizes:

<div class="w-64 lg:w-96 bg-gray-800 p-6">
  <!-- Sidebar Content -->
</div>
Enter fullscreen mode Exit fullscreen mode

In this example, the sidebar width will be 64 on smaller screens and 96 on larger screens (lg breakpoint).


Recommended Admin Dashboard Templates

If you're looking for ready-made templates to speed up your project, here are a few premium Tailwind-based templates:

  1. Flexy admin dashboard template

    A dynamic admin dashboard crafted with Next.js, React, and Ant Design, designed to meet the needs of modern web applications.

  2. Material Pro admin dashboard template

    A powerful admin dashboard developed with Next.js, React, and Ant Design, tailored for modern web applications.

  3. Adkit - Next.js Admin Template

    A versatile admin dashboard built with Next.js, React, and Ant Design, perfect for modern web applications.

  4. Linear Admin - Multi-purpose Admin Template

    A clean and minimal admin template with multiple layouts, ideal for various types of projects.

  5. Eclipse - Bootstrap 5 Dark Admin Templates Bundle

    A bundle of dark-themed admin templates built with Bootstrap 5, ideal for building professional dashboards.

These templates are paid but offer extensive features, customization options, and are perfect if you're looking to save time.


Conclusion

Building a dark mode admin dashboard with Tailwind CSS is a straightforward and rewarding process. With Tailwind’s utility-first classes and dark mode support, you can quickly build a sleek and modern dashboard. If you're looking for a head start, the templates listed above will provide you with a solid foundation to create a professional dashboard with minimal effort. Happy coding!

Top comments (0)