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?
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.
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.
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.
Works in Chrome, Safari, and Firefox:
<div>
and choose 'Inspect Element'.$0.offsetParent
. Press enter.Bonus: In Chrome and Safari, If you right-click the result in the console you can reveal it in the elements panel.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With