THE WORLD'S LARGEST WEB DEVELOPER SITE

HTML DOM scrollLeft Property

❮ Element Object

Example

Get the number of pixels the content of a <div> element is scrolled horizontally and vertically:

var elmnt = document.getElementById("myDIV");
var x = elmnt.scrollLeft;
var y = elmnt.scrollTop;
Try it Yourself »

More "Try it Yourself" examples below.


Definition and Usage

The scrollLeft property sets or returns the number of pixels an element's content is scrolled horizontally.

Tip: Use the scrollTop property to set or return the number of pixels an element's content is scrolled vertically.

Tip: To add scrollbars to an element, use the CSS overflow property.


Browser Support

Property
scrollLeft Yes Yes Yes Yes Yes

Syntax

Return the scrollLeft property:

element.scrollLeft

Set the scrollLeft property:

element.scrollLeft = pixels

Property Values

Value Description
pixels Specifies the number of pixels the element's content is scrolled horizontally.

Special notes:
  • If the number is a negative value, the number is set to "0"
  • If the element cannot be scrolled, the number is set to "0"
  • If the number is greater than the maximum allowed scroll amount, the number is set to the maximum number


Technical Details

Return Value: A Number, representing the number of pixels that the element's content has been scrolled horizontally

More Examples

Example

Scroll the contents of a <div> element TO 50 pixels horizontally and 10 pixels vertically:

var elmnt = document.getElementById("myDIV");
elmnt.scrollLeft = 50;
elmnt.scrollTop = 10;
Try it Yourself »

Example

Scroll the contents of a <div> element BY 50 pixels horizontally and 10 pixels vertically:

var elmnt = document.getElementById("myDIV");
elmnt.scrollLeft += 50;
elmnt.scrollTop += 10;
Try it Yourself »

Example

Scroll the contents of <body> by 30 pixels horizontally and 10 pixels vertically:

var body = document.body; // Safari
var html = document.documentElement; // Chrome, Firefox, IE and Opera places the overflow at the <html> level, unless else is specified. Therefore, we use the documentElement property for these browsers
body.scrollLeft += 30;
body.scrollTop += 10;
html.scrollLeft += 30;
html.scrollTop += 10;
Try it Yourself »

❮ Element Object