Highly Styled Toggle Switch - MeggiTools
Run
Toggle Theme
Share Link
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Highly Styled Toggle Switch</title> <style> body { display: flex; justify-content: center; align-items: center; height: 100vh; background-color: #f0f0f0; font-family: Arial, sans-serif; } .toggle-switch { position: relative; width: 80px; height: 40px; background-color: #ccc; border-radius: 20px; box-shadow: 0 0 15px rgba(0, 0, 0, 0.2); cursor: pointer; transition: background-color 0.3s ease-in-out; } .toggle-switch input { display: none; } .toggle-switch-label { position: absolute; width: 100%; height: 100%; border-radius: 20px; overflow: hidden; } .toggle-switch .toggle-switch-handle { position: absolute; top: 3px; left: 3px; width: 34px; height: 34px; background-color: #fff; border-radius: 50%; box-shadow: 0 0 15px rgba(0, 0, 0, 0.2); transition: left 0.3s ease-in-out, transform 0.3s ease-in-out; display: flex; justify-content: center; align-items: center; font-size: 20px; } .toggle-switch input:checked + .toggle-switch-label .toggle-switch-handle { left: 43px; transform: rotate(360deg); background-color: #4caf50; color: #fff; } .toggle-switch input:checked + .toggle-switch-label { background-color: #4caf50; } .toggle-switch .toggle-switch-handle::before { content: "🌙"; } .toggle-switch input:checked + .toggle-switch-label .toggle-switch-handle::before { content: "☀️"; } </style> </head> <body> <div class="toggle-switch"> <input type="checkbox" id="toggle" /> <label class="toggle-switch-label" for="toggle"> <span class="toggle-switch-handle"></span> </label> </div> <script> const toggleSwitch = document.querySelector('.toggle-switch'); toggleSwitch.addEventListener('click', () => { document.body.style.backgroundColor = toggleSwitch.querySelector('input').checked ? '#fff' : '#333'; }); </script> </body> </html>