skip to Main Content

I’m trying to add a bottom border for each li item in my navbar. However, the border creates a separate line below the actual content of the li items even when I have given the div.nav-line class inside the li tags.

   $(function() {
        $(".dropdown").hover(
            function() { $(this).addClass('open') },
            function() { $(this).removeClass('open') }
        );
    });
.navbar {
  box-shadow: 0 3px 3px rgba(0, 0, 0, 0.3);
}

.navbar-default .navbar-nav {
  font-size: 15px;
}

.navbar-fixed-top {
  min-height: 80px;
}

.navbar-nav>li>a {
  padding-top: 0px;
  padding-bottom: 0px;
  line-height: 80px;
}

.dropdown-menu>.active>a,
.dropdown-menu>.active>a:hover,
.dropdown-menu>.active>a:focus {
  color: #ffffff;
  text-decoration: none;
  outline: 0;
  background-color: #b4a28f;
}

.navbar-nav>li>.nav-line {
  background-color: #3178b9;
  height: 2px;
  width: 0%;
  /*        -webkit-transition: all 100ms ease-out;
        -moz-transition: all 100ms ease-out;
        -o-transition: all 100ms ease-out;
        transition: all 100ms ease-out;*/
}

.navbar-nav>li:hover>.nav-line {
  background-color: #3178b9;
  height: 2px;
  width: 100%;
  -webkit-transition: all 150ms ease-in;
  -moz-transition: all 150ms ease-in;
  -o-transition: all 150ms ease-in;
  transition: all 150ms ease-in;
}
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.css">

<nav class="navbar navbar-default navbar-fixed-top">
  <div class="container">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#collapse" aria-expanded="false">
                        <span class="sr-only">Toggle navigation</span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
      <a class="navbar-brand" href="">
        <!-- <img class="img img-responsive" src="www/images/srs-logo.jpg" alt="SRS Constructions"> -->
        SRS Constructions
      </a>
    </div>
    <div class="collapse navbar-collapse navbar-right" id="collapse">
      <ul class="nav navbar-nav">
        <li class="active"><a class="main" href="#main">Home <span class="sr-only">(current)</span></a>
          <div class="nav-line"></div>
        </li>
        <li class="dropdown" id="nav-about">
          <a href="#about" class="dropdown-toggle main" role="button" aria-haspopup="true" aria-expanded="false">About
                            </a>
          <ul class="dropdown-menu" style="left:0;right: auto">
            <li><a href="about.html">The Founder</a></li>
            <li role="separator" class="divider"></li>
            <li><a href="health-policy.html">HSE Policy</a></li>
            <li><a href="quality-policy.html">Quality Policy</a></li>
          </ul>
          <div class="nav-line"></div>
        </li>
        <li><a class="main" href="#services">Services</a>
          <div class="nav-line"></div>
        </li>
        <li><a class="main" href="#projects">Our Projects</a>
          <div class="nav-line"></div>
        </li>
        <li><a class="main" href="#whyus">Why Us</a>
          <div class="nav-line"></div>
        </li>
        <li><a class="main" href="#contact">Contact</a>
          <div class="nav-line"></div>
        </li>
      </ul>
    </div>
  </div>
</nav>
  1. I want the border to appear inside the li container so that there is no extra line which spans the whole navbar (the background color on hover does not cover the full height of the li item, since the line of 2px is present.) .
  2. I do not want the line for the active li item.

Please help me to achieve this.

2

Answers


  1. Ok the following code should be good for you to use:

    I have added the following – relative positioning to li, absolute positioning to navline, and then transparent background for the active navline

    $(function() {
            $(".dropdown").hover(
                function() { $(this).addClass('open') },
                function() { $(this).removeClass('open') }
            );
        });
    .navbar {
      box-shadow: 0 3px 3px rgba(0, 0, 0, 0.3);
    }
    
    .navbar-default .navbar-nav {
      font-size: 15px;
    }
    
    .navbar-fixed-top {
      min-height: 80px;
    }
    
    .navbar-nav>li {
        position:relative;
    }
    .navbar-nav>li>a {
      padding-top: 0px;
      padding-bottom: 0px;
      line-height: 80px;
    }
    
    .dropdown-menu>.active>a,
    .dropdown-menu>.active>a:hover,
    .dropdown-menu>.active>a:focus {
      color: #ffffff;
      text-decoration: none;
      outline: 0;
      background-color: #b4a28f;
    }
    
    .navbar-nav>li>.nav-line {
      position:absolute; 
      bottom:-1px;
      left:0;
      background-color: #3178b9;
      height: 2px;
      width: 0%;
      /*        -webkit-transition: all 100ms ease-out;
            -moz-transition: all 100ms ease-out;
            -o-transition: all 100ms ease-out;
            transition: all 100ms ease-out;*/
    }
    .navbar-nav>li:hover>.nav-line {
      background-color: #3178b9;
      height: 2px;
      width: 100%;
      -webkit-transition: all 150ms ease-in;
      -moz-transition: all 150ms ease-in;
      -o-transition: all 150ms ease-in;
      transition: all 150ms ease-in;
    }
    
    
    .navbar-nav>li.active>.nav-line {
        background-color:transparent;
    }
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.css">
    
    <nav class="navbar navbar-default navbar-fixed-top">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#collapse" aria-expanded="false">
                            <span class="sr-only">Toggle navigation</span>
                            <span class="icon-bar"></span>
                            <span class="icon-bar"></span>
                            <span class="icon-bar"></span>
                        </button>
          <a class="navbar-brand" href="">
            <!-- <img class="img img-responsive" src="www/images/srs-logo.jpg" alt="SRS Constructions"> -->
            SRS Constructions
          </a>
        </div>
        <div class="collapse navbar-collapse navbar-right" id="collapse">
          <ul class="nav navbar-nav">
            <li class="active"><a class="main" href="#main">Home <span class="sr-only">(current)</span></a>
              <div class="nav-line"></div>
            </li>
            <li class="dropdown" id="nav-about">
              <a href="#about" class="dropdown-toggle main" role="button" aria-haspopup="true" aria-expanded="false">About
                                </a>
              <div class="nav-line"></div>
              <ul class="dropdown-menu" style="left:0;right: auto">
                <li><a href="about.html">The Founder</a></li>
                <li role="separator" class="divider"></li>
                <li><a href="health-policy.html">HSE Policy</a></li>
                <li><a href="quality-policy.html">Quality Policy</a></li>
              </ul>
            </li>
            <li><a class="main" href="#services">Services</a>
              <div class="nav-line"></div>
            </li>
            <li><a class="main" href="#projects">Our Projects</a>
              <div class="nav-line"></div>
            </li>
            <li><a class="main" href="#whyus">Why Us</a>
              <div class="nav-line"></div>
            </li>
            <li><a class="main" href="#contact">Contact</a>
              <div class="nav-line"></div>
            </li>
          </ul>
        </div>
      </div>
    </nav>
    Login or Signup to reply.
  2. Add this in CSS.

    .navbar-nav li.active a.main {
    display: none
    }

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search