This tool provides a visual reference for the different size and positioning values of elements through JavaScript. Click the buttons to get the values along with guidelines showing where they are drawn from. Details on each property and additional notes are further below.
Notes
-
There's a ton of documentation on MDN about getting element and sizes positions. So much so that it's a struggle to sort through. I built this page for a quick visual reference to show me what I need.
-
This example isn't really responsive yet. If you resize you'll need to refresh to get things to line back up.
-
The
.clientTop,.clientLeft,.offsetTop, and.offsetLeftare integers. Browsers can render with floating point precision. Sometimes that makes the lines like like they are slightly off as a result. -
The values of
.scrollTop, and.scrollLefthad subpixel precision in modern browsers. The off by one appearance should not be a problem with them. -
The
top,left,bottom,right,x, andyproperties on.getBoundingClientRect()all offer subpixel precision in modern browsers. -
The
getClientRects()can also be used but the values are all the same for block elements which is all I'm covering here. -
I'm not adding
.getComputedStyle()since those values are intentionally inaccurate at times to resist browser fingerprinting. -
.offsetTopand.offsetLeftare based off a0, 0coordinate at the upper left of the page in this example. That is their offsetParent. That starting position can change if another element in their tree has apositionapplied to it. -
The
topandleftproperties from.getBoundingClientRect()produce the same values asy, andx, respectively. -
The
top,bottom,left,right,x,yproperties are.getBoundingClientRect()are based on the viewport. If the top left of the viewport is at the0, 0position of the page thetop/y,left/xvalues will be the same as.offsetTopand.offsetLeft, respectively. Scrolling causes the values to diverge. -
The content area in the example overflows on to the right and down. The overflow is clipped by default. It's shown when selecting one of the
scrollproperties to better illustrate the positioning. -
The values of
.clientWidthand.clientHeightdon't take border into account. The values of.offsetWidthand.offsetHeightdo. -
The values are
.clientTopand.clientLeftare the size of the border instead of a distance to the element. -
I haven't seen a way to get margins directly. As far as I can tell, you have to do calculations with other elements to figure that out.