Bootstrap 4 Scrollspy

Bootstrap 4 Scrollspy

Bootstrap 4 offers a scrollspy component that is used to make the website more attractive and eye-catching. Basically, the scrollspy is used on the single-page website.

The scrollspy highlights the nav menu item in the navigation bar based on the scroll position of the user. It also indicates which link is currently active.

Basic Scrollspy – The default position of the scrollspy is horizontal. It automatically updates the link when you scroll the page.

Steps for creating Scrollspy

  • To create a scrollspy, add the data-spy=”scroll” attribute to the <body> element that is used as a scrollable region. Also, add the data-target attribute to the <body> element. The value of the data-target attribute should be id or class name of the navigation bar such as .navbar to ensure that the scrollable region is connected to the navigation bar.
  • Add the id to the <div> element to create the scrollable section. The value of the id of the scrollable element and the value of the href of the nav menu item must be the same.

Note: The scrollspy needs the CSS position property ‘ position: relative; ’ to make the scrollspy work properly.

Example

<!DOCTYPE html>
 <html lang="en">
 <head>
   <title>Bootstrap 4 Scrollspy Example</title>
   <meta charset="utf-8">
   <meta name="viewport" content="width=device-width, initial-scale=1"> 
   <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
   <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
   <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
   <style>
   body {
       position: relative;  
   }
   </style>
 </head>
 <body data-spy="scroll" data-target=".navbar" data-offset="50">
 <nav class="navbar navbar-expand-sm bg-dark navbar-dark fixed-top">  
   <ul class="navbar-nav">
     <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>
     <li class="nav-item dropdown">
       <a class="nav-link dropdown-toggle" href="#" id="navbardrop" data-toggle="dropdown">
         Section 4
       </a>
       <div class="dropdown-menu">
         <a class="dropdown-item" href="#section41">Section 4.1</a>
         <a class="dropdown-item" href="#section42">Section 4.2</a>
       </div> 
     </li>
   </ul>
 </nav>
 <div id="section1" class="container-fluid bg-success" style="padding-top:70px;padding-bottom:70px">
   <h1>Section 1</h1>
   <p>Bootstrap 4 offers a scrollspy component which is used to make the website more attractive and eye-catching. The scrollspy is basically used in single page website.</p>
   <p>The scrollspy highlights the nav menu item in the navigation bar based on the scroll position of the user. It also indicate which link is currently active. </p>
 </div> 
 <div id="section2" class="container-fluid bg-warning" style="padding-top:70px;padding-bottom:70px">
   <h1>Section 2</h1>
   <p>Bootstrap 4 offers a scrollspy component which is used to make the website more attractive and eye-catching. The scrollspy is basically used in single page website.</p>
   <p>The scrollspy highlights the nav menu item in the navigation bar based on the scroll position of the user. It also indicate which link is currently active. </p>
 </div> 
 <div id="section3" class="container-fluid bg-secondary" style="padding-top:70px;padding-bottom:70px">
   <h1>Section 3</h1>
   <p>Bootstrap 4 offers a scrollspy component which is used to make the website more attractive and eye-catching. The scrollspy is basically used in single page website.</p>
   <p>The scrollspy highlights the nav menu item in the navigation bar based on the scroll position of the user. It also indicate which link is currently active. </p>
 </div> 
 <div id="section41" class="container-fluid bg-danger" style="padding-top:70px;padding-bottom:70px">
   <h1>Section 4 Submenu 1</h1>
   <p>Bootstrap 4 offers a scrollspy component which is used to make the website more attractive and eye-catching. The scrollspy is basically used in single page website.</p>
   <p>The scrollspy highlights the nav menu item in the navigation bar based on the scroll position of the user. It also indicate which link is currently active. </p>
 </div>
 <div id="section42" class="container-fluid bg-info" style="padding-top:70px;padding-bottom:70px">
   <h1>Section 4 Submenu 2</h1> 
   <p>Bootstrap 4 offers a scrollspy component which is used to make the website more attractive and eye-catching. The scrollspy is basically used in single page website.</p>
   <p>The scrollspy highlights the nav menu item in the navigation bar based on the scroll position of the user. It also indicate which link is currently active. </p>
 </div>
 </body>
 </html> 

Output

Bootstrap 4 Scrollspy

Scrollspy Vertical Menu – Bootstrap 4 allows you to create the scroll spy menu vertical instead of horizontal.

Example

<!DOCTYPE html>
 <html lang="en">
 <head>
   <title>Bootstrap 4 Scrollspy Example</title>
   <meta charset="utf-8">
   <meta name="viewport" content="width=device-width, initial-scale=1">
   <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> 
   <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
   <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
   <style>
   body {
     position: relative;
   }
   ul.nav-pills { 
     top: 20px;
     position: fixed;
   }
   div.col-8 div {
     height: 500px;
   }
   </style>
 </head> 
 <body data-spy="scroll" data-target="#myScrollspy" data-offset="1">
 <div class="container-fluid">
   <div class="row">
     <nav class="col-sm-3 col-4" id="myScrollspy">
       <ul class="nav nav-pills flex-column">
         <li class="nav-item">
           <a class="nav-link active" 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>
         <li class="nav-item dropdown">
           <a class="nav-link dropdown-toggle" data-toggle="dropdown" href="#">Section 4</a>
           <div class="dropdown-menu"> 
             <a class="dropdown-item" href="#section41">Section 4.1</a>
             <a class="dropdown-item" href="#section42">Section 4.2</a>
           </div>
         </li>
       </ul>
     </nav>
     <div class="col-sm-9 col-8">
       <div id="section1" class="bg-success">     
         <h1>Section 1</h1>
         <p>Scroll this section and look at the navigation list while scrolling!</p>
       </div>
       <div id="section2" class="bg-warning"> 
         <h1>Section 2</h1>
         <p>Scroll this section and look at the navigation list while scrolling!</p>
       </div>        
       <div id="section3" class="bg-secondary">          
         <h1>Section 3</h1>
         <p>Scroll this section and look at the navigation list while scrolling!</p>
       </div>
       <div id="section41" class="bg-danger">         
         <h1>Section 4.1</h1>
         <p>Scroll this section and look at the navigation list while scrolling!</p>
       </div>      
       <div id="section42" class="bg-info">          
         <h1>Section 4.2</h1>
         <p>Scroll this section and look at the navigation list while scrolling!</p>
       </div>
     </div>
   </div>
 </div>
 </body>
 </html> 

Output

Bootstrap 4 Scrollspy

Related Topics

Bootstrap 4 Dropdowns

Bootstrap Dropdowns Bootstrap 4 provides a dropdown component, which is a toggle and contextual layout, used to display a predefined list of links. It allows the user to select a value from the dropdown...

15 minutes read.

Bootstrap Datepicker

What is a Bootstrap Datepicker? It is used to display a calendar with the date, month, and time from a dropdown menu. We will use an example to see how the date...

5 minutes read.

Bootstrap Navigation bar

Navigation Bars In Bootstrap, We can create navigation bar like a navigation header. It is placed at the top of the page. We can collapse or extend it according to screen...

3 minutes read.

Bootstrap Panels

A bootstrap panel is used to create bordered box with some padding around its content. It is created with the .panels classes, and .panel-body classes. There are following classes of the panel: Panel header Panel...

4 minutes read.

Bootstrap Buttons Groups

Bootstrap 4 Button Group Bootstrap 4 provides a feature button group that helps in creating a series of buttons together in a group. Basic Button Group  The .btn-group class is used within the <div> tag to create a...

6 minutes read.

Bootstrap 4 Jumbotron

Bootstrap 4 Jumbotron Bootstrap offers Jumbotron, which is used to get more attention to specific content or information on the web page. It provides a gray background-color to the content to make that content...

2 minutes read.

Bootstrap 4 Progress Bar

Bootstrap Progress Bar Bootstrap 4 offers progress bars that are used to represent the progress of tasks such as downloading a file, software installation, etc. on the computer. It represents the percentage of the...

9 minutes read.

Bootstrap First Example

1) Add HTML5 doctype Before starting the example of Bootstrap, we should know about the some basic rules of Bootstrap. We have to add HTML5 doctype with lang element and CSS properties. <!DOCTYPE html>   <html lang="en">     <head>       <meta charset="utf-8">      </head>   </html> 2) Bootstrap...

2 minutes read.

Environment Setup

How to use Bootstrap? We can use bootstrap in our project either by downloading or by providing link of the library files. 1) Download Bootstrap files To download files, visit official site of bootstrap www.getbootstrap.com/download/ and...

1 minute read.

Bootstrap 4 Icons

Bootstrap 4 Icons Bootstrap 3 contains the default icon library. But in bootstrap 4, you have to include the external icon library by yourself. Many free icon libraries are available such...

2 minutes read.

Bootstrap 4 Alerts

Bootstrap 4 Alerts Bootstrap 4 offers alerts that are used to provide feedback messages on the user’s actions. There is no boundation of text length in the alerts. Alerts can be created...

1 minute read.

Bootstrap With CSS

← Prev Next → ...

1 minute read.

Bootstrap 4 Inputs

Bootstrap Inputs Bootstrap Form Inputs – Several pre-defined input types are available in HTML and Bootstrap. These input types help in styling the forms. When an input type is specified in the form, it means...

10 minutes read.

Difference between Bootstrap and WordPress

What is WordPress? WordPress is a free open-source CMS (Content Management System) that is written using Hypertext Pre-processor (PHP) and is paired with MySQL and is supported with HTTPS (Hypertext Transfer...

3 minutes read.

CakePHP Layouts & Elements

A layout includes a presentation code that can be wrapped out in your view. It means that what you want to see in your view should be well-positioned in layout. When you create...

9 minutes read.

Text Shadows in CSS

Text shadows are created with the text-shadow CSS property. It takes a list of shadows separated by commas to be applied to the text and any of its styles. Each...

5 minutes read.

Bootstrap Toast Example

What are toasts? Toasts are similar to notifications, and they are lightweight and act as push notifications from mobile and desktop operating systems. They’re built using flexbox in Bootstrap. They can...

4 minutes read.

Bootstrap 4 Utilities

Bootstrap 4 Utilities Bootstrap 4 utilities also known as helper classes that are used to add more style to the components. Utilities are used to make the website or webpage more stylish or elegant....

10 minutes read.

Bootstrap Time Picker Example

What is a Bootstrap Time picker? It is used to display the time in hours, minutes and seconds from a dropdown menu. In this section, we will create an example to...

4 minutes read.

Bootstrap List group

Bootstrap list group provides a group of a list of items. The most basic list is an unordered list. We can use <ul> element with .list-group class and <li> element with .lsit-group-item. Let us see...

3 minutes read.