vasil_k_k

Dark/Light Theme Toggle

Feb 26th, 2026 (edited)
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
CSS 1.64 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Dark/Light Theme Toggle</title>
  6. <style>
  7.   body {
  8.     /* Initial light theme colors  */
  9.     --bg: #f8fafc;          
  10.     --text: #0f172a;        
  11.     --btcolor: #6366f1;      
  12.  
  13.     background: var(--bg);
  14.     color: var(--text);
  15.     min-height: 100vh;
  16.     display: flex;
  17.     align-items: center;
  18.     justify-content: center;
  19.     flex-direction: column;
  20.     gap: 2rem;
  21.     transition: background 0.4s ease, color 0.4s ease;
  22.     font-family: system-ui, -apple-system, sans-serif;
  23.   }
  24.   /* This selector handles the dark theme colors */
  25.   body:has(#theme-toggle:checked) {
  26.     --bg: #0f172a;          
  27.     --text: #e2e8f0;        
  28.     --btcolor: #f59e0b;    
  29.   }
  30.   h1 {
  31.     font-size: 2.5rem;
  32.     letter-spacing: -1px;
  33.     transition: color 0.3s ease;
  34.   }
  35.   .toggle-btn {
  36.     padding: 0.75rem 1.5rem;
  37.     border-radius: 0.5rem;
  38.     background: var(--btcolor);
  39.     color: white;
  40.     font-weight: 600;
  41.     letter-spacing: 0.5px;
  42.     cursor: pointer;
  43.     transition: all 0.3s ease;
  44.     user-select: none;
  45.     box-shadow: 0 6px 20px rgba(0,0,0,0.15);
  46.    
  47.     &:hover {
  48.       transform: translateY(-2px);
  49.       box-shadow: 0 10px 28px rgba(0,0,0,0.2);
  50.     }
  51.   }
  52. </style>
  53. </head>
  54. <body>
  55.   <input style="display: none;" type="checkbox" id="theme-toggle">
  56.   <label for="theme-toggle" class="toggle-btn">
  57.     Toggle Theme
  58.   </label>
  59.   <h1>Hello World</h1>
  60. </body>
  61. <script>
  62.   // Sync checkbox with system theme on load
  63.   window.matchMedia('(prefers-color-scheme: dark)').matches &&
  64.     (document.getElementById('theme-toggle').checked = true);
  65. </script>
  66. </html>
Advertisement
Add Comment
Please, Sign In to add comment