<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Light Bulb Switch</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <div class="container">
        <div class="switch" id="switch">
            <div class="toggle"></div>
        </div>
        <div class="bulb" id="bulb"></div>
    </div>
    <script src="script.js"></script>
</body>
</html>
body {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    margin: 0;
    background-color: #f0f0f0;
    font-family: Arial, sans-serif;
}

.container {
    text-align: center;
}

.switch {
    width: 60px;
    height: 30px;
    background-color: #ccc;
    border-radius: 15px;
    position: relative;
    cursor: pointer;
    margin: 20px auto;
}

.toggle {
    width: 28px;
    height: 28px;
    background-color: white;
    border-radius: 50%;
    position: absolute;
    top: 1px;
    left: 1px;
    transition: all 0.3s ease;
}

.bulb {
    width: 50px;
    height: 80px;
    background-color: #555;
    border-radius: 50% 50% 45% 45%;
    margin: 20px auto;
    box-shadow: 0 0 20px 5px #ccc;
    transition: background-color 0.3s, box-shadow 0.3s;
}

.bulb.on {
    background-color: green;
    box-shadow: 0 0 20px 5px green;
}
const bulb = document.getElementById('bulb');
const switchButton = document.getElementById('switch');

switchButton.addEventListener('click', function() {
    const toggle = switchButton.querySelector('.toggle');
    
    // Toggle the switch position
    if (toggle.style.left === '1px') {
        toggle.style.left = '31px'; // Move toggle to right
        bulb.classList.add('on');   // Turn bulb on
    } else {
        toggle.style.left = '1px';  // Move toggle to left
        bulb.classList.remove('on'); // Turn bulb off
    }
});

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.