• Home
  • /
  • Realistic Desk Lamp with CSS & JS | On/Off Toggle Switch

Realistic Desk Lamp with CSS & JS | On/Off Toggle Switch

HTML

				
					<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  <title>Realistic Lamp | Learn with Arshyan</title>
  <link rel="stylesheet" href="style.css" />
  <script defer src="script.js"></script>
</head>
<body>
  <div class="container">
    <h1>Desk Lamp</h1>

    <div class="lamp-container">
      <div class="lamp">
        <div class="lamp-head"></div>
        <div class="lamp-arm"></div>
        <div class="lamp-stand"></div>
        <div class="lamp-base"></div>
        <div class="lamp-light" id="lamp-light"></div>
      </div>
    </div>

    <div class="switch-wrapper">
      <label class="switch">
        <input type="checkbox" id="lamp-toggle" />
        <span class="slider"></span>
      </label>
      <span id="lamp-status">OFF</span>
    </div>
  </div>
</body>
</html>

				
			

CSS

				
					/* RESET & BASE */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
body {
  background-color: #1e1e2f;
  font-family: 'Segoe UI', sans-serif;
  color: #fff;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}
.container {
  text-align: center;
}
h1 {
  margin-bottom: 2rem;
  font-size: 2rem;
  font-weight: 600;
}

/* LAMP STRUCTURE */
.lamp-container {
  position: relative;
  width: 200px;
  height: 350px;
  margin: 0 auto 2rem;
}
.lamp {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  z-index: 2;
}

/* HEAD */
.lamp-head {
  width: 80px;
  height: 40px;
  background: #3b82f6;
  border-radius: 40% 40% 5px 5px;
  box-shadow: inset 0 -5px 10px rgba(0,0,0,0.2);
  z-index: 2;
}

/* ARM */
.lamp-arm {
  width: 6px;
  height: 80px;
  background: #4b5563;
  margin-top: 5px;
}

/* STAND */
.lamp-stand {
  width: 10px;
  height: 60px;
  background: #4b5563;
  margin-top: -10px;
  z-index: 1;
}

/* BASE */
.lamp-base {
  width: 100px;
  height: 20px;
  background: #374151;
  border-radius: 10px;
  box-shadow: 0 4px 8px rgba(0,0,0,0.3);
  margin-top: 10px;
}

.lamp-light {
  position: absolute;
  top: 40px; /* just under lamp head */
  left: 50%;
  transform: translateX(-50%);
  width: 120px;
  height: 160px;
  background: radial-gradient(
    ellipse at center,
    rgba(255, 255, 180, 0.35),
    rgba(255, 255, 150, 0.2) 50%,
    transparent 80%
  );
  opacity: 0;
  transition: opacity 0.4s ease;
  pointer-events: none;
  z-index: 0;
  filter: blur(2px);
  clip-path: polygon(45% 0%, 55% 0%, 100% 100%, 0% 100%);
}


/* SWITCH */
.switch-wrapper {
  display: flex;
  margin-top: -120px; /* move up */
  align-items: center;
  justify-content: center;
  gap: 1rem;
}
#lamp-status {
  font-size: 1.2rem;
  font-weight: bold;
}

/* TOGGLE SWITCH STYLE */
.switch {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 34px;
}
.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}
.slider {
  position: absolute;
  cursor: pointer;
  top: 0; left: 0; right: 0; bottom: 0;
  background-color: #888;
  transition: 0.4s;
  border-radius: 34px;
}
.slider:before {
  position: absolute;
  content: "";
  height: 24px;
  width: 24px;
  left: 5px;
  bottom: 5px;
  background-color: white;
  transition: 0.4s;
  border-radius: 50%;
}
input:checked + .slider {
  background-color: #facc15;
}
input:checked + .slider:before {
  transform: translateX(26px);
}

				
			

JavaScript

				
					const toggle = document.getElementById("lamp-toggle");
const glow = document.getElementById("lamp-light");
const status = document.getElementById("lamp-status");

toggle.addEventListener("change", () => {
  if (toggle.checked) {
    glow.style.opacity = "1";
    status.textContent = "ON";
  } else {
    glow.style.opacity = "0";
    status.textContent = "OFF";
  }
});

				
			

Share this post

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to Top