The .scrollLeft() method gets or sets the horizontal scroll offset of matched elements as a unitless pixel Integer — ideal for carousels, tab bars, and wide tables. This tutorial covers getter and setter overloads, comparisons with .scrollTop(), native element.scrollLeft, and .css(), scroll event handling, read-modify-write button patterns, and animated horizontal scrolling.
01
Getter
.scrollLeft() → Integer
02
Setter
Number (pixels)
03
vs scrollTop
Horizontal twin
04
Animate
{ scrollLeft }
05
Overflow
auto / scroll
06
Since 1.2.6
Core API
Fundamentals
Introduction
After learning to position elements with .position() and .offset(), you often need to control what portion of wide content is visible inside a scrollable container. jQuery’s .scrollLeft() returns a plain integer you can read, increment, or pass directly to .animate() — without parsing CSS strings.
Available since jQuery 1.2.6, .scrollLeft() works as both a getter (no arguments) and a setter (pixel value). The getter reads the horizontal scroll offset of the first matched element — how many pixels of content are hidden to the left. The setter scrolls every element in the set to that position. It is the horizontal counterpart of .scrollTop().
Concept
Understanding the .scrollLeft() Method
Given a jQuery object, .scrollLeft() either returns the current horizontal scroll offset of the first matched element as a unitless pixel Integer (getter), or sets the scroll position on every matched element (setter). The value represents pixels of content hidden to the left of the visible area — 0 when flush left or when the element is not scrollable.
Think of .scrollLeft() as a horizontal window into overflowing content. .scrollTop() is the vertical twin. Native element.scrollLeft does the same at the DOM level; jQuery adds collection semantics and chaining. Use .css() for styling overflow — not for reading the current scroll offset.
💡
Beginner Tip
var x = $("div").scrollLeft() gives you an Integer like 120 — pixels scrolled from the left edge. For smooth motion, use $("div").animate({ scrollLeft: 300 }, 400) instead of jumping with the setter.
Foundation
📝 Syntax
jQuery .scrollLeft() has two argument forms — one getter and one setter:
Get — no arguments (since 1.2.6)
jQuery
.scrollLeft() // -> Integer
Returns the horizontal scroll offset of the first matched element as a unitless pixel Integer.
Reports pixels hidden to the left — 0 at the left edge or when not scrollable.
Native equivalent: element.scrollLeft.
Set — Number (since 1.2.6)
jQuery
.scrollLeft( value ) // -> jQuery
value — horizontal scroll position in pixels (e.g. 300).
Applied to every matched element in the set.
Returns the original jQuery object for chaining — e.g. .scrollLeft(300).css("borderColor", "green").
Animate — smooth scroll (since 1.2.6)
jQuery
.animate({ scrollLeft: value }, duration )
Tweens the scroll offset over time — ideal for tab bars and carousels.
Read current position with .scrollLeft(), compute target, then animate.
Returns the jQuery object; use a completion callback for post-scroll actions.
Return value
Getter — unitless Integer (pixels). May be 0 when not scrollable.
Setter — the original jQuery object for chaining.
Important note
⚠️
Hidden elements
.scrollLeft() does not work on display:none elements — jQuery cannot read or set a meaningful scroll offset on hidden nodes. Show the element before measuring or scrolling.
Official jQuery API example
jQuery
var p = $( "p" ).first();
$( "p" ).last().text( "scrollLeft:" + p.scrollLeft() );
Cheat Sheet
⚡ Quick Reference
Goal
Code
Read horizontal scroll (Integer)
$("div").scrollLeft()
Scroll to pixel position
$("div").scrollLeft(300)
Smooth horizontal scroll
$("div").animate({ scrollLeft: 300 }, 400)
Scroll by delta (read-modify-write)
$el.scrollLeft($el.scrollLeft() + 50)
Vertical counterpart
$("div").scrollTop()
Native equivalent
element.scrollLeft
Listen for scroll changes
$("div").on("scroll", fn)
Enable scrolling (CSS)
$("div").css("overflow-x", "auto")
Compare
📋 .scrollLeft() vs .scrollTop() vs native scrollLeft vs .css()
Four ways to work with scroll — horizontal offset, vertical twin, native DOM, and CSS styling.
Vertical counterpart — same getter/setter/animate pattern
native
el.scrollLeft
DOM property on a single element — no jQuery chaining
.css()
overflow-x
Style overflow — does not read current scroll offset
Hands-On
Examples Gallery
Examples 1–2 follow the official jQuery API documentation. Examples 3–5 handle scroll events, read-modify-write buttons, and animated tab bars. Use DevTools or the Try-it links to run each snippet.
📚 Getting Started
Official jQuery demos for reading and setting horizontal scroll.
Example 1 — Official Demo: Get Paragraph scrollLeft
Read the horizontal scroll offset of the first <p> inside a scrollable container and display it in the last paragraph.
jQuery
var p = $( "p" ).first();
$( "p" ).last().text( "scrollLeft:" + p.scrollLeft() );
scrollLeft:0 (at left edge on load)
After scrolling right manually -> scrollLeft:120 (example)
Getter returns Integer from first matched element only
How It Works
No-argument .scrollLeft() returns a unitless pixel Integer — how many pixels of content are hidden to the left. On load the value is typically 0 when the container is at the left edge.
Example 2 — Official Demo: Set scrollLeft to 300
Click a button to scroll a wide demo div horizontally to pixel position 300.
Click button -> div.demo jumps to scrollLeft 300
300px of content hidden to the left becomes visible on the right
Setter applies to every matched div.demo element
How It Works
.scrollLeft(300) sets the horizontal scroll position instantly. The setter returns the jQuery object for chaining. The container must have overflow-x: auto or scroll and content wider than the viewport.
📈 Practical Patterns
Scroll events, button controls, and animated tab navigation.
Example 3 — Display scrollLeft on Scroll Event
Attach a scroll handler and show the live horizontal offset as the user scrolls.
On scroll -> "scrollLeft: 0" at left edge
Drag scrollbar right -> "scrollLeft: 85" (updates live)
At far right -> max scrollLeft (content width minus container width)
Use scroll event + .scrollLeft() getter for progress indicators
How It Works
The native scroll event fires whenever the scroll offset changes. Inside the handler, $(this).scrollLeft() reads the current horizontal position — the foundation for scroll progress bars and snap detection.
Example 4 — Left/Right Buttons: Read-Modify-Write scrollLeft
Increment or decrement scroll position by 80 pixels per button click.
Right click -> scrollLeft increases by 80px
Left click -> scrollLeft decreases by 80px
At left edge -> scrollLeft stays 0 (cannot go negative)
Read current .scrollLeft(), add/subtract step, write back with setter
How It Works
This read-modify-write pattern is the standard way to scroll by a fixed step: getter returns current offset, you adjust the number, setter applies the new position. jQuery clamps at 0 on the left — it will not scroll into negative territory.
Example 5 — Animate Tab Bar: { scrollLeft: target }
Click a tab to smoothly scroll the tab bar so the active tab comes into view.
Click off-screen tab -> tab bar animates horizontally over 300ms
Active tab scrolls into view with 20px left padding
.scrollLeft() reads current offset; .position().left gives tab local X
Smooth carousel and tab-bar UX without CSS scroll-behavior
How It Works
Combine .position().left (tab offset inside the bar) with the bar’s current .scrollLeft() to compute the target scroll position, then .animate({ scrollLeft: target }) for smooth motion. This is a common pattern for overflowing horizontal tab lists.
Applications
🚀 Common Use Cases
Image carousels — .scrollLeft(current + slideWidth) or .animate({ scrollLeft: ... }) to advance slides.
Overflowing tab bars — scroll the active tab into view on click (see Example 5).
Wide data tables — sync a sticky header with body .scrollLeft() on horizontal scroll.
Custom prev/next controls — read-modify-write with left/right buttons (see Example 4).
Scroll progress indicators — scroll event + .scrollLeft() to update a horizontal progress bar.
Timeline and kanban boards — programmatic scroll to a column: $board.scrollLeft(columnOffset).
🧠 How .scrollLeft() Reads and Writes Scroll Offset
1
Match container
jQuery object holds scrollable DOM elements with overflowing content.
Input
2
Detect mode
No args → getter. Number arg → setter on each element.
.scrollLeft() has been part of jQuery since 1.2.6+. It wraps the native scrollLeft property with no browser-specific quirks beyond jQuery itself. Animation via .animate({ scrollLeft }) works wherever jQuery animation is supported.
✓ jQuery 1.2.6+
jQuery .scrollLeft()
Supported in jQuery 1.x, 2.x, and 3.x across all modern browsers and IE with supported jQuery builds. Native equivalent: element.scrollLeft — jQuery adds collection semantics, getter/setter overloads, and animate integration.
100%With jQuery loaded
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
.scrollLeft()Universal
Bottom line: Safe in any jQuery project. Use .scrollLeft() for horizontal scroll math; use .css('overflow-x') to enable scrolling.
Wrap Up
Conclusion
The jQuery .scrollLeft() method gets or sets horizontal scroll offset as a unitless pixel Integer — the go-to helper when you need to read, jump, or animate horizontal scroll inside overflow containers. It complements .scrollTop() for vertical scroll and native element.scrollLeft for single-element access.
Remember the official demo: var p = $( "p" ).first(); $( "p" ).last().text( "scrollLeft:" + p.scrollLeft() ); reads the offset, and $( "div.demo" ).scrollLeft( 300 ) jumps to a position. Pair .scrollLeft() with scroll events for live feedback, read-modify-write for buttons, and .animate({ scrollLeft: target }) for smooth tab bars.
Use .scrollLeft() when you need an Integer for scroll math or .animate()
Ensure overflow-x: auto or scroll on containers with wide content
Use read-modify-write for step buttons: $el.scrollLeft($el.scrollLeft() + step)
Pair scroll events with the getter for live progress indicators
Use .animate({ scrollLeft: target }) for smooth tab bar and carousel motion
❌ Don’t
Call .scrollLeft() on display:none elements — it will not work
Assume 0 always means “at left edge” — non-scrollable containers also return 0
Use .css() to read scroll position — it cannot report current offset
Expect the getter to read all matched elements — only the first
Forget .scrollTop() when you need vertical scroll — they are independent axes
Summary
Key Takeaways
Knowledge Unlocked
Six things to remember about .scrollLeft()
Horizontal scroll — ready for math and animation.
6
Core concepts
#01
Getter
Integer
Read
=02
Setter
Pixels
Write
Y03
scrollTop
Vertical
Compare
fx04
Animate
Smooth
Motion
ovf05
Overflow
Required
CSS
hide06
Hidden
No-op
Caveat
❓ Frequently Asked Questions
Calling .scrollLeft() with no arguments is the getter — it returns the current horizontal scroll offset of the first matched element as a unitless Integer (pixels hidden to the left). Calling .scrollLeft(value) is the setter — it scrolls every matched element to that horizontal position and returns the original jQuery object for chaining. The getter reads only the first element; the setter applies to all elements in the set.
.scrollLeft() measures and sets horizontal scroll — how many pixels of content are hidden to the left inside an overflow container. .scrollTop() is the vertical counterpart — pixels hidden above the visible area. Both return Integers, work since jQuery 1.2.6, fail on hidden elements, and support .animate({ scrollLeft: value }) or .animate({ scrollTop: value }). Use .scrollLeft() for carousels, tab bars, and wide tables; use .scrollTop() for page scroll and vertical panels.
Elements with display:none (or otherwise not rendered) have no scrollable layout box — the browser cannot compute or apply a meaningful scroll offset. jQuery delegates to the native scrollLeft property, which is unreliable or zero on hidden nodes. Show the element first, or measure after making it visible, before reading or setting .scrollLeft(). The same limitation applies to .scrollTop().
A getter result of 0 means either the element is scrolled all the way to the left (no content hidden on the left side) or the element is not horizontally scrollable. Non-scrollable containers — those without overflow: auto or overflow: scroll and whose content fits within the visible width — always report 0 because there is nothing to scroll. Do not assume 0 always means 'at the left edge'; check overflow and content width first.
Yes — pass scrollLeft as an animation property: $container.animate({ scrollLeft: target }, duration). jQuery tweens the scroll offset smoothly, which is ideal for tab bars, image carousels, and horizontal snap navigation. Read the current value with .scrollLeft(), compute a target, then animate. For instant jumps without transition, use the setter .scrollLeft(value) directly.
For meaningful non-zero values, the element must be scrollable — typically overflow-x: auto, overflow-x: scroll, or overflow: auto/scroll on a container whose content is wider than its visible width. Without that, .scrollLeft() getter returns 0 and the setter has no visible effect. .css() cannot replace .scrollLeft() for reading scroll position; use .scrollLeft() or native element.scrollLeft for the current offset.
Did you know?
jQuery’s .scrollLeft() has existed since version 1.2.6 — the horizontal twin of .scrollTop(). Both wrap the native DOM scroll properties so you can call them on jQuery collections and chain setters. Unlike dimension methods such as .width(), scroll getters return an Integer (not fractional) in most browsers. You can animate either axis with .animate({ scrollLeft: n }) or .animate({ scrollTop: n }) — a pattern that predates CSS scroll-behavior: smooth by many years. For wide tables, syncing header and body scroll often means copying $body.scrollLeft() to $header.scrollLeft() inside a shared scroll handler.