JavaScript Element getBoundingClientRect() Method

Beginner
⏱️ 12 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline
Instance method

What You’ll Learn

Element.getBoundingClientRect() is an instance method that returns a DOMRect describing an element’s size and position relative to the viewport. Learn top, left, width, height, how scrolling changes values, document coordinates, and five try-it labs aligned with MDN.

01

Kind

Instance method

02

Returns

DOMRect

03

Args

None

04

Box

Border box

05

Relative to

Viewport

06

Status

Baseline widely

Introduction

When you build tooltips, sticky headers, drag-and-drop UIs, or scroll-spy navigation, you often need to know where an element sits on screen and how big it is. getBoundingClientRect() answers both questions in one call.

It returns a DOMRect with pixel values for top, left, right, bottom, x, y, width, and height. Positions are relative to the viewport, not the whole document.

💡
Beginner tip

MDN: the rectangle includes padding and border (the CSS border box). If the page scrolls, top and left change because they are viewport-relative. Add window.scrollY and window.scrollX for document coordinates.

This page is part of JavaScript Element. Pair it with checkVisibility() when you need visibility checks too.

Understanding the getBoundingClientRect() Method

Calling el.getBoundingClientRect() measures the smallest rectangle that contains the entire element’s border box and returns it as a DOMRect.

  • It is an instance method on Element.
  • Takes no parameters (MDN).
  • Returns a DOMRect with size and viewport-relative position.
  • width / height include padding and border-width.
  • Scrolling updates top, left, right, bottom.
  • Related: getClientRects() for per-line boxes (MDN).

📝 Syntax

General form of Element.getBoundingClientRect (MDN):

JavaScript
getBoundingClientRect()

Parameters

None.

Return value

A DOMRect object. Its left, top, right, bottom, x, and y are distances from the viewport edges in pixels. width and height describe the border box size.

Common patterns

JavaScript
const box = element.getBoundingClientRect();

console.log(box.width, box.height);
console.log(box.top, box.left);

// Document coordinates (MDN):
const docTop = box.top + window.scrollY;
const docLeft = box.left + window.scrollX;

// Center point for tooltips:
const centerX = box.left + box.width / 2;
const centerY = box.top + box.height / 2;

⚡ Quick Reference

GoalCode
Read size + positionel.getBoundingClientRect()
Width / heightrect.width, rect.height
Viewport offsetrect.top, rect.left
Document positionrect.top + scrollY
Per-line boxesel.getClientRects()
MDN statusBaseline Widely available (since July 2015)

🔍 At a Glance

Four facts to remember about Element.getBoundingClientRect().

Returns
DOMRect

Size + position

Baseline
widely

Since July 2015

Box model
border

Inc. padding

Scroll
viewport

Values shift

📋 Layout measurement APIs

getBoundingClientRect()offsetWidth / offsetHeightgetClientRects()
ReturnsDOMRect (size + position)Number (size only)DOMRectList (multiple boxes)
PositionViewport-relativeNo positionViewport-relative
Best forTooltips, overlays, scroll logicQuick size readsWrapped inline text
Scroll effectYes (top/left change)NoYes
Box includedBorder boxBorder boxBorder box per fragment

Examples Gallery

Examples follow MDN Element.getBoundingClientRect() patterns. Use View Output or Try It Yourself for each case.

📚 Getting Started

Read width, height, top, and left from a styled box (MDN basic example).

Example 1 — Basic Size and Position

MDN: width and height include padding and border.

JavaScript
const box = document.querySelector(".demo-box");
const rect = box.getBoundingClientRect();

console.log(rect.width);   // includes padding + border
console.log(rect.height);
console.log(rect.top);     // distance from viewport top
console.log(rect.left);    // distance from viewport left
Try It Yourself

How It Works

The returned rectangle is the element’s border box. Exact top and left depend on layout and viewport size.

Example 2 — Values Change When You Scroll

MDN scrolling demo: top decreases as you scroll down.

JavaScript
const box = document.getElementById("example");
const rect = box.getBoundingClientRect();

console.log(rect.top);
// Smaller number as you scroll down the page
Try It Yourself

How It Works

Positions are relative to the viewport, not the document. Scroll listeners often re-read getBoundingClientRect() for sticky UI or scroll-spy.

📈 Practical Patterns

Document coordinates, center points, and in-viewport checks.

Example 3 — Document Coordinates

MDN: add scrollY and scrollX for page-relative position.

JavaScript
const rect = element.getBoundingClientRect();

const docTop = rect.top + window.scrollY;
const docLeft = rect.left + window.scrollX;

console.log(docTop, docLeft);
// Stable while scrolling
Try It Yourself

How It Works

Use document coordinates when comparing elements across a long page or storing positions that should not change with scroll offset alone.

Example 4 — Center Point for a Tooltip

Compute the middle of an element to position floating UI.

JavaScript
const rect = button.getBoundingClientRect();

const centerX = rect.left + rect.width / 2;
const centerY = rect.top + rect.height / 2;

tooltip.style.left = `${centerX}px`;
tooltip.style.top = `${centerY - 8}px`;
Try It Yourself

How It Works

getBoundingClientRect() is the standard way to align popovers, tooltips, and dropdowns to trigger elements.

Example 5 — Is the Element in the Viewport?

Check whether any part of the box intersects the visible viewport.

JavaScript
const rect = element.getBoundingClientRect();
const vh = window.innerHeight;
const vw = window.innerWidth;

const inView =
  rect.bottom > 0 &&
  rect.right > 0 &&
  rect.top < vh &&
  rect.left < vw;

console.log(inView);
Try It Yourself

How It Works

For richer visibility rules (opacity, overflow, etc.), see checkVisibility(). Simple intersection checks often start with getBoundingClientRect().

🚀 Common Use Cases

  • Positioning tooltips, popovers, and context menus near a trigger element.
  • Scroll-spy navigation that highlights the section currently in view.
  • Drag-and-drop hit testing and drop-zone detection.
  • Lazy-loading images when they enter the viewport.
  • Measuring carousel slides or grid items for animations.
  • Converting viewport coordinates to document coordinates for layout math.

🧠 How getBoundingClientRect() Measures an Element

1

Call with no arguments

element.getBoundingClientRect()

Invoke
2

Compute border box

Browser finds the smallest rectangle containing padding, border, and content.

Layout
3

Return DOMRect

top, left, width, height relative to the viewport.

Result
4

Use for UI math

Add scrollX/scrollY for document coords; use getClientRects() for wrapped text.

📝 Notes

  • Not Deprecated, Experimental, or Non-standard on MDN (Baseline Widely available, July 2015).
  • Coordinates are viewport-relative; they change when the page scrolls.
  • Includes padding and border (border box), not margin.
  • For document position: rect.top + window.scrollY (MDN).
  • Wrapped inline text may need getClientRects() instead.
  • Related: checkVisibility(), closest(), JavaScript hub.

Browser Support

Element.getBoundingClientRect() is Baseline Widely available (MDN: across browsers since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.

Baseline Widely available

Element.getBoundingClientRect()

Measure element size and viewport position with DOMRect — essential for tooltips, scroll logic, and layout UI.

Baseline Widely available
Google Chrome Supported · Desktop & Mobile
Yes
Microsoft Edge Supported · Chromium
Yes
Mozilla Firefox Supported · Desktop & Mobile
Yes
Apple Safari Supported · macOS & iOS
Yes
Opera Supported · Modern versions
Yes
Internet Explorer Supported in legacy IE
Yes
getBoundingClientRect() Excellent

Bottom line: Use getBoundingClientRect() when you need both size and viewport-relative position. Remember to account for scroll when converting to document coordinates.

Conclusion

Element.getBoundingClientRect() returns a DOMRect with an element’s border-box size and viewport-relative position—the go-to API for layout measurements in the browser.

Continue with checkVisibility(), closest(), shadowRoot, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Re-read on scroll/resize when positioning fixed UI
  • Add scrollX/scrollY for document coordinates
  • Use center math for tooltips and popovers
  • Cache reads inside animation frames when possible
  • Pair with ResizeObserver when layout changes often

❌ Don’t

  • Assume top/left are document coordinates
  • Forget padding and border are included in width/height
  • Use for margin measurements (margin is outside the box)
  • Call excessively inside tight loops without throttling
  • Expect one rect for multi-line inline text (use getClientRects())

Key Takeaways

Knowledge Unlocked

Five things to remember about Element.getBoundingClientRect()

Size and viewport position in one call.

5
Core concepts
📄 02

Args

none

Zero
✍️ 03

Box

border

Model
04

Baseline

widely available

Status
05

Scroll

viewport-relative

Coords

❓ Frequently Asked Questions

It returns a DOMRect object with the element's size and its position relative to the viewport. Properties include top, left, right, bottom, x, y, width, and height in pixels.
No. MDN marks Element.getBoundingClientRect() as Baseline Widely available (across browsers since July 2015). It is not Deprecated, Experimental, or Non-standard.
Yes. MDN states the returned rectangle is the border box, including padding and border-width (not only content). With box-sizing: border-box, width and height match the CSS width/height.
Yes. top, left, right, and bottom are relative to the viewport, so they change when the page scrolls. Add window.scrollX and window.scrollY to get document coordinates.
offsetWidth and clientWidth return numbers for size only. getBoundingClientRect() returns a full DOMRect with position and size relative to the viewport.
Use getClientRects() when an element spans multiple lines (like inline text) and you need one rectangle per fragment. getBoundingClientRect() returns the union of all those boxes.
Did you know?

MDN notes that the returned rectangle can be thought of as the union of the rectangles from getClientRects() for the element. For everyday single-box elements, getBoundingClientRect() is the simplest choice.

More Element Topics

Browse Element methods and properties to keep building your DOM skills.

checkVisibility() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful