JSFiddle - React, Tailwind, and code Playground
HTML
<div id="light"><div id="button"></div></div>
CSS
padding: 10px;
#light {
position: relative;
width: 40px;
margin: auto;
background-color: black;
z-index: 10;
margin-top: 420px;
height: 20px;
border-radius: 40px;
}
#button {
width: 16px;
background-color: cornflowerblue;
height: 16px;
border-radius: 40px;
position: relative;
top: 2px;
left: 4px;
cursor: pointer;
transition: 0.5s;
}
JavaScript
var button = document.getElementById("button");
button.onclick = function() {
if (window.getComputedStyle(button, null).getPropertyValue('left') === "4px") {
button.style.left = "20px";
} else {
button.style.left = "4px";
}
}
/*
var stil = window.getComputedStyle(button).getPropertyValue("left");
button.onclick = function() {
if (stil === "4px") {
stil = "20px";
} else {
stil = "4px";
}
}
*/