Struggling to incorporate Bootstrap 4 scrollSpy on my website with an active state featuring a red border-bottom effect upon scrolling and/or clicking.
This is the desired outcome when scrolling or clicking on a section:
https://i.sstatic.net/J1qJg.jpg
Check out my Codepen for reference.
Looking for a Bootstrap/CSS-only solution that displays a Border Bottom only in its active state during scrollSpy and/or onClick events?
Still new to Bootstrap 4, currently learning jQuery and/or JS. Seeking assistance in finding a solution using Bootstrap, CSS, or even a minimalistic approach involving just a few lines of jQuery. Thank you!
CSS:
body {
position: relative;
}
.navbar-nav .nav-link {
margin-right: 15px;
margin-left: 15px;
}
.navbar-nav li a{color:#d3d3d3;}
.navbar-nav li a:hover {color:#333;}
.navbar-expand-sm .navbar-nav a:hover:before {
border-bottom: 5px solid #fff;
bottom: -7px; width:auto;
content: " ";
left:18px;
position: absolute;
right:18px;
}
.navbar-expand-sm .navbar-nav .active a::after {
border-bottom: 5px solid red;
bottom: -7px; width:auto;
content: " ";
left:18px;
position: absolute;
right:18px;
}
.navbar-nav > li {
float: left;
position: relative;
}
.custom-toggler .navbar-toggler-icon {
background-image: url("data:image/svg+xml;charset=utf8,%3Csvg viewBox='0 0 32 32' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath stroke='rgba(255, 0, 0, 1)' stroke-width='2' stroke-linecap='round' stroke-miterlimit='10' d='M4 8h24M4 16h24M4 24h24'/%3E%3C/svg%3E");
}
HTML:
<body data-spy="scroll" data-target=".navbar" data-offset="50">
<div class="wrapper">
<nav class="navbar navbar-expand-sm bg-dark navbar-dark fixed-top">
<a class="navbar-brand" href="index.html">
<img src="/assets/brand/bootstrap-solid.svg" width="30" height="30" class="d-inline-block align-top" alt="">CMM
</a>
<button class="navbar-toggler custom-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav ml-md-auto">
<li class="nav-item active">
<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">Link 1</a>
<a class="dropdown-item" href="#section42">Link 2</a>
</div>
</li>
</ul>
</div>
</nav>
<div id="section1" class="container-fluid bg-success" style="padding-top:70px;padding-bottom:70px">
<h1>Section 1</h1>
</div>
<div id="section2" class="container-fluid bg-warning" style="padding-top:70px;padding-bottom:70px">
<h1>Section 2</h1>
</div>
<div id="section3" class="container-fluid bg-secondary" style="padding-top:70px;padding-bottom:70px">
<h1>Section 3</h1>
</div>
<div id="section41" class="container-fluid bg-danger" style="padding-top:70px;padding-bottom:70px">
<h1>Section 4 Submenu 1</h1>
</div>
<div id="section42" class="container-fluid bg-info" style="padding-top:70px;padding-bottom:70px">
<h1>Section 4 Submenu 2</h1>
</div>
</div>
</body>