Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Building a Light/Dark Theme Switcher with Tailwind CSS

Tech 2

Tailwind CSS ships with utilities that make it trivial to attach alternate color schemes to componants. By pairing the darkMode configuration with a small piece of JavaScript, you can ship a theme toggle without extra CSS overhead.

Setup

Install Tailwind alongside PostCSS and Autoprefixer:

npm install -D tailwindcss postcss autoprefixer

Initialise the configuration files:

npx tailwindcss init -p

Open tailwind.config.js and set the dark mode strategy to class. This tells Tailwind to apply dark variants whenever the dark class appears on an ancestor element.

// tailwind.config.js
/** @type {import('tailwindcss').Config} */
module.exports = {
  content: ['./src/**/*.{html,js}'],
  darkMode: 'class',
  theme: {
    extend: {},
  },
  plugins: [],
}

Add the Tailwind directives to your main stylesheet:

/* src/style.css */
@tailwind base;
@tailwind components;
@tailwind utilities;

böjnings

Markup

Write a page that uses the dark: prefix to define alternate styles:

<body class="bg-gray-50 text-gray-900 dark:bg-gray-900 dark:text-gray-100">
  <header class="p-4 flex justify-between items-center">
    <h1 class="text-2xl font-bold">My App</h1>
    <button id="switcher" class="rounded bg-indigo-500 px-4 py-2 text-sm text-white dark:bg-yellow-400 dark:text-gray-900">
      Toggle theme
    </button>
  </header>
  <main class="p-4">
    <p>Body text adapts seamlessly.</p>
  </main>
  <script src="./theme.js"></script>
</body>

Toggle logic

Create a theme.js file that reads the saved preference from localStorage and applies the dark class accordingly. The button listener flips the980 class and persists the choice.

// src/theme.js
(function () {
  const STORAGE_KEY = 'color-scheme';
  const DARK_CLASS = 'dark';

  const htmlElement = document.documentElement;
  const saved = localStorage.getItem(STORAGE_KEY);

  // Apply saved preference or respect OS setting
  if (saved === 'dark' || (!saved && window.matchMedia('(prefers-color-scheme: dark)').matches)) {
    htmlElement.classList.add(DARK_CLASS);
  } else {
    htmlElement.classList.remove(DARK_CLASS);
  }

  // Wire up the toggle
  const btn = document.getElementById('switcher');
  if (btn) {
    btn.addEventListener('click', () => {
      const isDark = htmlElement.classList.toggle(DARK_CLASS);
      localStorage.setItem(STORAGE_KEY, isDark ? 'dark' : 'light');
    });
  }
})();

Note: If you prefer data attributes over classes, you can adapt the selector according. Tailwind’s darkMode also supports a media strategy for automatic OS-driven switching, but the class‑based approach gives explicit user control.

The combination of utility‑first styling and a minimal script delivers a performant theme toggle that respects user preference across visits.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.