|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="UTF-8"> |
| 5 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 6 | + <title>Dark/Light Theme Toggle</title> |
| 7 | + <style> |
| 8 | + /* Default Light Theme */ |
| 9 | + :root { |
| 10 | + --background-color: #ffffff; |
| 11 | + --text-color: #000000; |
| 12 | + } |
| 13 | + |
| 14 | + /* Dark Theme */ |
| 15 | + .dark-mode { |
| 16 | + --background-color: #121212; |
| 17 | + --text-color: #ffffff; |
| 18 | + } |
| 19 | + |
| 20 | + body { |
| 21 | + background-color: var(--background-color); |
| 22 | + color: var(--text-color); |
| 23 | + font-family: Arial, sans-serif; |
| 24 | + transition: background-color 0.3s, color 0.3s; |
| 25 | + } |
| 26 | + |
| 27 | + .theme-toggle-btn { |
| 28 | + position: absolute; |
| 29 | + top: 20px; |
| 30 | + right: 20px; |
| 31 | + padding: 10px 20px; |
| 32 | + cursor: pointer; |
| 33 | + background-color: var(--text-color); |
| 34 | + color: var(--background-color); |
| 35 | + border: none; |
| 36 | + border-radius: 5px; |
| 37 | + transition: background-color 0.3s, color 0.3s; |
| 38 | + } |
| 39 | + </style> |
| 40 | +</head> |
| 41 | +<body> |
| 42 | + |
| 43 | + <h1>Dark/Light Theme Toggle</h1> |
| 44 | + <p>This is an example of switching between dark and light modes using a toggle button.</p> |
| 45 | + <button class="theme-toggle-btn" id="theme-toggle">Toggle Theme</button> |
| 46 | + |
| 47 | + <script> |
| 48 | + const toggleButton = document.getElementById('theme-toggle'); |
| 49 | + const body = document.body; |
| 50 | + |
| 51 | + toggleButton.addEventListener('click', () => { |
| 52 | + body.classList.toggle('dark-mode'); |
| 53 | + updateButtonText(); |
| 54 | + }); |
| 55 | + |
| 56 | + function updateButtonText() { |
| 57 | + if (body.classList.contains('dark-mode')) { |
| 58 | + toggleButton.textContent = 'Switch to Light Mode'; |
| 59 | + } else { |
| 60 | + toggleButton.textContent = 'Switch to Dark Mode'; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + // Initialize button text |
| 65 | + updateButtonText(); |
| 66 | + </script> |
| 67 | + |
| 68 | +</body> |
| 69 | +</html> |
0 commit comments