How to position a DIV in specific coordinates with JavaScript?

Spread the love

Sometimes, we want to position a DIV in specific coordinates with JavaScript.

In this artivcle, we’ll look at how to position a DIV in specific coordinates with JavaScript.

How to position a DIV in specific coordinates with JavaScript?

To position a DIV in specific coordinates with JavaScript, we set the left and top properties.

For instance, we write

const d = document.getElementById("yourDivId");
d.style.position = "absolute";
d.style.left = xPos + "px";
d.style.top = yPos + "px";

to select the element with getElementById.

Then we set its position to 'absolute' with

d.style.position = "absolute";

Then we set its left and top positions to the x and y coordinates.

Conclusion

To position a DIV in specific coordinates with JavaScript, we set the left and top properties.

Leave a Reply

Your email address will not be published. Required fields are marked *