Bootstrap 4 navbar active class
Asked Answered
T

4

7

I have started learning web programming as a project and I've been having a hard time with getting my links to show as active on the navbar. I did start by looking for similar questions asked in the past but none of the answers seemed to fix my problem.

Here is my code

<div>
<hr>
    <nav class="container-fluid navbar navbar-expand-sm bg-dark navbar-dark" style="padding-left: 75px; margin-top: -16px;">
        <ul class="navbar-nav">
            <li class="active nav-item">
                <a class="nav-link active" style = "padding-left: 0px; color: white;" href="#">Most Popular</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#" style="color: white">News</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#" style="color: white">Sports</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#" style="color: white">Science</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#" style="color: white">Politics</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#" style="color: white">Economics</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#" style="color: white">Random</a> 
            </li>
            <li class="nav-item">
                <a class="nav-link " style="padding-left: 480px; color: white; " href="#">Log in</a>
            </li>
        </ul>
    </nav>
</div>

and I tried using this javascript I found but it hasn't worked so far

$('.nav li').click(function(){
$('.nav li').removeClass('active');
$(this).addClass('active');
})

Hopefully, my code isn't too funky since I am just getting started. Any help would be tremendously appreciated thank you very much!

Tourane answered 4/5, 2018 at 10:14 Comment(0)
P
11

There are multiple issues...

  1. The selector $('.nav li') does nothing because you have no .nav class used anywhere in the markup. It should be $('.navbar-nav .nav-link').
  2. You have style="color:white" on the links which will override any changes you make with the active class.
  3. You have no CSS for the active class, and by default Bootstrap active class on navbar-dark is going to be white. What color are you trying to set?
  4. Set active in the nav-link instead of the li,

.navbar-dark .nav-item > .nav-link.active  {
    color:white;
}

$('.navbar-nav .nav-link').click(function(){
    $('.navbar-nav .nav-link').removeClass('active');
    $(this).addClass('active');
})

Demo: https://www.codeply.com/go/I3EjDb74My

Patriciapatrician answered 4/5, 2018 at 10:28 Comment(6)
Thank you so much, I will go ahead and try those changes, I wish I could upvote you moreTourane
will do for sure, the script is still not working but other than that its perfectTourane
Sadly that link is now 404Karyosome
Works for me. Also the relevant code is in the questionPatriciapatrician
I am wondering how would you handle selection on a nav-item which is a dropdown menu, would you select both the parent wrapper and the individual dropdown-item?Frivol
There are two potential problems with this: 1) the documentation shows the active class applied to the li (nav-item) not a (nav-link) and 2) if the link actually navigates to a new page the navbar will of course immediately reset to its default state.Livable
E
2

If you have relative links (e.g. "/projects") in your navbar,

// remove any current navbar active classes
$(".navbar .nav-item.active").removeClass('active');
// add active class to proper navbar item that matches window.location
$('.navbar .nav-item a[href="' + location.pathname + '"]').closest('li').addClass('active');
Eldoneldora answered 25/6, 2020 at 20:37 Comment(1)
Thanks , the only solution which is working for me but it has kind of glitch in it. whenever i am moving to other page ,- It is highlight the the home page first and then moving thr highlight to other the clicked page.Klemm
D
0

Absolutely! Here's the code with the fix for highlighting the active navbar link, along with explanations

$(document).ready(function() {
  $('.nav-link').click(function() {
    // Remove 'active' class from all links
    $('.nav-link').removeClass('active');
    // Add 'active' class to the clicked link
    $(this).addClass('active');
  });
});
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Active Navbar Link</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
</head>
<body>
  <div>
    <hr>
    <nav class="container-fluid navbar navbar-expand-sm bg-dark navbar-dark" style="padding-left: 75px; margin-top: -16px;">
      <ul class="navbar-nav">
        <li class="nav-item active">  <a class="nav-link" href="#">Most Popular</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">News</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">Sports</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">Science</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">Politics</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">Economics</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">Random</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" style="padding-left: 480px;" href="#">Log in</a>
        </li>
      </ul>
    </nav>
  </div>

  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  <script src="script.js"></script> </body>
</html>
Doubleedged answered 28/6 at 2:51 Comment(0)
E
-1
// active nav

// get current url
var location = window.location.href;

// remove active class from all
$(".navbar .nav-item").removeClass('active');

// add active class to div that matches active url
$(".nav-item a[href='"+location+"']").addClass('active');
Elgar answered 22/8, 2019 at 16:53 Comment(2)
This script causes my website to constantly reload. Definitely not what I expected.Ludeman
Ditto, this caused my site to constantly reload, crashing most browsers.Centrobaric

© 2022 - 2024 — McMap. All rights reserved.