Why won't scrollspy work in Bootstrap 4?

497 Views Asked by At

I seem to be following the documentation exactly but I can't figure out why it won't highlight the navbar list-item text as a scroll down to those sections.

This is my body tag:

<body data-spy="scroll" data-target=".navbar" id="home">

Here's my nav bar:

<!-- nav-->  
<nav class="navbar navbar-expand-lg navbar-light navbar-toggleable-xl 
 bg-primary fixed-top">
  <div class="container">
  <button class="navbar-toggler" type="button" data-toggle="collapse" 
   data-target="#navbarNavAltMarkup" aria-
   controls="navbarNavAltMarkup" aria-expanded="false" aria-
   label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <div class="collapse navbar-collapse" id="navbarNavAltMarkup">
    <div class="navbar-nav">
      <a class="nav-item nav-link text-white" href="#home">Home</a>
      <a class="nav-item nav-link text-light" href="#about">About</a>
      <a class="nav-item nav-link text-light" 
          href="#speakers">Speakers</a>
      <a class="nav-item nav-link text-light" 
         href="#schedule">Schedule</a>
    </div>
  </div>
   <a class="hidden-xs-down navbar-brand text-white" 
     href="#">Presented by Treehouse</a>
   </div>
</nav>

Here's an example of a body section that doesn't highlight when I scroll down to it:

 <!--about-->
        <div class="row" id="about">
            <div class="col-md-4">
                <h3 class="mb-4">About Full Stack Conf</h3>
                <!--Why won't the image shape change?-->
                <img class="mb-3 img-fluid rounded hidden-xs-down" src="img/pdx.jpg" alt="Portland">
                <p>The beautiful city of Portland, Oregon will be the host city for Full Stack Conf!</p>
                <p>Explore the future of JavaScript with a lineup of industry professionals. Discover new techniques to advance your career as a web developer.</p>
            </div>
            <div class="col-md-4">
                <h3 class="mb-4">Expert Speakers</h3>
                <p>Our expert speaker lineup was just announced, so don't wait too long before grabbing your tickets!</p>
                <p>Want to meet the international JavaScript community and share skills with some of the world's top experts, hackers, and makers? Be the first to know what to expect for the future of JavaScript.</p>
                <p>Full Stack Conf is committed to being inclusive and welcoming for everyone. We look forward to another intensive day of learning and sharing.</p>
            </div>
            <div class="col-md-4">
                <h3 class="mb-4">What You'll Learn</h3>
                <ul>
                    <li><strong>MongoDB</strong>: NoSQL database</li>
                    <li><strong>Angular</strong>: JavaScript framework</li>
                    <li><strong>Express</strong>: Framework for Node</li>
                    <li><strong>Node.js</strong>: JavaScript environment</li>
                    <li><strong>ES2015</strong>: Latest version of JavaScript</li>
                    <li><strong>Babel</strong>: JavaScript compiler</li>
                </ul>
            </div>
        </div>
        <!-- /about -->
1

There are 1 best solutions below

1
On

It is working, but the text-* classes you have on the links are overriding the appearance of the active class. Use the navbar-dark instead and make sure body is position:relative...

https://www.codeply.com/go/MbK0piY1HL

<nav class="navbar navbar-expand-lg navbar-dark navbar-toggleable-xl bg-primary fixed-top">
</nav>