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

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.

Bootstrap 4 Grid System

Bootstrap 4 Grid System The bootstrap 4 grid system is used to create a responsive website layout. The bootstrap 4 offers a mobile-first flexbox grid system that allows you to divide...

4 minutes read.

Bootstrap 4 Grid Classes

Bootstrap 4 Grid Classes Extra-small Grid – In the extra-small grid system class, when you create the grid (columns), the columns will not be stacked on top of each other on...

8 minutes read.

Dropdown & Dropup

Bootstrap Dropdowns The bootstrap dropdown menu is used to create a toggleable menu. It provides users to choose one value from a predefined list. Before creating dropdown menu, we have to know...

3 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 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 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 Images

Bootstrap 4 Images Bootstrap provides several classes for images to make them responsive and also helps to give some style to the images. Image Shapes Rounded Corners Bootstrap 4 offers .rounded class that makes the corners...

5 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.

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 4 Pagination

Bootstrap 4 Pagination Bootstrap pagination is used to show the existence of a series of web pages of the website. It allows navigation on multiple pages of a website. In pagination, there is a block...

9 minutes read.

Bootstrap 4 Popover

Bootstrap 4 Popover Popover A popover is similar to the tooltip. The only difference between tooltip and Popover is that in the tooltip, the popup box appears when you move the cursor...

6 minutes read.

Difference between bootstrap.css and bootstrap-theme.css

The "bootstrap.css" uses to create a website with pre-existing designs and context. It uses basic Bootstrap classes like rows and columns with a CSS styling tool. The website is styled...

5 minutes read.

Bootstrap Validation Example

Validation We can provide valuable and actionable feedback to the users through an HTML form validation. It happens via custom styles or behaviors of default browsers and JavaScript. How does validation work...

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 Typography

Bootstrap provides simple and easily customized typography for headings, body text, lists, and many more. Headline <h1> to <h6> By default, Bootstrap has HTML headings (<h1> to <h6>) that are given below: Example <h1>h1. Bootstrap heading</h1>   <h2>h2. Bootstrap heading</h2>   <h3>h3. Bootstrap heading</h3>   <h4>h4. Bootstrap heading</h4>   <h5>h5. Bootstrap heading</h5>   <h6>h6. Bootstrap heading</h6> Try Now ←...

1 minute 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 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 With CSS

← Prev Next → ...

1 minute read.

Bootstrap Glyphicons

Bootstrap Glyphicon is used to create different type of glyphicons It is used in the web project. It provides 260 Glyphicons from the Glyphivons Halflings set. There are various examples of...

1 minute read.