JSFiddle - React, Tailwind, and code Playground
HTML
<div id="cont">
<div id="numero">
1
</div>
</div>
<script>
document.getElementById('cont').onmousemove=moveDiv;
</script>
CSS
div#cont{
position:relative;
border-style: solid;
border-width: 2px;
width:500px;
height:500px;
margin-left:auto;
margin-right:auto;
padding:0;
}
div#numero{
position:absolute;
background-color:#ff3324;
border-style: solid;
border-width: 2px;
width:30px;
height:30px;
font-family: "Lucida Grande", "Lucida Sans Unicode", "Lucida Sans", Geneva, Verdana, sans-serif;
font-size:1em;
line-height: 30px;
text-align:center;
margin-left:0;
margin-right:0;
}
JavaScript
function moveDiv(e){
if(!e){e=window.event;}
var el = document.getElementById('numero');
x = e.clientX-this.offsetLeft-this.clientLeft-el.offsetWidth/2;
y = e.clientY-this.offsetTop-this.clientTop-el.offsetHeight/2;
el.style.left = Math.min(Math.max(0,x),this.clientHeight-el.offsetWidth) +"px";
el.style.top = Math.min(Math.max(0,y),this.clientHeight-el.offsetHeight) +"px";
}