Deepanshu Singh
Deepanshu Singh

Reputation: 59

How to set Coordinates(Position) of an Image in a webpage?

I want to know if it is possible to know or set a particular position or say coordinates of an Image or DIV element in a webpage?

Example : Say there is a image1 or DIV1 (containing some data) , Now I want to set coordinate say (x,y) on a webpage and because of some reasons if coordinates of Image or DIV element gets change due to some reason , Then how I can get the change location of that Images or DIV on my webpage ?

Thanks

Upvotes: 2

Views: 27845

Answers (1)

Timur Osadchiy
Timur Osadchiy

Reputation: 6209

You should look at css position: absolute; with left, top, right, bottom properties. To change the position later use JavaScript:

var el = document.getElementById("container");
el.style.top = "150px";
el.style.left = "100px";
#container {
  width: 100px;
  height: 100px;
  position: absolute;
  top: 50%;
  left: 50%;
  margin-top: -50px;
  margin-left: -50px;
  background: orange;
}
<div id="container"></div>

Upvotes: 3

Related Questions