Element: scrollLeft property
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
ThescrollLeft property of theElement interface 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, meaning that it isn't necessarily a whole number.
In this article
Value
A double-precision floating-point value indicating the number of pixels by which the element is currently scrolled horizontally from the origin, where a positive value means the element is scrolled to the right (to reveal more content to the right). If the element isn't scrolled at all left or right, thenscrollLeft is 0. If the document is not the active document, the returned value is 0. If the document is rendered on a subpixel-precise device, then the returned value is also subpixel-precise and may contain a decimal component.
It's possible forscrollLeft to be negative if the element can be scrolled to the left from the initial containing block. For example, if the element'sdirection isrtl (right-to-left) and content grows to the left, thenscrollLeft is0 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.
Safari responds to overscrolling by updatingscrollLeft beyond the maximum scroll position (unless the default "bounce" effect is disabled, such as by settingoverscroll-behavior tonone), while Chrome and Firefox do not.
ThescrollLeft property can be set, which causes the element to scroll to the specified horizontal position, in the same way as usingElement.scroll() withbehavior: "auto".
Examples
>HTML
<div> <div>Click the button to slide right!</div></div><button type="button">Slide right</button>CSS
#container { width: 100px; height: 100px; border: 1px solid #cccccc; overflow-x: scroll;}#content { width: 250px; background-color: #cccccc;}JavaScript
const button = document.getElementById("slide");button.onclick = () => { document.getElementById("container").scrollLeft += 20;};Result
Specifications
| Specification |
|---|
| CSSOM View Module> # dom-element-scrollleft> |