Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Element inner dimensions: clientWidth and clientHeight

`clientWidth` and `clientHeight` read an element’s inner width and height in whole CSS pixels. They include padding but exclude borders, margins, and space taken up by scrollbars.

Version: Current web platform; runtime support varies

Syntax

element.clientWidth
element.clientHeight

clientWidth and clientHeight are read-only Element properties, not methods. They return width and height in whole CSS pixels. They are part of CSSOM View, not ECMAScript, and require a DOM with layout support.

They include content and padding, not borders or margins. Scrollbars that take up space reduce width (vertical) or height (horizontal). No CSS box (display: none) or an ordinary inline box gives 0 × 0; an inline-block is measured.

For layout viewport dimensions excluding scrollbar space, read document.documentElement in standards mode or document.body in quirks mode.

scrollWidth and scrollHeight include overflowing content. getBoundingClientRect() includes borders, reflects CSS transforms, and can return fractional dimensions.

Examples

<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>clientWidth and clientHeight</title>
</head>
<body>
  <script>
    const box = document.createElement('div');
    box.style.cssText = 'display: block; box-sizing: content-box; width: 120px; height: 60px; padding: 8px 12px; border: 3px solid; margin: 10px; overflow: hidden;';
    document.body.append(box);

    console.log(`${box.clientWidth} × ${box.clientHeight}`);
    box.style.display = 'none';
    console.log(`${box.clientWidth} × ${box.clientHeight}`);

    box.remove();
  </script>
</body>
</html>

Result

144 × 76
0 × 0

Save the example as an HTML file, open it in a browser, and check the console. The first line includes 12 px of padding on the left and right and 8 px at the top and bottom, but not the border or margin. After setting `display: none`, both measurements are zero.

Related lessons

  • JavaScript · Lesson 27 · Intermediate · Version: 3

Sources