how can i easily implement a dark-light css theme toggle switch for use in html5?
- User
how can i easily implement a dark-light css theme toggle switch for use in html5?
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:
- CSS Variables: Uses CSS custom properties to define colors that change based on the theme
- Data Attribute: Uses
data-themeattribute on the HTML element to control the theme - LocalStorage: Saves the user's preference so it persists across page reloads
- Smooth Transitions: CSS transitions make the theme change smooth
- 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.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.