Dark Mode Toggle

A sun/moon tailwind dark mode toggle with a sliding knob and smooth color transition.

Tailwind CSS
<button class="relative w-14 h-8 rounded-full bg-slate-800 transition-colors duration-300" aria-label="Toggle dark mode">
  <span class="absolute top-1 left-1 w-6 h-6 rounded-full bg-amber-300 shadow flex items-center justify-center translate-x-6 transition-transform duration-300">
    <svg class="w-4 h-4 text-slate-900" fill="currentColor" viewBox="0 0 20 20">
      <path d="M17.293 13.293A8 8 0 016.707 2.707a8.001 8.001 0 1010.586 10.586z"/>
    </svg>
  </span>
</button>

Free to use in personal and commercial projects. No attribution required.

Variants

Same component, different looks. Preview each one live and copy the code you like.

Light state

Shows the toggle in light mode so users see both states before copying.

Tailwind CSS

Large

Bigger hit area for settings pages and mobile-first layouts.

Tailwind CSS

Minimal

No icon inside the knob for ultra clean interfaces.

Tailwind CSS

About this dark mode toggle

A tailwind dark mode toggle is one of the most copied UI patterns on the web, and for good reason: every site with a dark theme needs a visible, tappable way to switch it. Developers searching for a dark mode switch tailwind css usually want something that works with Tailwind's class-based dark mode and needs no JavaScript framework, just a button that flips the knob and swaps the icon.

This theme toggle button is a single button element with a rounded track and an absolutely positioned knob. The knob sits on the left in light mode and slides right with translate-x-6 in dark mode; the 300ms transition makes the slide feel physical. The moon icon inside the knob tells the user the current state at a glance, and aria-label keeps it accessible to screen readers.

Show the toggle in your header or settings menu, somewhere users expect theme controls. Keep the knob icon in sync with the actual theme: sun for light mode, moon for dark mode. Pair it with a tiny script that toggles the dark class on the html element and stores the choice in localStorage so the preference survives reloads.

Dark mode stopped being a novelty years ago; for many users it is the default way they browse. Developers, night readers, and anyone with sensitive eyes expect every serious site to offer it. A visible toggle is about control: auto-detect guesses wrong on shared devices, and users should not have to dig through settings to fix the guess. No toggle means some visitors simply leave.

The control is a single button containing a rounded track and an absolutely positioned knob. In dark mode the knob slides right with translate-x-6; the 300ms transition makes the movement feel physical rather than instant. Because it is one real button element, it is natively keyboard-focusable with no extra work. A tiny script toggles the dark class on the html element and stores the choice in localStorage.

On first visit with no saved preference, follow the operating system's color scheme instead of defaulting to light; overriding the OS choice is the fastest way to annoy. Transition the theme change over 300ms on background and text colors so the flip feels intentional, not glitchy. Persist the choice in localStorage, but re-check the OS preference if the user clears site data; a stale default is worse than no default. If your emails or embedded widgets can't follow the theme, scope dark mode to your chrome and keep content surfaces light.

The most visible failure is the theme flash: the page paints light for a split second before JavaScript applies the saved dark choice. It happens because the class gets added after styles load, and on every visit it tells dark-mode users the site forgot them. Fix it with a tiny synchronous script in the document head that reads localStorage and sets the class before first paint. Deferred scripts and framework effects run too late.

Frequently asked questions

How do I make a tailwind dark mode toggle work with class-based dark mode?

Set darkMode: 'class' in your Tailwind config, then add a click handler that toggles the 'dark' class on document.documentElement. The toggle button itself just needs to slide its knob to match the current state.

How do I remember the user's theme choice?

Store it in localStorage when the toggle is clicked, and read it on page load before rendering. This avoids a flash of the wrong theme. Many sites also fall back to prefers-color-scheme when no stored choice exists.

Should the icon show the current theme or the theme it will switch to?

Show the current theme. A moon icon while in dark mode confirms where you are, which matches what most major sites do. Keep the aria-label accurate, for example 'Switch to light mode' when dark is active.

How do I stop the page flashing light mode before dark mode loads?

Add a tiny inline script in the document head that reads the saved theme from localStorage and adds the dark class to the document element before the CSS paints. The flash happens because the class is applied after styles load. This script must run synchronously in the head, not deferred, to beat the first paint.

How do I set up dark mode in Tailwind CSS v4?

Tailwind v4 drops the darkMode config option in favor of a CSS-first approach. Add @custom-variant dark (&:where(.dark, .dark *)); to your stylesheet, then toggle the dark class on the html element exactly as before. All your existing dark: utilities keep working once the variant is registered.

Should dark mode default to the user's system preference?

Yes. When there is no saved choice, check window.matchMedia("(prefers-color-scheme: dark)").matches and apply that theme. A saved manual choice should always override the system setting on later visits. This gives new visitors the right theme instantly while respecting deliberate changes.

Where to use it

  • ✓ Site headers with theme switching
  • ✓ Settings and profile pages
  • ✓ Documentation sites
  • ✓ Dashboards with dark mode

Design notes

When to use

A dark mode toggle belongs in the header of any site that offers a dark theme, visible on every page so visitors can switch without hunting through settings. The sun and moon icons are universally understood, and the sliding knob gives satisfying feedback. It matters most for content sites people read at night.

What to avoid

Do not bury the toggle in a settings submenu, because a theme switch people cannot find might as well not exist. Avoid flashing the wrong theme on page load before JavaScript applies the saved choice, since that flash is jarring. And do not ignore the operating system preference on first visit, because most users expect the site to match their device.

Accessibility

Make it a real button with an aria-pressed state or a label like Switch to dark mode, and keep it fully keyboard operable. Announce the theme change so screen reader users know the switch worked.

Customization tips

Swap bg-slate-800 for your track color in each state, and change the knob icon to sun for light mode. To animate the icon swap, crossfade two SVGs with opacity transitions. Match the 300ms duration to your other theme transitions so the whole page shifts together.

Customize it with our tools