--- layout: docs title: Scrollspy description: Automatically update Bootstrap navigation or list group components based on scroll position to indicate which link is currently active in the viewport. group: components toc: true --- ## How it works Scrollspy has a few requirements to function properly: - It toggles `.active` class on anchor elements, so is handy to be used on a Bootstrap [nav component]({{< docsref "/components/navs-tabs" >}}) or [list group]({{< docsref "/components/list-group" >}}). - Anchors (``) are required and must point to an element with that `id`. When successfully implemented, your nav or list group will update accordingly, moving the `.active` class from one item to the next based on their associated targets. {{< callout >}} ### Scrollable containers and keyboard access If you're making a scrollable container (other than the ``), be sure to have a `height` set and `overflow-y: scroll;` applied to it—alongside a `tabindex="0"` to ensure keyboard access. {{< /callout >}} ## Example in navbar Scroll the area below the navbar and watch the active class change. The dropdown items will be highlighted as well.

First heading

This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.

Second heading

This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.

Third heading

This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.

Fourth heading

This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.

Fifth heading

This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.

```html

First heading

...

Second heading

...

Third heading

...

Fourth heading

...

Fifth heading

...

``` ## Example with nested nav Scrollspy also works with nested `.nav`s. 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.

Item 1

This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.

Item 1-1

This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.

Item 1-2

This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.

Item 2

This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.

Item 3

This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.

Item 3-1

This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.

Item 3-2

This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.

```html

Item 1

...

Item 1-1

...

Item 1-2

...

Item 2

...

Item 3

...

Item 3-1

...

Item 3-2

...

``` ## Example with list-group Scrollspy also works with `.list-group`s. Scroll the area next to the list group and watch the active class change.

Item 1

This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.

Item 2

This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.

Item 3

This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.

Item 4

This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.

```html
Item 1 Item 2 Item 3 Item 4

Item 1

...

Item 2

...

Item 3

...

Item 4

...

``` ## Example with simple anchors Scrollspy also works with `anchor` elements. Scroll the area and watch the active class change.

Item 1

This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.

Item 2

This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.

Item 3

This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.

Item 4

This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.

```html
Item 1 Item 2 Item 3 Item 4

Item 1

...

Item 2

...

Item 3

...

Item 4

...

``` ## Usage ### Via data attributes To easily add scrollspy behavior to your topbar navigation, add `data-bs-spy="scroll"` to the element you want to spy on (most typically this would be the ``). Then add the `data-bs-target` attribute with the ID or class of the parent element of any Bootstrap `.nav` component. ```html ... ... ``` ### Via JavaScript ```js var scrollSpy = new bootstrap.ScrollSpy(document.body, { target: '#navbar-example' }) ``` {{< callout danger >}} #### Resolvable ID targets required Navbar links must have resolvable id targets, else they are being ignored. For example, a `home` must correspond to something in the DOM like `
` {{< /callout >}} {{< callout info >}} #### Non-visible target elements ignored Target elements that are not visible will be ignored and their corresponding nav items will never be highlighted. {{< /callout >}} ### Options Options can be passed via data attributes or JavaScript. For data attributes, append the option name to `data-bs-`, as in `data-bs-root-margin=""`. {{< bs-table "table" >}} | Name | Type | Default | Description | | --- | --- | --- | --- | | `rootMargin` | string | `0px 0px -40%` | Intersection Observer [rootMargin](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/rootMargin) valid units, when calculating position of scroll. | | `target` | string \| jQuery object \| DOM element | | Specifies element to apply Scrollspy plugin. | {{< /bs-table >}} {{< callout warning >}} **Deprecated Options** Since 5.1 we [were using](https://getbootstrap.com/docs/5.1/components/scrollspy/#options) `offset` & `method` options, that are now deprecated, and replaced by `rootMargin`. To keep compatibility of them, we will continue parse, given `offset` to `rootMargin`, till **v6** {{< /callout >}} ### Methods {{< bs-table "table bs-js-table" >}} | Method | Description | | --- | --- | | `refresh` | When using scrollspy in conjunction with adding or removing of elements from the DOM, you'll need to call the refresh method. | | `dispose` | Destroys an element's scrollspy. (Removes stored data on the DOM element) | | `getInstance` | *Static* method which allows, to get the scrollspy instance associated with a DOM element | | `getOrCreateInstance` | *Static* method which allows, to get the scrollspy instance associated with a DOM element, or create a new one in case it wasn't initialised. | {{< /bs-table >}} Here's an example using the refresh method: ```js var dataSpyList = [].slice.call(document.querySelectorAll('[data-bs-spy="scroll"]')) dataSpyList.forEach(function (dataSpyEl) { bootstrap.ScrollSpy.getInstance(dataSpyEl) .refresh() }) ``` ### Events {{< bs-table "table bs-js-table" >}} | Event | Description | | --- | --- | | `activate.bs.scrollspy` | This event fires on the scroll element whenever a new item becomes activated by the scrollspy. | {{< /bs-table >}} ```js var firstScrollSpyEl = document.querySelector('[data-bs-spy="scroll"]') firstScrollSpyEl.addEventListener('activate.bs.scrollspy', function () { // do something... }) ```