Scrollspy is used track certain elements and update bootstrap navigation or list group components based on scroll position to indicate which link is currently active in the viewport.

Points to remember
  • It must be used on a Bootstrap's nav component or list group.
  • Scrollspy requires position: relative; on the element you're spying on, usually the <body>.
  • When you want spy on elements other than the <body>, you must set height and overflow-y: scroll;.
  • Anchors (<a>) are required and must point to an element with that id.

After implementation, your nav or list group will update accordingly, moving the .active class from one item to the next based on their associated targets.

How to use

Scrollspy can added via data attributes or via javascript.

Create a bootstrap's nav component. On the body element add the data attribute data-spy="scroll" and set data-target="<target-id>" where <target-id> is the id or the class name of the navigation bar. The scrollable elements must match the ID of the links inside the navbar's list items (<div id="section-one"> matches <a href="#section-one">). The dropdown items will be highlighted as well.

Basic Navbar

When you scroll the area below the navbar, the active class change. The dropdown items will be highlighted as well.


Add data-offset attribute which is used to set pixels to offset from top when calculating position of scroll.

Scrollspy on Nested nav

Scrollspy also works with nested .navs. If a nested .nav is .active, its parents will also be .active. Scroll the area next to the navbar and watch the active class change.

Scroll spy with list-group

Scrollspy also works with .list-groups. Scroll the area next to the list group and watch the active class change.

Via data attributes

To easily add scrollspy behavior to your topbar navigation, add data-spy="scroll" to the element you want to spy on (most typically this would be the <body>). Then add the data-target attribute with the ID or class of the parent element of any Bootstrap .nav component.

body {
  position: relative;
<body data-spy="scroll" data-target="#navbar-example">
  <div id="navbar-example">
    <ul class="nav nav-tabs" role="tablist">
Via JavaScript

After adding position: relative; in your CSS, call the scrollspy via JavaScript.

$('body').scrollspy({ target: '#navbar-example' })

Resolvable ID targets required

Navbar links must have resolvable id targets. For example, a <a href="#home">home</a> must correspond to something in the DOM like <div id="home"></div>.


Non-:visible target elements ignored

Target elements that are not :visible according to jQuery will be ignored and their corresponding nav items will never be highlighted.

JavaScript Behaviors



When using scrollspy in conjunction with adding or removing of elements from the DOM, you’ll need to call the refresh method like so:

$('[data-spy="scroll"]').each(function () {
  var $spy = $(this).scrollspy('refresh')


Destroys an element's scrollspy.


Options can be passed via data attributes or JavaScript. For data attributes, append the option name to data-, as in data-offset="".

Name Type Default Description
offset number 10 Pixels to offset from top when calculating position of scroll.
Event Type Description This event fires on the scroll element whenever a new item becomes activated by the scrollspy.
$('[data-spy="scroll"]').on('', function () {
  // do something…
Related Tutorial
Follow Us #
Contents +