JSFiddle - React, Tailwind, and code Playground
HTML
<div class="a" id="a">
A
</div>
<div class="b">
B
</div>
<div class="c">
C
<div class="d">
D
</div>
</div>
<textarea id="debug">
</textarea>
CSS
.a,.b,.c,.d{
border: solid 1px black;
opacity: 0.5;
font-family: arial;
position: absolute;
font-size: 20px;
}
.a{
width:300px;
height:250px;
top:30px;
left:20px;
background:green;
}
.b{
width:300px;
height:145px;
top:10px;
left:20px;
background:blue;
}
.c{
width:150px;
height:300px;
top:30px;
left:60px;
background:red;
}
.d{
margin:10px;
background:yellow;
width:100px;
height:200px
}
#debug{
position:absolute;
border:solid 1px;
top:20px;
left:350px;
width:250px;
height:200px;
}
JavaScript
let el = document.getElementById("a");
let debug = document.getElementById("debug");
let rect = el.getBoundingClientRect();
let right = rect.x + rect.width;
let bottom = rect.y + rect.height;
let elements = [];
for (let i = rect.x; i < right; i += 10) {
for (let j = rect.y; j < bottom; j += 10) {
let sampled = document.elementFromPoint(i, j);
if (sampled && sampled !== el && elements.indexOf(sampled) === -1) {
elements.push(sampled);
}
}
}
debug.value = elements;