Theme Toggle

Built-in dark mode with automatic theme switching and localStorage persistence

Quick Start

Try it now! The theme toggle button is already active on this page. Look for the floating button in the bottom-right corner and try switching themes!

1Include Required Files

Add the theme CSS and JavaScript to your page:

Include Theme Files • HTML

    
    
    


    

    
    
    

2That's It!

The theme toggle will automatically:

Zero Configuration Required

The theme toggle works out of the box with sensible defaults. No additional setup needed!

Features

Automatic Dark Mode

Complete dark mode support for all RSL components including navbar, buttons, forms, modals, cards, and more.

localStorage Persistence

User theme preference is saved automatically and restored on subsequent visits.

System Preference Detection

Respects user's operating system color scheme preference on first visit.

Live Theme Updates

Automatically detects when user changes system theme and updates accordingly (if no manual preference set).

Glassmorphism Effects

Beautiful glassmorphic cards and components in dark mode with backdrop blur.

Accessible

WCAG compliant color contrast ratios in both light and dark modes.

How It Works

Theme Detection

When a user first visits your site, the theme toggle:

  1. Checks localStorage for a saved theme preference
  2. If no preference exists, detects the system color scheme using prefers-color-scheme media query
  3. Applies the appropriate theme and saves it to localStorage

Theme Application

The theme is applied by setting a data-theme attribute on the <html> element:

Theme Application • JavaScript
// Light mode
document.documentElement.setAttribute('data-theme', 'light');

// Dark mode
document.documentElement.setAttribute('data-theme', 'dark');

All dark mode styles are then applied using CSS attribute selectors:

Dark Mode CSS • CSS
[data-theme="dark"] body {
    background: var(--rsl-dark-bg-primary);
    color: var(--rsl-dark-text-primary);
}

[data-theme="dark"] .card {
    background: rgba(255, 255, 255, 0.05);
    border-color: rgba(255, 255, 255, 0.1);
}

CSS Variables Reference

RSL uses CSS custom properties for theming. All components automatically respond to theme changes:

Color Variables

Variable Light Mode Dark Mode Purpose
--rsl-bg-primary #ffffff --rsl-dark-bg-primary Main background color
--rsl-bg-secondary #f8f9fa --rsl-dark-bg-secondary Secondary background
--rsl-text-primary #212529 --rsl-dark-text-primary Primary text color
--rsl-text-secondary #6c757d --rsl-dark-text-secondary Secondary text color
--rsl-border #e0e0e0 --rsl-dark-border Border color
--rsl-primary #6E7BFF #6E7BFF Brand primary color

Dark Mode Specific Variables

Variable Value Purpose
--rsl-dark-bg-primary #1a1a2e Dark mode main background
--rsl-dark-bg-secondary #16213e Dark mode secondary background
--rsl-dark-bg-tertiary #0f3460 Dark mode tertiary background
--rsl-dark-text-primary #ffffff Dark mode primary text
--rsl-dark-text-secondary rgba(255, 255, 255, 0.8) Dark mode secondary text
--rsl-dark-border rgba(255, 255, 255, 0.1) Dark mode borders

Customization

Manually Toggle Theme

You can programmatically toggle the theme in your own JavaScript:

Manual Theme Toggle • JavaScript
// Get current theme
const currentTheme = document.documentElement.getAttribute('data-theme');

// Toggle theme
const newTheme = currentTheme === 'dark' ? 'light' : 'dark';
document.documentElement.setAttribute('data-theme', newTheme);

// Save to localStorage
localStorage.setItem('rsl-theme', newTheme);

Customize Toggle Button Position

Override the CSS to change the button position:

Custom Position • CSS
/* Move to top-right corner */
.rsl-theme-toggle {
    top: 2rem;
    right: 2rem;
    bottom: auto;
}

/* Move to left side */
.rsl-theme-toggle {
    left: 2rem;
    right: auto;
}

Add Dark Mode to Custom Components

Use CSS variables and dark mode selectors for your custom components:

Custom Component Dark Mode • CSS
/* Use CSS variables for automatic theme support */
.my-custom-card {
    background: var(--rsl-bg-primary);
    color: var(--rsl-text-primary);
    border: 1px solid var(--rsl-border);
}

/* Or use explicit dark mode rules */
[data-theme="dark"] .my-custom-card {
    background: rgba(255, 255, 255, 0.05);
    color: var(--rsl-dark-text-primary);
    border-color: var(--rsl-dark-border);
}

Component Compatibility

All RSL components fully support dark mode out of the box:

  • Navbar
  • Buttons
  • Forms
  • Cards
  • Modals
  • Alerts
  • Toasts
  • Accordion
  • Tabs
  • Pagination
  • Breadcrumbs
  • Carousel
  • Off-Canvas
  • Popover
  • Tables

Accessibility

WCAG Compliant

All color combinations in both light and dark modes meet WCAG 2.1 Level AA contrast requirements.

Keyboard Navigation

The theme toggle button is fully keyboard accessible:

Screen Reader Support

The button includes proper ARIA labels for screen reader users:

Learn More

View Examples Try Playground