jQuery .scrollLeft() Method

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Horizontal scroll

What You’ll Learn

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

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().

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.

📝 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() );

⚡ Quick Reference

GoalCode
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 equivalentelement.scrollLeft
Listen for scroll changes$("div").on("scroll", fn)
Enable scrolling (CSS)$("div").css("overflow-x", "auto")

📋 .scrollLeft() vs .scrollTop() vs native scrollLeft vs .css()

Four ways to work with scroll — horizontal offset, vertical twin, native DOM, and CSS styling.

.scrollLeft()
scroll X

Get/set horizontal scroll — returns unitless Integer (getter)

.scrollTop()
scroll Y

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

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() );
Try It Yourself

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.

jQuery
$( "button" ).on( "click", function() {
  $( "div.demo" ).scrollLeft( 300 );
});
Try It Yourself

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.

jQuery
$( "#panel" ).on( "scroll", function() {
  $( "#out" ).text( "scrollLeft: " + $( this ).scrollLeft() );
});
Try It Yourself

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.

jQuery
var step = 80;
$( "#left" ).on( "click", function() {
  var $el = $( "#track" );
  $el.scrollLeft( $el.scrollLeft() - step );
});
$( "#right" ).on( "click", function() {
  var $el = $( "#track" );
  $el.scrollLeft( $el.scrollLeft() + step );
});
Try It Yourself

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.

jQuery
$( ".tab" ).on( "click", function() {
  var $bar = $( "#tabbar" );
  var $tab = $( this );
  var target = $tab.position().left + $bar.scrollLeft() - 20;
  $bar.animate({ scrollLeft: target }, 300 );
  $( ".tab" ).removeClass( "active" );
  $tab.addClass( "active" );
});
Try It Yourself

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.

🚀 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 indicatorsscroll 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.

Route
3

Read or write

Getter reads native scrollLeft. Setter assigns pixel offset. Requires visible, scrollable layout.

DOM
4

Return result

Getter → unitless pixel Integer. Setter → same jQuery object for chaining.

📝 Notes

  • Available since jQuery 1.2.6 — horizontal scroll counterpart of .scrollTop().
  • Getter returns a unitless pixel Integer from the first matched element only.
  • Setter applies to all matched elements and returns the jQuery object for chaining.
  • Requires overflow-x: auto, scroll, or content wider than the container for meaningful values.
  • Does not work on hidden (display:none) elements.
  • 0 means at the left edge or not scrollable — check overflow before assuming.
  • Animatable via .animate({ scrollLeft: value }, duration).
  • Native equivalent: element.scrollLeft — jQuery adds collection semantics.

Browser Support

.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 Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All 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.

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.

💡 Best Practices

✅ Do

  • 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

Key Takeaways

Knowledge Unlocked

Six things to remember about .scrollLeft()

Horizontal scroll — ready for math and animation.

6
Core concepts
= 02

Setter

Pixels

Write
Y 03

scrollTop

Vertical

Compare
fx 04

Animate

Smooth

Motion
ovf 05

Overflow

Required

CSS
hide 06

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.

Next: .scrollTop()

Learn how to read and set vertical scroll position — including page scroll with $(window).

.scrollTop() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful