Alok
Alok

Reputation: 8978

Add margin to the border-bottom of active nav- link without affecting the text

I have been working on my Bootstrap Nav and kind of worked it out, but somehow struggling with this thing which to make the bottom border of the active link to take up the width of the text, not the container it is in. So for this came up with this idea to add some margin to the border-bottom of the active link. After numerous efforts, all I'm getting is the messed up UI. I need your help guys, would be great to learn something today.

CODE

Trying to add the code snippet but the Bootstrap nav is not working fine.

HTML

<!doctype html>
<html lang="en">
  <head>
  <!-- Required meta tags -->
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

  <!-- Bootstrap CSS -->
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous">    
  </head>
  <body>

     <!-- Just an image -->
     <nav class="navbar navbar-expand-lg navbar-light bg-light">
       <div class="container">
         <a class="navbar-brand" href="#home" style="border-bottom: none">HOME</a>
         <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="col-lg-3"></div>
         <div class="pt-lg-0 pt-3 collapse navbar-collapse" id="navbarNavAltMarkup">
         <div class="navbar-nav">
            <a class="nav-item nav-link" href="#section-1">SECOND PAGE</a>
            <a class="nav-item nav-link" href="#section-2">THIRD PAGE</a>
        </div>
       </div>
      </div>
    </nav>

    <section class="testimonial home-page" id="home">
      <div class="container">
        <h1>Bringing ease to<br>each home</h1>
        <div class="d-none d-sm-block" style="margin-top: 350px"></div>
      </div>
    </section>

   <section class="testimonial home-page" id="section-1">
      <div class="container">
        <h1>Second Page</h1>
        <div class="d-none d-sm-block" style="margin-top: 350px"></div>
      </div>
   </section>

   <section class="testimonial home-page" id="section-2">
     <div class="container">
        <h1>Third Page</h1>
        <div class="d-none d-sm-block" style="margin-top: 350px"></div>
     </div>
   </section>
</body>
</html>

CSS

.navbar {
   top: 0px;
   width: 100%;
   z-index: 999;
   display: block;
   padding: 20px !important;
   position: fixed!important;
   -webkit-backface-visibility: hidden
 }

 .navbar-nav a {line-height: normal}
 .home-page H1 {padding-top: 219px;color: #616161}
 .active {color: grey!important; border-bottom: 5px solid #8ABE25}

JAVASCRIPT FOR ACTIVE LINK

$(document).ready(function () {
  $('.navbar div a').click(function(event){
    //remove all pre-existing active classes
    $('.active').removeClass('active');

    //add the active class to the link we clicked
    $(this).addClass('active');
  });
});

What I have tried from this this is this :

.active { 
  position: fixed!important;
  content: '';
  border-bottom: 1px solid #d2d7da;
  width: 70%;
  transform: translateX(-50%);
  bottom: -15px;
  left: 50%;
}

Messed up my UI. Also tried box-sizing: border-box but didn't work out in my case.

RESULT I want is like this :

Design to be achieved

This is my JsFiddle link : JsFiddle Result If the nav bar doesn't open up kindly expand the result so that you can see the other option, don't know why the JsFiddle is behaving weird. Any help would be appreciated.

Upvotes: 2

Views: 2891

Answers (1)

patelarpan
patelarpan

Reputation: 7991

$(document).ready(function () {
$('.navbar div a').click(function(event){
        //remove all pre-existing active classes
        $('.active').removeClass('active');

        //add the active class to the link we clicked
        $(this).addClass('active');
    });
});
.navbar {
  top: 0px;
  width: 100%;
  z-index: 999;
  display: block;
  padding: 20px !important;
  position: fixed!important;
  -webkit-backface-visibility: hidden
}

.navbar-nav a {line-height: normal}
.home-page H1 {padding-top: 219px;color: #616161}
.active span {color: grey!important; position: relative; display: inline-block;}

.active span:after {
    content:"";
    position:absolute;
    left:0;
    right:0;
    bottom:-4px;
    height: 5px;
    background-color: #8ABE25;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous">    
  </head>
  <body>
  
    <!-- Just an image -->
    <nav class="navbar navbar-expand navbar-light bg-light">
      <div class="container">
        <a class="navbar-brand" href="#home" style="border-bottom: none">
          HOME
        </a>
        <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="col-lg-3"></div>
        <div class="pt-lg-0 pt-3 collapse navbar-collapse" id="navbarNavAltMarkup">
          <div class="navbar-nav">
            <a class="nav-item nav-link" href="#section-1"><span>SECOND PAGE INFORMATION</span></a>
            <a class="nav-item nav-link" href="#section-2"><span>THIRD PAGE</span></a>
            <a class="nav-item nav-link" href="#section-2"><span>FOURTH PAGE</span></a>
            <a class="nav-item nav-link" href="#section-2"><span>FIFTH PAGE</span></a>
            <a class="nav-item nav-link" href="#section-2"><span>SIXTH PAGE</span></a>
          </div>
        </div>
      </div>
    </nav>
    
    <section class="testimonial home-page" id="home">
      <div class="container">
        <h1>Bringing ease to<br>each home</h1>
        <div class="d-none d-sm-block" style="margin-top: 350px"></div>
      </div>
    </section>
    
    <section class="testimonial home-page" id="section-1">
      <div class="container">
        <h1>Second Page</h1>
        <div class="d-none d-sm-block" style="margin-top: 350px"></div>
      </div>
    </section>
    
    <section class="testimonial home-page" id="section-2">
      <div class="container">
        <h1>Third Page</h1>
        <div class="d-none d-sm-block" style="margin-top: 350px"></div>
      </div>
    </section>
  </body>
</html>

wrap your text with span and giving the following style.

.active span {color: grey!important; position: relative; display: inline-block;}

.active span:after {
    content:"";
    position:absolute;
    left:0;
    right:0;
    bottom:-4px;
    height: 5px;
    background-color: #8ABE25;
}

Upvotes: 1

Related Questions