Categories
JavaScript Answers

How to position a DIV in a specific coordinates with JavaScript?

Spread the love

To position a DIV in a 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 = "10px";
d.style.top = "20px";

to select the element with getElementById.

We set its position to 'absolute'.

And then we set the left and top position to shift it horizontally and vertically.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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