JavaScript Element scrollLeft Property

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

What You’ll Learn

Element.scrollLeft is an instance property that gets or sets how far an element’s content is scrolled horizontally. Learn how to read the current position, move content to the right, assign a specific value, and understand subpixel and RTL behavior—with five examples and try-it labs.

01

Kind

Instance property

02

Access

Read and set

03

Type

Number (pixels)

04

Direction

Horizontal scroll

05

Status

Baseline widely

06

Pairs with

scrollWidth, clientWidth

Introduction

When a box has more content than fits horizontally, the user can scroll sideways. The scrollLeft property tells you how many pixels the content has moved from the left edge, and you can also change that value from JavaScript.

It is useful for image carousels, tab strips, horizontally scrollable tables, and any UI where you need to move content left or right programmatically.

JavaScript
const box = document.getElementById("container");
console.log(box.scrollLeft); // 0 at the start
box.scrollLeft += 20;
💡
Beginner tip

Unlike scrollHeight, which is read-only, scrollLeft can be both read and written to control horizontal scrolling.

Understanding the Property

MDN: the scrollLeft property gets or sets the number of pixels by which an element’s content is scrolled from its left edge. This value is subpixel precise in modern browsers.

  • Readable and writable — inspect or control horizontal scroll position.
  • Starts at 0 — when the element has not been scrolled horizontally.
  • Positive means scrolled right — revealing more content to the right.
  • Can be decimal — subpixel precision is possible on modern devices.

📝 Syntax

JavaScript
element.scrollLeft
element.scrollLeft = 100
element.scrollLeft += 20

Value

A double-precision floating-point number representing horizontal scroll offset in CSS pixels.

ItemDetail
TypeNumber (pixels)
AccessReadable and writable
Default0 when not scrolled
PrecisionCan include decimal subpixels
RelatedscrollWidth, clientWidth, scroll()
⚠️
RTL note

In right-to-left layouts, scrollLeft behavior can differ and values may become negative as you scroll. Test carefully if your UI supports RTL.

📋 MDN Slide-Right Example Shape

MDN increments scrollLeft when a button is clicked to move content to the right:

JavaScript
button.onclick = () => {
  document.getElementById("container").scrollLeft += 20;
};

Related learning: scrollWidth, clientWidth, and element.scroll().

⚡ Quick Reference

GoalCode / note
Read horizontal scrollel.scrollLeft
Scroll right by 20pxel.scrollLeft += 20
Jump to positionel.scrollLeft = 100
Reset to startel.scrollLeft = 0
Check if movedel.scrollLeft > 0
MDN statusBaseline Widely available

🔍 At a Glance

Four facts about Element.scrollLeft.

Kind
get/set

Instance

Type
number

Pixels

Moves
horizontal

Left/right

Baseline
widely

Jul 2015+

Examples Gallery

Examples follow MDN Element: scrollLeft. Labs use horizontally scrollable boxes so you can read and change the scroll position.

📚 Getting Started

Start by reading the current horizontal scroll position.

Example 1 — Read scrollLeft

Before scrolling, the value is usually 0.

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

How It Works

At the start of a horizontally scrollable box, scrollLeft is typically zero.

📈 Move, Set & Reset

Use MDN’s slide pattern, jump to a position, and reset scrolling.

Example 2 — Slide Right on Click

MDN increases scrollLeft by 20 pixels when a button is clicked.

JavaScript
button.onclick = () => {
  document.getElementById("container").scrollLeft += 20;
};
Try It Yourself

How It Works

Adding to scrollLeft moves the visible area to the right and reveals more content.

Example 3 — Set a Specific Position

Assign a number to jump directly to that horizontal scroll offset.

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

How It Works

Setting scrollLeft scrolls immediately, similar to element.scroll() with behavior: "auto".

Example 4 — Reset to the Start

Return the box to the leftmost position.

JavaScript
const box = document.getElementById("container");
box.scrollLeft = 100;
box.scrollLeft = 0;
console.log(box.scrollLeft);
Try It Yourself

How It Works

Assigning 0 sends the scroll position back to the beginning.

Example 5 — Support Snapshot

Feature-detect and remember the read/write behavior.

JavaScript
console.log({
  supported: "scrollLeft" in Element.prototype,
  access: "read and write",
  precision: "subpixel possible",
  status: "Baseline Widely available (MDN)"
});
Try It Yourself

How It Works

Safe to use widely. It is one of the core properties for controlling horizontal scrolling in JavaScript.

🚀 Common Use Cases

  • Moving a carousel or image strip to the next item.
  • Scrolling a tab bar so the active tab becomes visible.
  • Building custom previous/next buttons for horizontal content.
  • Resetting a horizontally scrollable panel back to the start.
  • Tracking how far the user has scrolled sideways in a container.

🔧 How It Works

1

Element has wider content

Overflow creates a horizontal scrollable area.

Layout
2

Browser tracks offset

scrollLeft stores how far content moved from the left edge.

State
3

You read or write it

Reading inspects position; writing scrolls the element.

API
4

Content moves left or right

That is how buttons, carousels, and custom scroll controls move horizontally.

📝 Notes

  • Not Deprecated, Experimental, or Non-standard on MDN (Baseline Widely available).
  • Values can be decimal because of subpixel precision.
  • In RTL layouts, behavior may differ and values can become negative.
  • Related: scrollWidth, clientWidth, scrollHeight, scroll(), JavaScript hub.

Browser Support

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

Readable and writable — returns or sets horizontal scroll offset in pixels.

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

Bottom line: Use scrollLeft to read or control horizontal scrolling. Increase it to move right, decrease it to move left, and remember subpixel and RTL differences.

Conclusion

scrollLeft is the horizontal scroll position property. You can read it to inspect where a container is scrolled, or write to it to move content left and right programmatically.

Continue with scrollHeight, role, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Use scrollLeft += value for step-by-step horizontal movement
  • Reset with scrollLeft = 0 when returning to the start
  • Test on real scrollable containers, not only the page
  • Consider subpixel values when comparing positions
  • Pair with scrollWidth and clientWidth for bounds logic

❌ Don’t

  • Assume the value is always a whole number
  • Forget RTL differences in international layouts
  • Confuse scrollLeft with scrollWidth
  • Expect it to work on non-scrollable elements
  • Ignore user scroll position when building custom controls

Key Takeaways

Knowledge Unlocked

Five things to remember about scrollLeft

Readable and writable horizontal scroll offset in pixels.

5
Core concepts
📝02

number

pixels

Type
🔍03

Horizontal

scroll offset

Use
04

Baseline

widely available

Status
🎯05

Subpixel

values possible

Tip

❓ Frequently Asked Questions

It gets or sets how many pixels the element's content is scrolled horizontally from its left edge. A positive value means the content has moved to the right.
No. MDN marks Element.scrollLeft as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
It is usually 0 when the element has not been scrolled horizontally. In some RTL layouts, behavior can differ and values may become negative as you scroll.
Yes. Modern browsers can return subpixel-precise floating-point values, so scrollLeft is not always a whole number.
Increase scrollLeft, for example: element.scrollLeft += 20. You can also assign a specific value: element.scrollLeft = 100.
scrollLeft is the current horizontal scroll position. scrollWidth is the total width of the scrollable content.
Did you know?

MDN’s slide-right demo works by simply doing container.scrollLeft += 20 each time the button is clicked.

Previous: scrollHeight

Learn how Element.scrollHeight measures full vertical content height.

← scrollHeight

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