ScrollSpy helps you highlight menu items automatically as you scroll through a page. It makes navigation easier by showing where you are on the page.
ScrollSpy component in Bootsrap
<body data-bs-spy="scroll" data-bs-target="#navbar-example" data-bs-offset="0" tabindex="0"> <!-- content with sections --> </body>
Use data-bs-spy="scroll" on the container you want to watch for scrolling.
data-bs-target points to the navigation menu that will update.
<body data-bs-spy="scroll" data-bs-target="#navbar-example" data-bs-offset="0" tabindex="0"> <!-- page content --> </body>
<nav id="navbar-example" class="navbar"> <ul class="nav nav-pills"> <li class="nav-item"> <a class="nav-link" href="#section1">Section 1</a> </li> <li class="nav-item"> <a class="nav-link" href="#section2">Section 2</a> </li> </ul> </nav>
<div data-bs-spy="scroll" data-bs-target="#list-example" data-bs-offset="0" tabindex="0" style="position: relative; height: 200px; overflow: auto;"> <ul class="list-group"> <li id="item-1" class="list-group-item">Item 1</li> <li id="item-2" class="list-group-item">Item 2</li> <li id="item-3" class="list-group-item">Item 3</li> </ul> </div>
This example shows a fixed top navigation bar with links to three sections. As you scroll, the active link in the menu updates automatically.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Bootstrap ScrollSpy Example</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet" /> <style> body { position: relative; } section { height: 500px; padding-top: 60px; border-bottom: 1px solid #ccc; } </style> </head> <body data-bs-spy="scroll" data-bs-target="#navbar-example" data-bs-offset="50" tabindex="0"> <nav id="navbar-example" class="navbar navbar-light bg-light fixed-top"> <ul class="nav nav-pills"> <li class="nav-item"> <a class="nav-link" href="#section1">Section 1</a> </li> <li class="nav-item"> <a class="nav-link" href="#section2">Section 2</a> </li> <li class="nav-item"> <a class="nav-link" href="#section3">Section 3</a> </li> </ul> </nav> <main class="container mt-5 pt-5"> <section id="section1"> <h2>Section 1</h2> <p>This is the first section of the page.</p> </section> <section id="section2"> <h2>Section 2</h2> <p>This is the second section of the page.</p> </section> <section id="section3"> <h2>Section 3</h2> <p>This is the third section of the page.</p> </section> </main> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script> </body> </html>
Make sure the container with data-bs-spy="scroll" has position: relative or similar for ScrollSpy to work.
Use tabindex="0" on the scroll container to allow keyboard focus and proper event handling.
Adjust data-bs-offset to match your fixed header height for accurate highlighting.
ScrollSpy highlights navigation links based on scroll position.
Use data-bs-spy="scroll" and data-bs-target to connect scrolling and menu.
Works well for long pages with multiple sections and fixed navigation.
Practice
Solution
Step 1: Understand ScrollSpy functionality
ScrollSpy tracks which section is visible on the page while scrolling.Step 2: Identify ScrollSpy's effect on navigation
It highlights the corresponding navigation link to show the active section.Final Answer:
To highlight navigation links based on the page scroll position -> Option DQuick Check:
ScrollSpy = Highlight nav links on scroll [OK]
- Confusing ScrollSpy with fixed headers
- Thinking ScrollSpy adds animations
- Assuming ScrollSpy lazy loads content
Solution
Step 1: Identify the attribute that activates ScrollSpy
The attributedata-bs-spy="scroll"tells Bootstrap to watch scroll events.Step 2: Differentiate from other attributes
data-bs-targetpoints to the nav but does not activate ScrollSpy itself.Final Answer:
data-bs-spy="scroll" -> Option AQuick Check:
Activate ScrollSpy = data-bs-spy="scroll" [OK]
- Using data-bs-toggle instead of data-bs-spy
- Confusing data-bs-target as activation attribute
- Adding non-existent data-bs-scroll attribute
<body data-bs-spy="scroll" data-bs-target="#navbar">
<nav id="navbar">
<a href="#section1">Section 1</a>
<a href="#section2">Section 2</a>
</nav>
<div id="section1">Content 1</div>
<div id="section2">Content 2</div>
</body>Solution
Step 1: Analyze the HTML snippet
The body hasdata-bs-spy="scroll"anddata-bs-target="#navbar"attributes.Step 2: Check ScrollSpy initialization requirements
Bootstrap 5 supports automatic initialization of ScrollSpy via data attributes without explicit JavaScript calls.Step 3: Expected behavior
The link corresponding to the section currently visible will be highlighted in the navbar as you scroll.Final Answer:
The link for the section currently visible will be highlighted in the navbar -> Option CQuick Check:
ScrollSpy highlights nav links based on scroll position [OK]
- Thinking data attributes alone activate highlighting
- Expecting auto-scroll to sections on load
- Confusing with sticky/fixed navbar behavior
<div id="scrollArea" data-bs-spy="scroll" data-bs-target="#menu">
<nav id="menu">
<a href="#part1">Part 1</a>
<a href="#part2">Part 2</a>
</nav>
<div id="part1">Content 1</div>
<div id="part2">Content 2</div>
</div>Solution
Step 1: Check ScrollSpy container requirements
ScrollSpy requires the scrollable container to have a fixed height and overflow: auto or scroll to detect scrolling.Step 2: Identify missing CSS for #scrollArea
The code lacks CSS to make #scrollArea scrollable, so ScrollSpy won't work properly.Final Answer:
The scrollable container #scrollArea needs a fixed height and overflow set -> Option AQuick Check:
Scrollable container needs height + overflow [OK]
- Not setting height and overflow on scroll container
- Placing nav outside scroll container unnecessarily
- Using wrong data-bs-target attribute
- Using full URLs in href instead of section IDs
Solution
Step 1: Enable ScrollSpy on the scroll container
Usually,bodyis the scroll container, so adddata-bs-spy="scroll"anddata-bs-targetthere.Step 2: Set
ScrollSpy requires the scroll container to haveposition: relative;on the scroll containerposition: relative;for correct offset calculations.Step 3: Ensure navbar links correspond to section IDs
Links in the navbar must have hrefs matching the IDs of the page sections for ScrollSpy to highlight correctly.Final Answer:
Add data-bs-spy="scroll" and data-bs-target on body, set position: relative; on body, and ensure navbar links match section IDs -> Option BQuick Check:
ScrollSpy setup = data attributes + position + matching links [OK]
- Adding data-bs-spy on wrong elements
- Forgetting position: relative on scroll container
- Using full URLs instead of section IDs in links
- Setting overflow on navbar instead of scroll container
Ready to prove it?
Pass the test and this pattern moves unknown
