Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Passing mouse clicks through an overlaying HTML element
When you have an overlaying HTML element that blocks mouse clicks to elements beneath it, you can pass clicks through using CSS or JavaScript techniques.
Method 1: Using CSS pointer-events
The simplest approach is to use the CSS pointer-events property to make the overlay non-interactive:
.overlay {
pointer-events: none;
}
Example: CSS pointer-events
<!DOCTYPE html>
<html>
<head>
<style>
.overlay {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(255, 0, 0, 0.3);
pointer-events: none;
}
.button {
margin: 50px;
padding: 10px 20px;
background-color: blue;
color: white;
border: none;
cursor: pointer;
}
</style>
</head>
<body>
<button class="button" onclick="alert('Button clicked!')">Click Me</button>
<div class="overlay"></div>
</body>
</html>
Method 2: JavaScript Click-Through
For more control, hide the overlay temporarily, find the element beneath, then restore the overlay:
<!DOCTYPE html>
<html>
<head>
<style>
.overlay {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 255, 0, 0.3);
}
.button {
margin: 50px;
padding: 10px 20px;
background-color: purple;
color: white;
border: none;
cursor: pointer;
}
</style>
</head>
<body>
<button class="button" onclick="alert('Purple button clicked!')">Target Button</button>
<div class="overlay" onclick="passClick(event)"></div>
<script>
function passClick(event) {
// Get mouse coordinates
let x = event.clientX;
let y = event.clientY;
// Hide overlay temporarily
let overlay = event.target;
overlay.style.display = 'none';
// Find element beneath
let elementBelow = document.elementFromPoint(x, y);
// Restore overlay
overlay.style.display = 'block';
// Trigger click on element below
if (elementBelow) {
elementBelow.click();
}
}
</script>
</body>
</html>
Comparison
| Method | Performance | Control | Use Case |
|---|---|---|---|
| CSS pointer-events | Best | Limited | Simple overlays |
| JavaScript click-through | Good | Full | Complex interactions |
Key Points
-
pointer-events: nonecompletely disables mouse interaction -
document.elementFromPoint(x, y)finds the topmost element at coordinates - JavaScript method allows selective click-through logic
- Always restore overlay visibility after temporary hiding
Conclusion
Use CSS pointer-events: none for simple click-through overlays. For complex scenarios requiring selective interaction, use the JavaScript approach with elementFromPoint().
Advertisements
