Guest User

svg color change example

a guest
Mar 27th, 2025
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 2.27 KB | Source Code | 0 0
  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>SVG Theme Toggle</title>
  7.   <style>
  8.     body {
  9.       margin: 2rem;
  10.       font-family: sans-serif;
  11.       transition: background-color 0.3s, color 0.3s;
  12.     }
  13.  
  14.     .theme-light {
  15.       background-color: white;
  16.       color: black;
  17.     }
  18.  
  19.     .theme-dark {
  20.       background-color: #121212;
  21.       color: white;
  22.     }
  23.  
  24.     .icon {
  25.       width: 50px;
  26.       height: 25px;
  27.       color: inherit;
  28.     }
  29.  
  30.     #theme-toggle {
  31.       display: none;
  32.     }
  33.  
  34.     .toggle-label {
  35.       display: inline-block;
  36.       cursor: pointer;
  37.       padding: 0.5rem 1rem;
  38.       background-color: #ccc;
  39.       border-radius: 0.5rem;
  40.       user-select: none;
  41.       margin-bottom: 1rem;
  42.     }
  43.  
  44.     #theme-toggle:checked + .theme-wrapper {
  45.       --theme: dark;
  46.     }
  47.  
  48.     .theme-wrapper {
  49.       --theme: light;
  50.     }
  51.  
  52.     .theme-wrapper {
  53.       all: unset;
  54.       display: block;
  55.     }
  56.  
  57.     .theme-wrapper[style*="--theme: dark"] {
  58.       background-color: #121212;
  59.       color: white;
  60.     }
  61.  
  62.     .theme-wrapper[style*="--theme: light"] {
  63.       background-color: white;
  64.       color: black;
  65.     }
  66.  
  67.     .theme-wrapper[style*="--theme: dark"] .icon {
  68.       color: white;
  69.     }
  70.  
  71.     .theme-wrapper[style*="--theme: light"] .icon {
  72.       color: black;
  73.     }
  74.   </style>
  75. </head>
  76. <body>
  77.   <input type="checkbox" id="theme-toggle" />
  78.  
  79.   <label for="theme-toggle" class="toggle-label">Toggle Theme</label>
  80.  
  81.   <div class="theme-wrapper" id="theme-container" style="--theme: light">
  82.     <h1>SVG Fill Color with Manual Toggle</h1>
  83.  
  84.     <div class="icon">
  85.       <svg width="50" height="25" viewBox="0 0 50 25" xmlns="http://www.w3.org/2000/svg" fill="currentColor">
  86.         <path d="M0,0 L50,0 L25,25 Z" />
  87.       </svg>
  88.     </div>
  89.  
  90.     <p>Click the toggle button above to switch between light and dark themes.</p>
  91.   </div>
  92.  
  93.   <script>
  94.     const toggle = document.getElementById('theme-toggle');
  95.     const container = document.getElementById('theme-container');
  96.  
  97.     toggle.addEventListener('change', () => {
  98.       container.style.setProperty('--theme', toggle.checked ? 'dark' : 'light');
  99.     });
  100.   </script>
  101. </body>
  102. </html>
  103.  
Tags: html. css
Advertisement
Add Comment
Please, Sign In to add comment