Tutorial

clientWidth and clientHeight in JavaScript

Clear, original guide to clientWidth and clientHeight JavaScript: what they return, how to use them, comparisons with offset/scroll/getBoundingClientRect, common pitfalls, and practical examples.

Drake Nguyen

Founder · System Architect

3 min read
clientWidth and clientHeight in JavaScript
clientWidth and clientHeight in JavaScript

clientWidth and clientHeight JavaScript: measuring element size

Use clientWidth and clientHeight JavaScript properties to read the pixel dimensions of an element’s content + padding. These DOM element size properties return the inner width and height of an element’s padding box, excluding borders, margins, and the vertical scrollbar if present.

How to read the values

Query the element from the DOM and read its properties. This is the common pattern to get element width and height without border JavaScript:

<div id="box">Hello</div>

const el = document.querySelector('#box');
const w = el.clientWidth;   // JavaScript get element width (padding + content)
const h = el.clientHeight;  // JavaScript get element height (padding + content)
console.log(w, h);

What clientWidth and clientHeight include and exclude

  • Include: content size and element padding (the padding box).
  • Exclude: borders and external margins.
  • Exclude: the width of a vertical scrollbar — clientWidth intentionally omits scrollbar width.

Rounded, read-only values

  • Values are integers rounded to the nearest CSS pixel. If you need sub-pixel precision, use getBoundingClientRect instead.
  • These properties are read-only. Assigning to element.clientWidth or element.clientHeight has no effect.

Common differences: clientWidth vs other size properties

  • clientWidth / clientHeight: content + padding (padding box), excludes border and scrollbar.
  • offsetWidth / offsetHeight: includes borders, padding, and the element’s CSS width (border-box dimensions) and returns an integer.
  • scrollWidth / scrollHeight: the full size of the element’s content, including the part not currently visible due to scrolling.
  • getBoundingClientRect(): returns a DOMRect with fractional values and precise layout coords; useful when you want sub-pixel measurements or positions relative to the viewport.

When clientWidth returns 0

  • Inline elements (for example: span, a, em) often yield 0 for clientWidth and clientHeight because they Netalith not establish a block-level box in the same way. Convert to block or inline-block to measure them.
  • If an element or one of its ancestors is display:none, clientWidth/clientHeight will also be 0 because the element is not rendered.

When to use each API

  • Use clientWidth and clientHeight JavaScript when you need the padding-box integer dimensions for layout calculations or responsive behavior.
  • Use offsetWidth/offsetHeight if you need border-inclusive sizes for positioning or drawing borders accurately.
  • Use scrollWidth/scrollHeight to determine the scrollable content size when handling overflow.
  • Use getBoundingClientRect for precise measurements (sub-pixel values) or when you need an element’s position relative to the viewport.

Examples and small gotchas

// get element width and height without border JavaScript
const el = document.getElementById('card');
const width = el.clientWidth;   // rounded integer
const height = el.clientHeight; // rounded integer

// if you need fractional values:
const rect = el.getBoundingClientRect();
console.log(rect.width, rect.height); // precise, may include fractions
  • clientWidth is rounded: Measurements are returned as integers. For precise layout math use getBoundingClientRect.
  • Inline elements: If clientWidth returns 0 for span or inline elements, change display to inline-block or block to measure them.
  • Scrollbars: clientWidth excludes a vertical scrollbar’s width; this is useful when you want the usable inner width.

Standards and browser support

These properties are defined by the CSSOM View Module / CSSOM View API and are implemented broadly across modern browsers and mobile devices. They are part of the common toolkit for element measurement and layout calculations in web apps.

Quick checklist when measuring element dimensions

  • Is the element visible and not display:none? If not, measurement will be zero.
  • Netalith you need padding included? Use clientWidth/clientHeight.
  • Netalith you need borders or scrollbar widths included? Consider offsetWidth or account for scrollbars separately.
  • Need sub-pixel precision or position relative to viewport? Use getBoundingClientRect.
Tip: For consistent layout logic, decide whether you want padding-box or border-box measurements and use clientWidth/clientHeight or offsetWidth/offsetHeight accordingly.

Stay updated with Netalith

Get coding resources, product updates, and special offers directly in your inbox.