Skip to content
TheChatArchiveThe Chat Archive
Claudeclaude-opus-4-20250514-thinking-16k· AnthropicHugging Face LMArena Human Preference 140K

how can i easily implement a dark-light css theme toggle switch for use in html5?

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Geminigemini-2.5-pro’s answer.
  1. User

    how can i easily implement a dark-light css theme toggle switch for use in html5?

  2. Claude Assistant

    Here's a simple way to implement a dark-light theme toggle:

    HTML Structure

    <button id="theme-toggle" aria-label="Toggle theme">
      <span class="sun-icon">☀️</span>
      <span class="moon-icon">🌙</span>
    </button>
    

    CSS

    /* Define CSS variables for themes */
    :root {
      --bg-color: #ffffff;
      --text-color: #000000;
      --secondary-bg: #f0f0f0;
      --border-color: #ddd;
    }
    
    [data-theme="dark"] {
      --bg-color: #1a1a1a;
      --text-color: #ffffff;
      --secondary-bg: #2d2d2d;
      --border-color: #444;
    }
    
    /* Apply the variables */
    body {
      background-color: var(--bg-color);
      color: var(--text-color);
      transition: background-color 0.3s ease, color 0.3s ease;
    }
    
    /* Theme toggle button styling */
    #theme-toggle {
      position: fixed;
      top: 20px;
      right: 20px;
      background: var(--secondary-bg);
      border: 2px solid var(--border-color);
      border-radius: 50%;
      padding: 10px;
      cursor: pointer;
      transition: all 0.3s ease;
    }
    
    /* Show/hide icons based on theme */
    .sun-icon { display: none; }
    .moon-icon { display: inline; }
    
    [data-theme="dark"] .sun-icon { display: inline; }
    [data-theme="dark"] .moon-icon { display: none; }
    

    JavaScript

    const themeToggle = document.getElementById('theme-toggle');
    const html = document.documentElement;
    
    // Check for saved theme preference or default to 'light'
    const currentTheme = localStorage.getItem('theme') || 'light';
    html.setAttribute('data-theme', currentTheme);
    
    themeToggle.addEventListener('click', () => {
      const currentTheme = html.getAttribute('data-theme');
      const newTheme = currentTheme === 'light' ? 'dark' : 'light';
      
      html.setAttribute('data-theme', newTheme);
      localStorage.setItem('theme', newTheme);
    });
    

    Complete Working Example

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Theme Toggle Example</title>
      <style>
        :root {
          --bg-color: #ffffff;
          --text-color: #000000;
          --secondary-bg: #f0f0f0;
          --border-color: #ddd;
        }
    
        [data-theme="dark"] {
          --bg-color: #1a1a1a;
          --text-color: #ffffff;
          --secondary-bg: #2d2d2d;
          --border-color: #444;
        }
    
        * {
          margin: 0;
          padding: 0;
          box-sizing: border-box;
        }
    
        body {
          background-color: var(--bg-color);
          color: var(--text-color);
          font-family: Arial, sans-serif;
          padding: 40px;
          transition: background-color 0.3s ease, color 0.3s ease;
          min-height: 100vh;
        }
    
        #theme-toggle {
          position: fixed;
          top: 20px;
          right: 20px;
          background: var(--secondary-bg);
          border: 2px solid var(--border-color);
          border-radius: 50%;
          padding: 10px;
          cursor: pointer;
          font-size: 20px;
          transition: all 0.3s ease;
        }
    
        #theme-toggle:hover {
          transform: scale(1.1);
        }
    
        .sun-icon { display: none; }
        .moon-icon { display: inline; }
    
        [data-theme="dark"] .sun-icon { display: inline; }
        [data-theme="dark"] .moon-icon { display: none; }
    
        /* Example content styling */
        .container {
          max-width: 800px;
          margin: 0 auto;
        }
    
        .card {
          background: var(--secondary-bg);
          padding: 20px;
          border-radius: 8px;
          margin-bottom: 20px;
          border: 1px solid var(--border-color);
        }
      </style>
    </head>
    <body>
      <button id="theme-toggle" aria-label="Toggle theme">
        <span class="sun-icon">☀️</span>
        <span class="moon-icon">🌙</span>
      </button>
    
      <div class="container">
        <h1>Theme Toggle Demo</h1>
        <div class="card">
          <h2>Card Title</h2>
          <p>This is an example card that responds to theme changes.</p>
        </div>
      </div>
    
      <script>
        const themeToggle = document.getElementById('theme-toggle');
        const html = document.documentElement;
    
        // Check for saved theme preference or default to 'light'
        const currentTheme = localStorage.getItem('theme') || 'light';
        html.setAttribute('data-theme', currentTheme);
    
        themeToggle.addEventListener('click', () => {
          const currentTheme = html.getAttribute('data-theme');
          const newTheme = currentTheme === 'light' ? 'dark' : 'light';
          
          html.setAttribute('data-theme', newTheme);
          localStorage.setItem('theme', newTheme);
        });
      </script>
    </body>
    </html>
    

    Key Features:

    1. CSS Variables: Uses CSS custom properties to define colors that change based on the theme
    2. Data Attribute: Uses data-theme attribute on the HTML element to control the theme
    3. LocalStorage: Saves the user's preference so it persists across page reloads
    4. Smooth Transitions: CSS transitions make the theme change smooth
    5. Accessible: Includes proper ARIA labels for screen readers

    You can customize the colors, add more CSS variables for different elements, or style the toggle button differently (like using a sliding switch instead of a button). The principle remains the same: toggle a data attribute and use CSS to apply different styles based on that attribute.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.