Web design is constantly evolving, with special attention to interactive details that captivate users. A LED effect can give your website a modern and innovative look by playing with light and user interaction. This tutorial will guide you through creating a LED lighting effect using HTML, CSS, and JavaScript. This effect creates an LED light that follows the mouse movement and reveals hidden text when it passes over it. We will explore each step of the process in detail, explaining how the code works so you can integrate it into your own projects.
The LED effect is not only aesthetic; it also adds a layer of interactivity that can improve user engagement. When a user interacts with elements that respond visually, it creates a sense of satisfaction and can encourage them to explore the site further. Moreover, this effect is perfect for design websites, portfolios, or any platform that wants to stand out visually.
To begin, we need to structure our project with HTML. Here is the basic code that we will use to create the LED effect.
LED Light
Digital marketing agency
Mondesign Web
container and a title h1 with the ID title.the <div id=" »light »"> : is the element that will represent the LED light.
The next step is to style the HTML elements in order to create the desired visual effect. The following CSS file defines the page layout as well as the appearance of the LED effect.
@import url('https://fonts.googleapis.com/css2?family=Playfair+Display:ital,wght@0,400..900;1,400..900&display=swap');
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Playfair Display', serif;
}
body {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
overflow: hidden;
background: rgb(14, 14, 14);
background: linear-gradient(270deg, rgba(14, 14, 14, 1) 0%, rgba(0, 96, 181, 1) 50%, rgba(14, 14, 14, 1) 100%);
}
#title {
position: relative;
color: #FFFBFB;
font-weight: 700;
font-size: 10em;
cursor: default;
opacity: 0;
transition: opacity 0.1s ease;
}
#light {
position: absolute;
transform: translate(-50%, -50%);
width: 20px;
height: 300px;
background: #FFFBFB;
border-radius: 5px;
box-shadow: 0 0 15px #FFFBFB, 0 0 100px #FFFBFB, 0 0 200px #FFFBFB, 0 0 300px #0060B5;
}
.title{
position: relative;
color: white;
font-weight: 500;
font-size: 52px;
}
.container{
display: flex;
flex-direction: column;
align-items: center;
} * {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Playfair Display', serif;
}
Playfair Display to all elements. body {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
overflow: hidden;
background: rgb(14, 14, 14);
background: linear-gradient(270deg, rgba(14, 14, 14, 1) 0%, rgba(0, 96, 181, 1) 50%, rgba(14, 14, 14, 1) 100%);
}
#title {
position: relative;
color: #FFFBFB;
font-weight: 700;
font-size: 10em;
cursor: default;
opacity: 0;
transition: opacity 0.1s ease;
}
#light {
position: absolute;
transform: translate(-50%, -50%);
width: 20px;
height: 300px;
background: #FFFBFB;
border-radius: 5px;
box-shadow: 0 0 15px #FFFBFB, 0 0 100px #FFFBFB, 0 0 200px #FFFBFB, 0 0 300px #0060B5;
}
light Is a thin vertical rectangle.If you also want to explore other interesting effects for your website, check out our article on the floating text effect using HTML, CSS, and JavaScript. This tutorial guides you through a dynamic web design that could complement your skills in visual effects.
JavaScript is essential for making the LED effect interactive. Here is the script that allows the light to follow the mouse and reveal the text when it passes over it.
var text = document.getElementById('title');
var light = document.getElementById('light');
document.addEventListener('mousemove', function(event) {
var mouseX = event.clientX;
var mouseY = event.clientY;
light.style.left = mouseX + 'px';
light.style.top = mouseY + 'px';
var textRect = text.getBoundingClientRect();
var isWithinX = mouseX > textRect.left && mouseX < textRect.right;
var isWithinY = mouseY > textRect.top && mouseY < textRect.bottom;
if (isWithinX && isWithinY) {
text.style.opacity = 1;
} else {
text.style.opacity = 0;
}
}); Let’s analyze the following JavaScript code:
var text = document.getElementById('title');
var light = document.getElementById('light');
title, which corresponds to the title text on your page.lightwhich corresponds to the light element that you move around the page. document.addEventListener('mousemove', function(event) {
var mouseX = event.clientX;
var mouseY = event.clientY;
light.style.left = mouseX + 'px';
light.style.top = mouseY + 'px'; light.style.left and light.style.top : Positions the light element (#light) exactly where the mouse is, using the X and Y coordinates.
var textRect = text.getBoundingClientRect();
var textCenterX = textRect.left + textRect.width / 2;
var textCenterY = textRect.top + textRect.height / 2;
#title), That is to say the position of the text relative to the window.left) and adding half of its width/height. var isWithinX = mouseX > textRect.left && mouseX < textRect.right;
var isWithinY = mouseY > textRect.top && mouseY < textRect.bottom;
if (isWithinX && isWithinY) {
text.style.opacity = 1;
} else {
text.style.opacity = 0;
}
});
This script creates an interactive effect where the text (#title) becomes visible only when the light effect (#light) passes over it. When the user moves the mouse, the light follows the movement, and the text appears and disappears based on the position of this light, creating a dynamic reveal effect.
In this tutorial, we explored how to create an interactive LED lighting effect using HTML, CSS, and JavaScript. This type of effect is ideal for energizing a web interface by adding a modern and interactive touch. Whether for a portfolio, a marketing website, or any other project, this effect can be easily customized to suit your specific needs.
The LED lighting effect is not only visually appealing, but it can also enhance the user experience by making interactions with your site more engaging. By understanding the basics of its implementation, you can now experiment with and adapt this effect to your own projects to create even more captivating web experiences.
MonDesign Web, your digital marketing agency, dedicated to guiding you to the pinnacle of your online success.
We use cookies to improve your experience on our site. By using our site, you consent to cookies.
Websites store cookies to enhance functionality and personalise your experience. You can manage your preferences, but blocking some cookies may impact site performance and services.
Essential cookies enable basic functions and are necessary for the proper function of the website.
These cookies are needed for adding comments on this website.
Statistics cookies collect information anonymously. This information helps us understand how visitors use our website.
Google Analytics is a powerful tool that tracks and analyzes website traffic for informed marketing decisions.
Service URL: policies.google.com
Marketing cookies are used to follow visitors to websites. The intention is to show ads that are relevant and engaging to the individual user.
Facebook Pixel is a web analytics service that tracks and reports website traffic.
Service URL: www.facebook.com