Web/API/Element/scrollLeft

From Get docs


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.

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


Syntax

Getting the value

// Get the number of pixels scrolled
var sLeft = element.scrollLeft;

sLeft is an integer representing the number of pixels that element has been scrolled from the left edge.

Setting the value

// Set the number of pixels scrolled
element.scrollLeft = 10;

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

Example

HTML

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

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

CSS

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

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

JavaScript

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

button.onclick = function () {
  document.getElementById('container').scrollLeft += 20;
};

Result

Specifications

Specification Status Comment
CSS Object Model (CSSOM) View ModuleThe definition of 'scrollLeft' in that specification. Working Draft

Browser compatibility

Update compatibility data on GitHub

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
scrollLeft

Chrome Full support 43

Notes'

Full support 43

Notes'

Notes' For right-to-left elements, this property uses 0-100 (most left to most right) instead of negative values. See bug 721759.

Edge Full support 12

Notes'

Full support 12

Notes'

Notes' From Edge 79, for right-to-left elements, this property uses 0-100 (most left to most right) instead of negative values. See bug 721759. Notes' Before Edge 79, for right-to-left elements, this property uses 100-0 (most left to most right) instead of negative values.

Firefox

Full support 1

IE Full support 8

Notes'

Full support 8

Notes'

Notes' For right-to-left elements, this property uses 100-0 (most left to most right) instead of negative values.

Opera

Full support 8

Safari

Full support 6

WebView Android Full support 43

Notes'

Full support 43

Notes'

Notes' For right-to-left elements, this property uses 0-100 (most left to most right) instead of negative values. See bug 721759.

Chrome Android Full support 43

Notes'

Full support 43

Notes'

Notes' For right-to-left elements, this property uses 0-100 (most left to most right) instead of negative values. See bug 721759.

Firefox Android

Full support 4

Opera Android

Full support 10.1

Safari iOS

Full support 6

Samsung Internet Android

Full support 4.0

Legend

Full support  
Full support
See implementation notes.'
See implementation notes.


See also