Nearby lessons
28 of 39π§ BS5 Scrollspy
π What Is Bootstrap 5 Scrollspy?
Scrollspy highlights the nav link for the section currently in view. Put data-bs-spy="scroll" on the scrollable element (e.g. body or a div), data-bs-target="#navId" (the navβs id), and ensure nav links use href="#sectionId" matching section ids. Requires Bootstrapβs JavaScript.
π§ Enabling Scrollspy (data-bs-spy, data-bs-target)
data-bs-spy="scroll" β enables scrollspy on the element that scrolls. data-bs-target="#navId" β CSS selector of the nav (the nav must have that id). data-bs-offset β optional pixel offset for when the active state triggers. Nav links must be .nav-link with href="#sectionId"; sections need matching ids.
β Scrollspy Best Practices
- The spied element must be scrollable (e.g.
overflow-y: autoand a fixed height). - Nav links use
href="#sectionId"and sections use matchingids. - Use data-bs-offset if you have a fixed header so active state aligns correctly.
π§ Test Your Knowledge
3 QuestionsProgress: 0 / 3
Keep Going!BS5 Offcanvas