javascript get element height: measuring DOM height in JavaScript

Learn how to reliably read an element's height in the DOM with JavaScript. This guide covers offsetHeight, clientHeight, and getBoundingClientRect(), with practical code examples and cross‑browser notes for responsive UI design.

JavaScripting
JavaScripting Team
·5 min read
Element Height - JavaScripting
Quick AnswerSteps

To read an element’s height in JavaScript, first ensure the element exists and is visible, then measure its height after layout. Use either element.getBoundingClientRect().height or element.offsetHeight for the outer height, and element.clientHeight for the inner content height. For precise, include borders by using getBoundingClientRect().height. Avoid measuring before the element renders.

javascript get element height: Understanding the DOM height measurements

In JavaScript, measuring how tall an element appears on screen is a common task for building responsive UI and debugging layout issues. The phrase javascript get element height surfaces frequently in developer queries because height can mean several things: the visual height, the content height, and the height including borders. According to JavaScripting, the right approach depends on what you mean by height and when you measure it. The browser performs layout to compute the final geometry, so you should read height after layout has occurred.

JavaScript
// Basic read using the on-screen height const el = document.querySelector('#myBox'); const height = el.getBoundingClientRect().height; // height in CSS pixels console.log('Rendered height:', height);
JavaScript
// Alternative outer vs inner height in pixels console.log('outer (offsetHeight):', el.offsetHeight); console.log('inner (clientHeight):', el.clientHeight);

In practice, prefer getBoundingClientRect() for rendered height because it reflects transforms and CSS changes more accurately than offsetHeight in many layouts.

^note^1^ There is no ^note^ in real markdown; keep content functional.

Steps

Estimated time: 15-25 minutes

  1. 1

    Identify the target element

    Select the element you want to measure using a reliable selector. Ensure it is mounted in the DOM and not hidden.

    Tip: Prefer querySelector over brittle selectors to keep the code maintainable.
  2. 2

    Wait for layout to complete

    Measure after the browser has performed layout. Use requestAnimationFrame or a 'load' event to delay the read.

    Tip: Reading height before layout yields zeros.
  3. 3

    Choose the height metric

    Decide between getBoundingClientRect().height, offsetHeight, or clientHeight based on whether you need borders or padding.

    Tip: offsetHeight includes borders; clientHeight excludes borders.
  4. 4

    Read and log the height

    Call the chosen property and log or store the value for further processing.

    Tip: Keep unit consistency (px) when comparing heights.
  5. 5

    Handle dynamic changes

    If content changes, re-measure using ResizeObserver or a resize listener to keep UI in sync.

    Tip: Use ResizeObserver when supported for precise updates.
  6. 6

    Validate across scenarios

    Test with various content heights and responsive breakpoints to ensure accuracy.

    Tip: Test in at least two browser environments.
Pro Tip: Measure after the page has laid out to avoid reading a height of 0.
Warning: Do not measure elements with display:none; they have no layout height.
Note: Remember: offsetHeight includes padding and borders, while clientHeight excludes borders.
Pro Tip: Use getBoundingClientRect() for the most consistent on-screen height, including transforms.

Prerequisites

Required

Keyboard Shortcuts

ActionShortcut
Open DevToolsIn most browsersCtrl++I
Inspect element toolCtrl++C
Refresh with DevTools openCtrl+F5
Toggle device toolbar (responsive mode)Ctrl++M

Questions & Answers

What is the difference between offsetHeight and clientHeight?

offsetHeight includes padding and borders, giving the outer height. clientHeight is the inner height excluding borders, but including padding. Choose based on whether borders are relevant to your calculation.

OffsetHeight is the outer height including borders; clientHeight is the inner height excluding borders, so choose based on whether you care about borders.

When should I use getBoundingClientRect().height?

Use getBoundingClientRect().height when you need the element's on-screen height, including transforms. It tends to be reliable for visual layouts.

Use getBoundingClientRect().height to reflect what the user sees on screen.

Can I measure height of a hidden element?

Hidden elements (display:none) do not participate in layout, so their height is zero. Show or temporarily reveal them to measure.

If an element is hidden, you’ll get zero height; reveal it to measure.

Which method is best for dynamic content?

For dynamic content, prefer ResizeObserver (where supported) or re-measure on resize during layout updates.

ResizeObserver tracks height changes in real time, great for dynamic content.

Is ResizeObserver universally supported?

ResizeObserver has broad support but isn’t universal. Include a fallback for older browsers with a window resize listener.

Most modern browsers support ResizeObserver, but add a fallback.

How can I include margins in height?

Heights typically don’t include margins. To account for margins, compute height plus computed margins or use getBoundingClientRect(), then add margins if needed.

Margins aren’t part of element height; add them separately if needed.

What to Remember

  • Use getBoundingClientRect() for rendered height
  • OffsetHeight includes borders; clientHeight excludes borders
  • Measure after layout to avoid zero values
  • ResizeObserver helps track height changes
  • Test across browsers for consistency