Dark Mode Toggle

Theme switching styles using CSS light-dark() and the data-mode attribute. Supports automatic, light-only, and dark-only modes.

Overview

Vanilla Breeze uses the CSS light-dark() function for automatic theme switching. This snippet shows three approaches:

  1. Automatic - Follows OS/browser preference (default)
  2. Manual toggle - Switch via data-mode attribute
  3. Three-way - Light / System / Dark selector

Live Demo

Try the toggle button and three-way selector:

Base Setup

Enable light-dark support and define mode overrides:

Toggle Button Styles

Style a theme toggle button with icon swapping:

JavaScript

Minimal JavaScript for toggle functionality with localStorage persistence:

Three-Way Selector

For users who want explicit Light / System / Dark options:

How light-dark() Works

The CSS light-dark() function returns different values based on the computed color-scheme:

This is simpler than media queries because it automatically responds to both OS preferences AND manual color-scheme overrides.

Related

Tokens Starter

Color tokens with light-dark() setup

theme-picker Component

Built-in theme switching component

MDN: light-dark()

CSS light-dark() function reference