JavaScript Element clientWidth Property

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

What You’ll Learn

Element.clientWidth is a read-only instance property that returns an element’s inner width in pixels. Learn what padding and scrollbars do to the value, how the root element reports viewport width, and how it compares to offsetWidth / scrollWidth—with five examples and try-it labs.

01

Kind

Instance property

02

Access

Read-only

03

Type

Integer (px)

04

Includes

Padding

05

Excludes

Border · margin

06

Status

Baseline widely

Introduction

Layout work often needs the visible content box width: how wide is the area inside the borders? That is what clientWidth answers.

It includes padding, but leaves out borders, margins, and any vertical scrollbar. For elements with no CSS/inline layout box, the value is 0.

JavaScript
const box = document.getElementById("box");
console.log(box.clientWidth); // inner width in pixels
💡
Beginner tip

Think: client = the client area (content + padding). Borders sit outside that area and do not count.

Understanding the Property

MDN: the clientWidth read-only property of the Element interface is zero for elements with no CSS or inline layout boxes; otherwise, it is the inner width of an element in pixels. It includes padding but excludes borders, margins, and vertical scrollbars (if present).

  • Read-only — change CSS/layout, then read again.
  • Integer pixels — rounded layout measurement.
  • Formula — CSS width + padding − vertical scrollbar width.
  • Baseline — widely available since July 2015 (MDN).

📝 Syntax

JavaScript
clientWidth

Value

An integer: the element’s inner width in pixels.

ItemDetail
Typenumber (integer pixels)
AccessRead-only
IncludesContent width + padding
ExcludesBorder, margin, vertical scrollbar
⚠️
Root / viewport

On document.documentElement (the <html> root), or on <body> in quirks mode, clientWidth returns the viewport width excluding any scrollbar.

📋 Measuring a Box

Give an element a fixed width and padding, then read clientWidth:

JavaScript
<div id="box" style="width:200px;padding:10px;border:5px solid #333;">
  Hello
</div>
JavaScript
const box = document.getElementById("box");
console.log(box.clientWidth);
// width 200 + padding-left 10 + padding-right 10 = 220
// border is NOT included

Related learning: clientHeight, offsetWidth, scrollWidth, and getBoundingClientRect().

⚡ Quick Reference

GoalCode / note
Read widthel.clientWidth
Viewport widthdocument.documentElement.clientWidth
With bordersel.offsetWidth
Scrollable contentel.scrollWidth
MDN statusBaseline Widely available

🔍 At a Glance

Four facts about Element.clientWidth.

Kind
get only

Instance

Type
integer

Pixels

Includes
padding

Not border

Baseline
widely

Jul 2015+

Examples Gallery

Examples follow MDN Element: clientWidth. Labs use a styled box so the pixel measurements are easy to predict.

📚 Getting Started

Read the inner width and see how padding contributes.

Example 1 — Read clientWidth

Log the client width of a fixed-size box.

JavaScript
const box = document.getElementById("box");
console.log(box.clientWidth);
Try It Yourself

How It Works

With width: 200px and no padding, clientWidth is typically 200. Borders do not change this value.

Example 2 — Padding Is Included

Add horizontal padding and watch the measurement grow.

JavaScript
const box = document.getElementById("box");
// style: width:200px; padding:10px; border:5px solid
console.log(box.clientWidth); // 220 (200 + 10 + 10)
Try It Yourself

How It Works

Left and right padding add to the client width. The 5px border is ignored by clientWidth (but not by offsetWidth).

📈 Compare Sizes, Viewport & Snapshot

Relate client, offset, and scroll widths; then measure the viewport.

Example 3 — vs offsetWidth and scrollWidth

Compare three common width measurements on one element.

JavaScript
const box = document.getElementById("box");
console.log({
  clientWidth: box.clientWidth,
  offsetWidth: box.offsetWidth,
  scrollWidth: box.scrollWidth
});
Try It Yourself

How It Works

offsetWidth often adds borders (here +10). scrollWidth grows when content overflows the visible client area.

Example 4 — Viewport Width

Read the visible viewport width from the document element.

JavaScript
console.log(document.documentElement.clientWidth);
Try It Yourself

How It Works

This is a common way to get layout viewport width without including the scrollbar thickness. Exact numbers depend on the window size.

Example 5 — Support Snapshot

Feature-detect and remember what the value includes.

JavaScript
console.log({
  supported: "clientWidth" in Element.prototype,
  includes: "content width + padding",
  tip: "Use offsetWidth for borders; scrollWidth for overflow content",
  status: "Baseline Widely available (MDN)"
});
Try It Yourself

How It Works

Pick the measurement that matches your layout question: inner area, border box, or scrollable content.

🚀 Common Use Cases

  • Measuring the visible width of panels, cards, and sidebars.
  • Detecting whether content overflows (scrollWidth > clientWidth).
  • Sizing overlays or full-bleed overlays against the viewport.
  • Responsive logic that depends on available client area.
  • Debugging layout differences between padding and borders.

🔧 How It Works

1

Browser lays out the box

Width, padding, border, and scrollbars are computed.

Layout
2

Client area is measured

Content width plus horizontal padding.

Client
3

Borders and margins stay out

Vertical scrollbar width is subtracted if present.

Exclude
4

You get an integer

Use it for layout math, overflow checks, and viewport sizing.

📝 Notes

Browser Support

Element.clientWidth 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.clientWidth

Read-only integer — inner width in pixels (includes padding, excludes border/margin).

Baseline Widely available
Google Chrome Supported
Yes
Microsoft Edge Supported
Yes
Mozilla Firefox Supported
Yes
Apple Safari Supported
Yes
Opera Supported
Yes
Internet Explorer Supported
Yes
clientWidth Baseline

Bottom line: Use clientWidth for the visible client area. Compare with offsetWidth (borders) and scrollWidth (overflow). On the root element it reports viewport width.

Conclusion

clientWidth is the go-to measurement for an element’s inner width. Include padding, skip borders and margins, and use the root element when you need viewport width.

Continue with currentCSSZoom, clientTop, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Use it for visible content-area width
  • Compare with scrollWidth to detect overflow
  • Use document.documentElement.clientWidth for viewport
  • Re-read after layout or resize changes
  • Pair with clientHeight for 2D measurements

❌ Don’t

  • Expect borders or margins to be included
  • Assign to clientWidth (read-only)
  • Confuse it with CSS width alone when padding exists
  • Assume non-layout elements return a useful size
  • Skip reflow timing when measuring right after DOM inserts

Key Takeaways

Knowledge Unlocked

Five things to remember about clientWidth

Read-only inner width in pixels—padding in, border and margin out.

5
Core concepts
📝 02

Integer px

inner width

Type
🔍 03

Layout math

panels & overflow

Use
04

Baseline

widely available

Status
🎯 05

Padding in

border out

Tip

❓ Frequently Asked Questions

It returns the element's inner width in pixels, including padding but excluding borders, margins, and vertical scrollbars.
No. MDN marks Element.clientWidth as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
No. It is a read-only instance property. Change layout with CSS or style APIs, then read clientWidth again.
Roughly CSS width + CSS padding − width of a vertical scrollbar if one is present.
On the root <html> element (or <body> in quirks mode), clientWidth returns the viewport width excluding any scrollbar.
offsetWidth includes borders (and often scrollbars). scrollWidth is the full scrollable content width, which can be larger than the visible client area.
Did you know?

A classic overflow check is el.scrollWidth > el.clientWidth—true when content is wider than the visible client area.

Next: currentCSSZoom

Learn how to read an element’s effective CSS zoom factor.

currentCSSZoom →

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.

5 people found this page helpful