Bootstrap 5 Scrollspy
Scrollspy watches a scrolling area and automatically highlights the navigation link for the section currently in view. You've seen it on documentation sites: as you scroll, the "On this page" menu follows along.
How it works
Scrollspy needs three things:
- A scrollable element — either the page (
<body>) or a box with a fixedheightandoverflow-y: auto. - A navigation (nav, list group or plain links) whose links point to section ids:
href="#intro". - The attributes
data-bs-spy="scroll"anddata-bs-target="#navId"on the scrollable element (plustabindex="0"so keyboard users can scroll it).
When a section scrolls into view, Bootstrap adds .active to the matching link. Since 5.2 Scrollspy uses the browser's IntersectionObserver, so it's cheap and accurate.
Scrollspy with a nav
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
.spy-box { height: 180px; overflow-y: auto; position: relative; }
.spy-box section { min-height: 160px; }
</style>
</head>
<body class="p-3">
<nav id="topnav" class="nav nav-pills mb-2">
<a class="nav-link" href="#s-html">HTML</a>
<a class="nav-link" href="#s-css">CSS</a>
<a class="nav-link" href="#s-js">JS</a>
</nav>
<div class="spy-box border rounded p-3" data-bs-spy="scroll" data-bs-target="#topnav" data-bs-smooth-scroll="true" tabindex="0">
<section id="s-html"><h5>HTML</h5><p>Scroll down inside this box and watch the pills above. ↓</p></section>
<section id="s-css"><h5>CSS</h5><p>Now the CSS pill is active.</p></section>
<section id="s-js"><h5>JS</h5><p>And finally JS.</p></section>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>data-bs-smooth-scroll="true" makes a click on a link scroll smoothly to its section instead of jumping.
Scrollspy with a list group
Any element containing anchors works as the target. A vertical list group beside the content gives the classic documentation layout.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-3">
<div class="row">
<div class="col-4">
<div id="toc" class="list-group list-group-flush small">
<a class="list-group-item list-group-item-action" href="#step1">1. Install</a>
<a class="list-group-item list-group-item-action" href="#step2">2. Configure</a>
<a class="list-group-item list-group-item-action" href="#step3">3. Build</a>
<a class="list-group-item list-group-item-action" href="#step4">4. Deploy</a>
</div>
</div>
<div class="col-8">
<div data-bs-spy="scroll" data-bs-target="#toc" data-bs-smooth-scroll="true" tabindex="0"
class="border rounded p-3" style="height: 200px; overflow-y: auto; position: relative;">
<h6 id="step1">1. Install</h6><p>Run the installer and accept the defaults.</p><p class="pb-5">…</p>
<h6 id="step2">2. Configure</h6><p>Edit the config file with your settings.</p><p class="pb-5">…</p>
<h6 id="step3">3. Build</h6><p>Run the build command.</p><p class="pb-5">…</p>
<h6 id="step4">4. Deploy</h6><p>Upload the output folder.</p><p class="pb-5">…</p>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>Options and events
| Attribute / option | Default | Meaning |
|---|---|---|
data-bs-spy="scroll" |
— | Turns on scrollspy for this element |
data-bs-target / target |
— | Selector of the nav containing the links |
data-bs-smooth-scroll / smoothScroll |
false |
Smooth scrolling when a link is clicked |
data-bs-root-margin / rootMargin |
'0px 0px -25%' |
IntersectionObserver margin (when a section counts as "in view") |
data-bs-threshold / threshold |
[0.1, 0.5, 1] |
Visibility ratios checked |
When the active link changes, Bootstrap fires activate.bs.scrollspy on the scrollable element. The event's relatedTarget is the newly active link.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-3">
<nav id="days" class="nav nav-underline mb-2">
<a class="nav-link" href="#mon">Mon</a>
<a class="nav-link" href="#tue">Tue</a>
<a class="nav-link" href="#wed">Wed</a>
</nav>
<div id="schedule" class="border rounded p-3" style="height: 150px; overflow-y: auto; position: relative;" tabindex="0">
<div id="mon" style="min-height: 140px;">🏋️ Monday: gym</div>
<div id="tue" style="min-height: 140px;">🏊 Tuesday: swimming</div>
<div id="wed" style="min-height: 140px;">🧘 Wednesday: yoga</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<script>
const box = document.getElementById('schedule');
new bootstrap.ScrollSpy(box, { target: '#days' });
box.addEventListener('activate.bs.scrollspy', (e) => console.log('active:', e.relatedTarget.textContent));
</script>
</body>
</html>This example creates the ScrollSpy in JavaScript instead of using data-bs-spy.
Note: Each link's
hrefmust match an elementidthat really exists and is visible. Hidden sections (display: none) are ignored, and an anchor pointing to a missing id is skipped.
Tip: If you add or remove sections dynamically, call
bootstrap.ScrollSpy.getInstance(el).refresh()so Scrollspy re-reads the targets.
Test yourself
Which attribute turns an element into a scrollspy area?
- data-bs-toggle="scrollspy"
- data-bs-spy="scroll"
- data-bs-scroll="spy"
- data-spy="true"
What must a scrollable box (not the body) have for scrollspy to work?
- A fixed height and overflow-y: auto
- The .scrollspy class
- display: flex
- A data-bs-height attribute
Which class does scrollspy add to the link of the visible section?
- .current
- .visible
- .active
- .show