JSFiddle - React, Tailwind, and code Playground

by Ahmad Baktash Hayeri

HTML

<div id="parentDiv">
    <div id="childDiv"></div>
</div>

CSS

#parentDiv {
    background-color: black;
    height: 500px;
    width: 500px;
}

#childDiv {
    visibility: hidden;
    position: absolute;
    background-color: red;
    height: 100px;
    width: 100px;
    top: 50px;
    left: 50px;
}

JavaScript

document.getElementById('parentDiv').addEventListener('click', function(event) {
    var child = document.getElementById('childDiv');
    var mouseX = event.clientX || event.pageX;
    var mouseY = event.clientY || event.pageY;
    var top = mouseY - 50;
    var left = mouseX - 50;
    child.style.top = top.toString()+"px";
    child.style.left = left.toString()+"px";
    child.style.visibility = "visible";
    console.log()
});