9951 explained code solutions for 126 technologies


jqueryHow do I use jQuery to set the position of an element on a web page?


To use jQuery to set the position of an element on a web page, you can use the offset() method. This method takes two parameters - left and top - which represent the left and top positions of the element relative to the document.

For example, the following code will set the position of an element with an ID of myElement to left: 200px; top: 100px;:

$('#myElement').offset({ left: 200, top: 100 });

Code explanation

  • $('#myElement'): selects the element with an ID of myElement
  • offset(): sets the position of the element
  • { left: 200, top: 100 }: the left and top positions of the element relative to the document

Helpful links

Edit this code on GitHub