Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Dark/Light Theme Toggle</title>
- <style>
- body {
- /* Initial light theme colors */
- --bg: #f8fafc;
- --text: #0f172a;
- --btcolor: #6366f1;
- background: var(--bg);
- color: var(--text);
- min-height: 100vh;
- display: flex;
- align-items: center;
- justify-content: center;
- flex-direction: column;
- gap: 2rem;
- transition: background 0.4s ease, color 0.4s ease;
- font-family: system-ui, -apple-system, sans-serif;
- }
- /* This selector handles the dark theme colors */
- body:has(#theme-toggle:checked) {
- --bg: #0f172a;
- --text: #e2e8f0;
- --btcolor: #f59e0b;
- }
- h1 {
- font-size: 2.5rem;
- letter-spacing: -1px;
- transition: color 0.3s ease;
- }
- .toggle-btn {
- padding: 0.75rem 1.5rem;
- border-radius: 0.5rem;
- background: var(--btcolor);
- color: white;
- font-weight: 600;
- letter-spacing: 0.5px;
- cursor: pointer;
- transition: all 0.3s ease;
- user-select: none;
- box-shadow: 0 6px 20px rgba(0,0,0,0.15);
- &:hover {
- transform: translateY(-2px);
- box-shadow: 0 10px 28px rgba(0,0,0,0.2);
- }
- }
- </style>
- </head>
- <body>
- <input style="display: none;" type="checkbox" id="theme-toggle">
- <label for="theme-toggle" class="toggle-btn">
- Toggle Theme
- </label>
- <h1>Hello World</h1>
- </body>
- <script>
- // Sync checkbox with system theme on load
- window.matchMedia('(prefers-color-scheme: dark)').matches &&
- (document.getElementById('theme-toggle').checked = true);
- </script>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment