Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Underline active navbar links in bootstrap 4

I'm upgrading a website from bootstrap 3 to 4 (beta). The css I used to underline active navbar links is no longer working. Instead of underlining the item, it underlines the full width of the page. How can I underline active nav items in bootstrap 4?

Example Navbar

Bootstrap 3 css

.navbar-default .navbar-nav > .active:after {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    content: " ";
    border-bottom: 5px solid #5BC0EB;
}

Attempted Bootstrap 4 css

.navbar-light .navbar-nav .active::after {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    content: " ";
    border-bottom: 5px solid #5BC0EB;
}

HTML

<nav class="navbar fixed-top navbar-light bg-light navbar-expand-md">
    <div class="container">
        <a class="navbar-brand" href="#">My Website</a>

        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarCollapse" aria-controls="navbarCollapse"
            aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
        </button>

        <div class="collapse navbar-collapse justify-content-end" id="main-nav-collapse">
            <ul class="navbar-nav">
                <li class="nav-item active"><a class="nav-link" href="#about">About</a></li>
                <li class="nav-item"><a class="nav-link" href="#classes">Classes</a></li>
                <li class="nav-item"><a class="nav-link" href="#gallery">Gallery</a></li>
                <li class="nav-item"><a class="nav-link" href="#events">Events</a></li>
                <li class="nav-item"><a class="nav-link" href="#contact">Contact</a></li>
                <li class="nav-item"><a class="nav-link" href="#offers">Offers</a></li>
            </ul>
        </div>
    </div>
</nav>
like image 267
ChaoticNadirs Avatar asked Aug 23 '17 07:08

ChaoticNadirs


People also ask

How do I add a line under the navbar?

border-bottom: 3px solid #fff; You can also do this quite easily by just clicking on your Navbar, going to the Appearance panel > Border, select the bottom border icon, Style would be solid, Color #fff, Width 3px (or whatever you want.)

How set navbar active class in bootstrap?

To accomplish this task, you can use ng-controller(NavigationController) to set the bootstrap navbar active class with AngularJS. To run a single controller outside ng-view. You can set class= “active” when the angular route is clicked.

How do I change the background color of active link navbar?

Given an HTML document containing a list of items and the task is to change the background color of list of items when the item is active. The state of list of items can be changed by changing the background-color CSS property.


1 Answers

This happened because of your positioning.

Try to set relative position to your li and set after to a tag inside li which will be absolute positioned like below

.navbar-nav > li {
  float: left;
  position: relative;
}
.navbar-light .navbar-nav .active a::after {
  border-bottom: 5px solid #5bc0eb;
  bottom: -10px;
  content: " ";
  left: 0;
  position: absolute;
  right: 0;
}

Hope it helps.

like image 185
RaJesh RiJo Avatar answered Sep 18 '22 05:09

RaJesh RiJo