JavaScript Element scrollWidth Property

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

What You’ll Learn

Element.scrollWidth is a read-only instance property that measures the full width of an element’s content, including hidden overflow. Learn how it differs from clientWidth, how to detect horizontal overflow, and how it pairs with scrollLeft—with five examples and try-it labs.

01

Kind

Instance property

02

Access

Read-only

03

Type

Integer (pixels)

04

Includes

Padding & overflow

05

Status

Baseline widely

06

Pairs with

clientWidth, scrollLeft

Introduction

When a box has more content than fits in its visible width, the extra content is hidden behind a horizontal scrollbar. scrollWidth tells you how wide the full content is, not just the part you can currently see.

That makes it useful for carousels, tab strips, wide tables, and any UI where you need to know whether content overflows sideways or how far scrollLeft can move.

JavaScript
const box = document.getElementById("strip");
console.log(box.scrollWidth);
💡
Beginner tip

Think of scrollWidth as “how wide would this box need to be to show all content without scrolling horizontally?”

Understanding the Property

MDN: the read-only scrollWidth property is a measurement of the width of an element’s content, including content not visible on the screen due to overflow.

  • Read-only — use it to measure, not assign.
  • Includes padding — measured like clientWidth.
  • Excludes border and margin — and vertical scrollbars.
  • Equals clientWidth when content fits — no horizontal overflow needed.

📝 Syntax

JavaScript
scrollWidth

Value

An integer representing the total content width in CSS pixels.

ItemDetail
TypeInteger (pixels)
AccessRead-only property
IncludesPadding and hidden overflow content
ExcludesBorder, margin, vertical scrollbar
RelatedclientWidth, offsetWidth, scrollLeft
⚠️
Max horizontal scroll

The farthest scrollLeft can go is usually scrollWidth - clientWidth. That portable formula replaces the non-standard scrollLeftMax property.

📋 Overflow & Max Scroll Pattern

Detect horizontal overflow, then compute the maximum scroll offset:

JavaScript
function isOverflowingX(element) {
  return element.scrollWidth > element.clientWidth;
}

const maxLeft = box.scrollWidth - box.clientWidth;

Related learning: scrollHeight, scrollLeft, and clientWidth.

⚡ Quick Reference

GoalCode / note
Read full content widthel.scrollWidth
Visible inner widthel.clientWidth
Detect horizontal overflowel.scrollWidth > el.clientWidth
Max scrollLeftel.scrollWidth - el.clientWidth
Content fitsel.scrollWidth === el.clientWidth
MDN statusBaseline Widely available

🔍 At a Glance

Four facts about Element.scrollWidth.

Kind
get only

Instance

Type
integer

Pixels

Measures
overflow

Full content

Baseline
widely

Jul 2015+

Examples Gallery

Examples follow MDN Element: scrollWidth. Labs use horizontally scrollable boxes so you can compare visible width with full content width.

📚 Getting Started

Start by reading the full content width of a scrollable element.

Example 1 — Read scrollWidth

Measure the total width of content inside a scrollable strip.

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

How It Works

scrollWidth counts all content, including the part hidden to the right of the visible area.

📈 Compare, Overflow & Max Scroll

See how scrollWidth differs from clientWidth and powers horizontal scroll checks.

Example 2 — Compare with clientWidth

Visible width and full content width are often different in overflow boxes.

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

How It Works

clientWidth is what you see. scrollWidth is the full content width needed to avoid horizontal scrolling.

Example 3 — Detect Horizontal Overflow

Compare the two widths to learn whether content spills sideways.

JavaScript
function isOverflowingX(element) {
  return element.scrollWidth > element.clientWidth;
}

const box = document.getElementById("strip");
console.log(isOverflowingX(box));
Try It Yourself

How It Works

If scrollWidth is larger, the content does not fully fit in the visible width.

Example 4 — Compute Max scrollLeft

Use scrollWidth - clientWidth as the portable maximum horizontal scroll.

JavaScript
const box = document.getElementById("strip");
const maxLeft = box.scrollWidth - box.clientWidth;
box.scrollLeft = maxLeft;
console.log(maxLeft);
Try It Yourself

How It Works

That difference is how far you can scroll to the right before reaching the end.

Example 5 — Support Snapshot

Feature-detect and remember the measurement rules.

JavaScript
console.log({
  supported: "scrollWidth" in Element.prototype,
  returns: "integer pixels",
  includes: "padding and hidden overflow",
  status: "Baseline Widely available (MDN)"
});
Try It Yourself

How It Works

Safe to use widely. It is the horizontal counterpart of scrollHeight.

🚀 Common Use Cases

  • Detecting whether a carousel, tab bar, or table overflows horizontally.
  • Computing the maximum scrollLeft for custom next/previous controls.
  • Showing a “scroll sideways” hint when content is wider than the box.
  • Building horizontal scroll progress indicators.
  • Comparing visible and full content width after layout changes.

🔧 How It Works

1

Browser measures all content

Visible content and hidden overflow are both counted.

Layout
2

Padding is included

The measurement follows the same rules as clientWidth.

Box model
3

Returns an integer width

You read the value with element.scrollWidth.

Read
4

Compare with clientWidth and scrollLeft

Those comparisons power overflow checks and max-scroll calculations.

📝 Notes

  • Not Deprecated, Experimental, or Non-standard on MDN (Baseline Widely available).
  • Returns an integer, while scrollLeft may contain decimals.
  • Does not include border, margin, or a vertical scrollbar.
  • Related: scrollHeight, scrollLeft, clientWidth, scrollTopMax, JavaScript hub.

Browser Support

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

Read-only — returns the full content width in pixels, including hidden overflow.

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
scrollWidth Baseline

Bottom line: Use scrollWidth to measure full content width, detect horizontal overflow, and compute max scrollLeft with clientWidth.

Conclusion

scrollWidth is the full-content width measurement behind many horizontal scroll patterns. It includes hidden overflow, works with clientWidth, and pairs naturally with scrollLeft for max-scroll calculations.

Continue with shadowRoot, scrollHeight, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Compare scrollWidth with clientWidth for overflow checks
  • Use scrollWidth - clientWidth for max horizontal scroll
  • Re-measure after content or styles change
  • Pair with scrollLeft for progress and end-of-scroll logic
  • Test on real scrollable containers, not only the page body

❌ Don’t

  • Confuse scrollWidth with offsetWidth
  • Forget that border and margin are excluded
  • Assign el.scrollWidth = ... (read-only)
  • Assume width never changes after first measure
  • Use non-standard scrollLeftMax when this formula works

Key Takeaways

Knowledge Unlocked

Five things to remember about scrollWidth

Read-only integer width for all content, including hidden overflow.

5
Core concepts
📝02

integer

pixels

Type
🔍03

Full content

includes overflow

Use
04

Baseline

widely available

Status
🎯05

W − C

max scrollLeft

Tip

❓ Frequently Asked Questions

It returns the total width of an element's content, including content that is not visible because of overflow. It is a read-only measurement in pixels.
No. MDN marks Element.scrollWidth as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
clientWidth is the visible inner width of the element. scrollWidth is the full content width needed to show everything without a horizontal scrollbar. When content fits, they are usually equal.
Yes. Like clientWidth, scrollWidth includes padding but not border, margin, or a vertical scrollbar.
Compare scrollWidth and clientWidth. If scrollWidth is greater than clientWidth, the content is overflowing horizontally.
Use element.scrollWidth - element.clientWidth. That is the portable maximum for scrollLeft.
Did you know?

scrollWidth is the horizontal twin of scrollHeight. Together with clientWidth and scrollLeft, they power most carousel and sideways-scroll UI logic.

Previous: scrollTopMax

Learn the non-standard Firefox maximum vertical scroll helper.

← scrollTopMax

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