javascript - Center an element (Absolute) on the click -
so, when click on screen, want display circle (centered verticaly , horizontaly) click.
my poblem time corner top left of element.
here plunker: https://plnkr.co/edit/dlequk0ikxhwum1swk0t
i have tried with
margin-left:-50% but without success.
edit: got answers quickly, thanks!
the answer add class:
transform:translate(-50%, -50%); display: block; // code goes here function printmousepos(event) { var element = document.getelementbyid("body"); var para = document.createelement("div"); para.classname = "circle"; para.setattribute("style", "position : absolute;left:" + event.clientx + "px;top:" + event.clienty + "px"); element.appendchild(para); console.log(event.clientx, event.clienty); } document.addeventlistener("click", printmousepos); /* styles go here */ body { position: relative; height: 1000px; } .circle { color: blue; margin: 0 auto; width: 200px; height: 200px; border-style: solid; border-width: 20px; border-color: blue; border-radius: 50%; } <body id="body"> click everywhere </body>
add
transform: translate(-50%,-50%); display: block; to .circle class
Comments
Post a Comment