Master Bootstrap From Scratch

Clear, interactive, and structured Bootstrap lessons designed for beginners and developers.

Bootstrap ScrollSpy

Learn how to automatically update navigation links according to the section visible on the screen.

Introduction

Bootstrap ScrollSpy automatically updates the active navigation link when a user scrolls through different sections of a page. It is useful for long pages containing multiple topics, documentation, course details and FAQs. ScrollSpy works with Bootstrap's JavaScript functionality.

Learning Objectives

  • Understand the ScrollSpy component.
  • Connect navigation links to page sections.
  • Automatically highlight the active section.
  • Use ScrollSpy with a scrolling container.
  • Build a real-life course navigation example.

How ScrollSpy Works

Step 1
Scroll the Page

The user moves through the page content.

Step 2
Detect Section

ScrollSpy detects the visible section.

Step 3
Update Navigation

The matching navigation link becomes active.

1. Basic ScrollSpy

In this example, navigation links point to page sections using IDs. Bootstrap ScrollSpy watches the scrolling page and updates the active navigation link.

Program Code
<nav id="courseNav" class="nav nav-pills">
    <a class="nav-link" href="#htmlSection">HTML</a>
    <a class="nav-link" href="#cssSection">CSS</a>
    <a class="nav-link" href="#jsSection">JavaScript</a>
</nav>

<div data-bs-spy="scroll"
     data-bs-target="#courseNav"
     data-bs-smooth-scroll="true"
     tabindex="0"
     class="scrollspy-content">

    <section id="htmlSection">
        <h4>HTML</h4>
        <p>HTML defines webpage structure.</p>
    </section>

    <section id="cssSection">
        <h4>CSS</h4>
        <p>CSS styles webpage content.</p>
    </section>

    <section id="jsSection">
        <h4>JavaScript</h4>
        <p>JavaScript adds interactivity.</p>
    </section>

</div>

<script>
bootstrap.ScrollSpy.getOrCreateInstance(
    document.querySelector('[data-bs-spy="scroll"]')
);
</script>
Program Output

HTML

HTML defines the structure of a webpage using elements such as headings, paragraphs, links and forms.

CSS

CSS controls colors, spacing, fonts, layouts and responsive page design.

JavaScript

JavaScript adds behavior to websites, such as event handling and dynamic updates.

Scroll inside the content box to see the active navigation link change.

2. ScrollSpy with a Sidebar

A sidebar can contain links to multiple sections. ScrollSpy highlights the corresponding link while the user moves through the content.

Program Code
<div class="row">
    <div class="col-md-3">
        <nav id="sidebarNav" class="nav flex-column">
            <a class="nav-link" href="#overview">Overview</a>
            <a class="nav-link" href="#syllabus">Syllabus</a>
            <a class="nav-link" href="#projects">Projects</a>
        </nav>
    </div>

    <div class="col-md-9"
         data-bs-spy="scroll"
         data-bs-target="#sidebarNav"
         tabindex="0"
         style="height: 300px; overflow-y: auto; position: relative;">

        <section id="overview">
            <h4>Overview</h4>
            <p>Course introduction...</p>
        </section>

        <section id="syllabus">
            <h4>Syllabus</h4>
            <p>Course topics...</p>
        </section>

        <section id="projects">
            <h4>Projects</h4>
            <p>Practical projects...</p>
        </section>

    </div>
</div>
Program Output

Course Overview

Learn the course goals, prerequisites and skills you will develop.

This section introduces the learning path and explains how the course is organized.

Course Syllabus

Review the topics, technologies and concepts covered in the training.

Each topic builds on the previous concepts to develop practical skills.

Practical Projects

Practice your skills by building projects that apply the course concepts.

Review the project requirements, implementation steps and final output.

Scroll inside the right-hand panel to highlight Overview, Syllabus or Projects.

3. Real-Life Example: CIIT Course Details Page

Imagine a student opens the CIIT Training Institute website to learn about the .NET Full Stack course. The page contains several sections:

  • Course Overview
  • Course Syllabus
  • Training Duration
  • Live Projects
  • Placement Assistance

With ScrollSpy, when the student scrolls to Live Projects, the Live Projects link becomes active in the navigation. This helps the student understand which part of the course page they are viewing.

Real-Life Demo Output

Course Overview

Explore the .NET Full Stack learning path, including frontend, backend and database concepts.

Course Syllabus

Study C#, ASP.NET Core, MVC, Web API, SQL Server and related development concepts.

Training Duration

Display the actual course duration and batch schedule selected by the institute.

Live Projects

Practice by building an application with controllers, services, APIs and a database.

Placement Assistance

Display the institute's actual placement assistance services and candidate support.

Demo content only. Scroll within the panel and watch the corresponding navigation link become active.

Important Attributes and Classes

Attribute / Class Purpose
data-bs-spy="scroll" Enables ScrollSpy on an element.
data-bs-target Identifies the navigation element to update.
nav-pills Styles navigation links as pills.
nav-link Styles a navigation link.
active Indicates the active navigation link.
tabindex="0" Makes a scrolling container keyboard-focusable.
overflow-y: auto Allows a container to scroll vertically when needed.

Common Mistakes

  • Using a navigation href that does not match a section ID.
  • Forgetting to load the Bootstrap JavaScript bundle.
  • Forgetting position: relative on a custom scrolling container.
  • Applying ScrollSpy to the wrong element.
  • Reusing IDs for multiple navigation targets.

Summary

  • ScrollSpy highlights navigation based on the current section.
  • Navigation links must point to matching section IDs.
  • It can monitor page scrolling or a dedicated scrolling container.
  • It is useful for long course pages and documentation.
  • Bootstrap JavaScript is required for ScrollSpy behavior.