Welcome to my website U-GINE MEDIA. In this website I teach tutorials and share source code on some programming (HTML, CSS & JavaScript) tutorials.
Before we get started, do well to subscribe to my channel to never miss out on any update that I post every single day.
In this tutorial topic "Simple Ceiling Light Bulb | CSS Light Effects", we will learn how to create it following these simple easy steps.
Share this to your programmer friends. If y'all learnt something today, make sure to let me in the comments.
If interested in watching the video before running the code below, you can click the play button to get started.
Code Begins
- First, create a folder with any name you like. Then, make the necessary files inside it.
- Create a file called index.html to serve as the main file.
- Create a file called style.css for the CSS code.
- Finally, Create a file called script.js for the JavaScript code.
To start, add the following HTML codes to your index.html file. These codes include essential HTML markup with different semantic tags, such as div, form, input, button, image, etc., to build the website layout.
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="index.css">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Light Bulb</title>
</head>
<body>
<div class="bg">
<div class="switch">
<div class="switch_btn"></div>
</div>
<div class="bulb">
<div class="string"><span></span></div>
</div>
</div>
</body>
</html>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
background: #222;
}
.bg {
position: relative;
width: 800px;
height: 550px;
display: flex;
justify-content: space-between;
align-items: center;
flex-direction: row-reverse;
}
.switch {
width: 100px;
height: 100px;
border: 3px solid #000;
background-image: linear-gradient(#eee, #ccc, #eee);
border-radius: 10px;
display: flex;
justify-content: center;
align-items: center;
margin-top: -150px;
}
.switch_btn {
position: relative;
width: 30px;
height: 45px;
background: linear-gradient(#777, #fff, #777);
border-radius: 6px;
border: 2px solid #000;
cursor: pointer;
}
.switch_btn::before {
content: "";
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 85%;
background: linear-gradient(#ddd, #fff);
border-radius: 6px;
}
.switch_btn.on::before {
top: 15%;
}
.bulb {
position: relative;
top: -100px;
width: 300px;
height: 300px;
background: #555;
border-radius: 50%;
}
.bulb::before {
content: "";
position: absolute;
top: 100px;
left: 50%;
transform: translateX(-50%) rotate(0deg) translateY(-70%);
width: 150px;
height: 200px;
background: #555;
border-top: 150px solid #000;
border-radius: 30px;
}
.bulb.on {
box-shadow: 0 0 120px #fff,
0 0 200px #fff,
0 0 300px #fff,
0 0 400px #fff,
0 0 500px #fff,
0 0 600px #fff,
0 0 700px #fff;
background: #ffff;
}
.bulb.on::before {
background: #fff;
}
.string {
position: absolute;
left: 50%;
width: 5px;
height: 400px;
background: #000;
transform: translateX(50%) translateY(-90%);
z-index: -1;
}
.string span {
position: absolute;
top: -50px;
left: 50%;
width: 200px;
height: 50px;
background: #000;
transform: translateX(-50%);
border-bottom-right-radius: 100px;
border-bottom-left-radius: 100px;
}
Finally, add the following JavaScript code to your script.js file to make your work functional. This code handles various functions, even listeners, input handling, etc.
<script>
const switches = document.querySelector(".switch");
const switchBtn = document.querySelector(".switch_btn");
//do this after building the bulb
const bulb = document.querySelector(".bulb");
switches.addEventListener("click", e => {
console.log("ADDED")
switchBtn.classList.toggle("on");
//do this after building the bulb
bulb.classList.toggle("on");
})
</script>
0 Comments
We are happy to hear from you.