Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get position information of HTML element

I have a div{position:absolute}. From w3cschools:

absolute - The element is positioned relative to its first positioned (not static) ancestor element

Is there an easy way with browser inspector(Chrome, Firebug) to get the first not static ancestor, to which my div was positioned relatively?

like image 535
Islam Sabyrgaliyev Avatar asked Jan 30 '14 05:01

Islam Sabyrgaliyev


People also ask

How do you find the position of an element in HTML?

The offsetTop property returns the top position (in pixels) relative to the parent. The returned value includes: the top position, and margin of the element. the top padding, scrollbar and border of the parent.

How do you find XY coordinates of a div?

If the element is in the main document you can get the DIV's coordinates with... var X=window. getComputedStyle(abc,null). getPropertyValue('left'); var Y=window.

How do I find the coordinates of a div?

To get the X and Y coordinates for a div element with JavaScript, we an use the getBoundingXClientRect method. to select the div with querySelector . And then we call getBoundingClientRect to get the position of the div. Then we get the x and y coordinates of the div with position.


1 Answers

Works in Chrome, Safari, and Firefox:

  1. Right-click the <div> and choose 'Inspect Element'.
  2. In the inspector choose the "Console" tab and type in $0.offsetParent. Press enter.
  3. The result is the offset parent of your selected element.

Bonus: In Chrome and Safari, If you right-click the result in the console you can reveal it in the elements panel.

like image 193
oztune Avatar answered Oct 11 '22 01:10

oztune