Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • jQuery
  • AngularJS
  • ReactJS
  • Next.js
  • React Native
  • NodeJS
  • Express.js
  • MongoDB
  • MERN Stack
  • PHP
  • WordPress
  • Bootstrap
  • Tailwind
  • CSS Frameworks
  • JS Frameworks
  • Web Development
Open In App
Next Article:
How to create Glowing Star effect using HTML and CSS?
Next article icon

How to create a Spotlight Effect using HTML and CSS ?

Last Updated : 07 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we are going to create a spotlight effect over the image when we hover over it. This is mainly based on HTML, CSS and JavaScript. The below steps have to be followed to create this effect.

HTML Section: In this section, we will create a container elements for the background image and the mouse pointer. The CSS and JavaScript files are also linked here.

HTML
<!DOCTYPE html> <html>  <head>     <link rel="stylesheet" href="style.css">     <script src="index.js"></script> </head>  <body>     <h1>         Hover mouse over the image          to get spotlight effect     </h1>          <div class="main_box">         <div class="img"></div>         <div class="mouse"></div>     </div> </body>  </html> 

CSS Section: In this section, CSS is used to give different types of animations and effects to our HTML page so that it looks interactive to the users. The browser effects are first reset, then the position and size of the image and mouse pointer are set. The filter property is used to give visual effects to the element. The clip-path property is used to convert the element into different kind of shapes.

CSS
/* Resetting the browser stylesheet */ * {     padding: 0;     margin: 0;     box-sizing: border-box;     overflow: hidden;     background-color: #000;     color: #fff; }  /* Styling the heading */ h1 {     display: flex;     align-items: center;     align-content: center;     justify-content: center; }  /* Position the mouse pointer and     the background image */ .main_box, .img, .mouse {     position: absolute;     top: 0;     left: 0;     width: 100%;     height: 100%; }  .main_box {     cursor: none;     margin-top: 3em; }  .img, .mouse {     background-image: url( 'https://media.geeksforgeeks.org/wp-content/cdn-uploads/20190417124305/250.png');     background-size: cover;     background-repeat: no-repeat;     background-position: center; }  /* Reduce the brightness of the image */ .img {     filter: brightness(10%); }  /* Make a circle with the clip-path     property for the spotlight in the effect */ .mouse {     clip-path: circle(5em at 0, 0); } 

JavaScript Section: This section handles the interactive portion of the webpage. It detects the mouse movement over the image using the offsetX and offsetY properties for getting the X and Y coordinates. The clipPath property is then used to create a circle for the spotlight effect.

JavaScript
// Select the container box and the mouse placeholder let main = document.querySelector('.main_box'); let mouse = document.querySelector('.mouse');  // Add an event listener for detecting // the movement of the mouse main.addEventListener('mousemove',                        (e) => {    // Use a circle with a clipPath   // and the offsetX and offsetY property   mouse.style.clipPath =      `circle(10em at ${e.offsetX}px ${e.offsetY}px)`; }); 

Complete Code: It is the combination of above three sections of code.

HTML
<!DOCTYPE html> <html>  <head>     <style>         /* Resetting the browser stylesheet */         * {             padding: 0;             margin: 0;             box-sizing: border-box;             overflow: hidden;             background-color: #000;             color: #fff;         }          /* Styling the heading */         h1 {             display: flex;             align-items: center;             align-content: center;             justify-content: center;         }          /* Position the mouse pointer and             the background image */         .main_box,         .img,         .mouse {             position: absolute;             top: 0;             left: 0;             width: 100%;             height: 100%;         }                  .main_box {             cursor: none;             margin-top: 3em;         }                  .img,         .mouse {             background-image: url( 'https://media.geeksforgeeks.org/wp-content/cdn-uploads/20190417124305/250.png');             background-size: cover;             background-repeat: no-repeat;             background-position: center;         }          /* Reduce the brightness of the image */         .img {             filter: brightness(10%);         }          /* Make a circle with the clip-path property             for the spotlight in the effect */         .mouse {             clip-path: circle(5em at 0, 0);         }     </style> </head>  <body>     <h1>         Hover mouse over the image          to get spotlight effect     </h1>          <div class="main_box">         <div class="img"></div>         <div class="mouse"></div>     </div>      <script>          // Select the container box and the          // mouse placeholder         let main = document.querySelector('.main_box');         let mouse = document.querySelector('.mouse');          // Add an event listener for detecting         // the movement of the mouse         main.addEventListener('mousemove',             (e) => {                  // Use a circle with a clipPath                 // and the offsetX and offsetY property                 mouse.style.clipPath =         `circle(10em at ${e.offsetX}px ${e.offsetY}px)`;             });     </script> </body>  </html> 

Output:

https://media.geeksforgeeks.org/wp-content/uploads/20240802210538/Untitled112.mp4


Next Article
How to create Glowing Star effect using HTML and CSS?

R

rahulmahajann
Improve
Article Tags :
  • Technical Scripter
  • Web Technologies
  • Web Templates

Similar Reads

  • How to create reflection effect using HTML and CSS ?
    The reflection effect is one of the coolest effects that one can use in his/her website. It is a type of informal effect so it is highly recommended not to use it any professional project. You can use it in your personal projects and maybe your portfolio to show your creativity. In this effect, we t
    3 min read
  • How to create Glowing Star effect using HTML and CSS?
    The glowing star effect is one of the coolest effects that is used for dark themed websites. It is known as star effect because it has small glowing balls animated in different ways which looks like stars. This effect can be used for image slider, loader, and maybe as an showcase UI element. Approac
    3 min read
  • How to create Neumorphism Effect using HTML and CSS ?
    Neumorphism (neomorphism) is a modern way of styling web-elements of any web-page and providing a 3D effect. This animation effect can be easily generated by using HTML and CSS. Box-shadow property of CSS can be used to implemented Neumorphism. It is used to add a dark shadow to one side and a light
    2 min read
  • How to create text-reveal effect using HTML and CSS ?
    Text-reveal is a type of effect in which all the alphabets of the text are revealed one by one in some animated way. There are uncountable ways to animate text for this effect. It is up to your creativity how you want the text to reveal. We will look at a basic and easy way to get started. Table of
    3 min read
  • How to create a Portfolio Gallery using HTML and CSS ?
    The portfolio gallery is useful when your website contains different types of content or so much content. With the help of a portfolio gallery, you can easily display all the content on your front page to the user. To create a portfolio gallery we will need only HTML and CSS. We can use JavaScript a
    4 min read
  • How to Create a Cutout Text using HTML and CSS ?
    Cutout text is used as a background header of the webpage. The cutout text creates an attractive look on the webpage. To create a cutout text we will use only HTML and CSS. We display the cutout text and then we make the text blending of an element’s background with the element’s parent. The CSS mix
    3 min read
  • How to Create Engraved Text Effect using HTML and CSS ?
    The engraved text effect is an effect that you can use in your websites as a heading or a sub-heading to make it look more pleasing and attractive. Approach: The engraved text effect can be easily generated using HTML and CSS. First we will create a basic text using HTML and then by using the CSS te
    2 min read
  • How to create a Hero Image using HTML and CSS ?
    A Hero Image is a large image with text, often placed at the top of a webpage. Hero images can be designed using HTML and CSS. This article contains two sections. The first section attaches the image and designs the basic structure. The second section designs the images and texts on the images. The
    2 min read
  • How to create Image Folding Effect using HTML and CSS?
    In this article, we are going to create an image folding effect below the main image. This is a simple project, we can achieve our target only by using HTML and CSS. Approach: Create the main div in which we are creating 4 list tags.Use of nth-child() selector property to give different styles to di
    2 min read
  • How to create windows loading effect using HTML and CSS ?
    In this article, we are going to create a window loading effect before the lock screen appears using HTML and CSS.  Glimpse of the Windows Loading Effect: Approach: Create an HTML file that contains HTML div in which we are giving the loader effect.Then we create 5 span elements which are used for c
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences