W3cubDocs

/Web APIs

Element: scrollLeft property

The Element.scrollLeft property gets or sets the number of pixels that an element's content is scrolled from its left edge.

If the element's direction is rtl (right-to-left), then scrollLeft is 0 when the scrollbar is at its rightmost position (at the start of the scrolled content), and then increasingly negative as you scroll towards the end of the content.

It can be specified as any integer value. However:

  • If the element can't be scrolled (e.g., it has no overflow), scrollLeft is set to 0.
  • If specified as a value less than 0 (greater than 0 for right-to-left elements), scrollLeft is set to 0.
  • If specified as a value greater than the maximum that the content can be scrolled, scrollLeft is set to the maximum.

Warning: On systems using display scaling, scrollLeft may give you a decimal value.

Value

A number.

Examples

HTML

html

<div id="container">
  <div id="content">Click the button to slide right!</div>
</div>

<button id="slide" type="button">Slide right</button>

CSS

css

#container {
  width: 100px;
  height: 100px;
  border: 1px solid #ccc;
  overflow-x: scroll;
}

#content {
  width: 250px;
  background-color: #ccc;
}

JavaScript

js

const button = document.getElementById("slide");

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

Result

Specifications

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
scrollLeft 86
1–86For right-to-left elements, this property uses 0-100 (most left to most right) instead of negative values. See bug 721759.
86
12–86For right-to-left elements, this property uses 0-100 (most left to most right) instead of negative values. See bug 721759.
1
5For right-to-left elements, this property uses 100-0 (most left to most right) instead of negative values.
8 1 86
4.4–86For right-to-left elements, this property uses 0-100 (most left to most right) instead of negative values. See bug 721759.
86
18–86For right-to-left elements, this property uses 0-100 (most left to most right) instead of negative values. See bug 721759.
4 10.1 1 14.0
1.0–14.0For right-to-left elements, this property uses 0-100 (most left to most right) instead of negative values. See bug 721759.

See also

© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollLeft