goldlife goldlife - 4 months ago 24
CSS Question

Twitter Bootstrap Navbar with Transition Border - Background Color is not over the whole active Item

Please see my working Example.
As you can see the background color is not over the whole active Item.
When I remove the transition CSS, it will work like in this example (please resize the browser to the mobile version)

https://getbootstrap.com/examples/navbar-fixed-top/#

How can I solve this problem?



/* Navbar */
.navbar-fixed-top{
top: 80px;
}
.navbar-default{ /* assigning the top bar to the entire navbar div element */
background-color: #4F4F4F;
}
nav.navbar {
box-shadow: 0 0 2px 0 #ccc;
}
nav.navbar a {
color: #FF7D36;
}
nav.navbar ul.navbar-nav a {
color: #FF7D36;
border-style: solid;
border-width: 0 0 2px 0;
border-color: #000;
}
nav.navbar ul.navbar-nav a:hover,
nav.navbar ul.navbar-nav a:visited,
nav.navbar ul.navbar-nav a:focus,
nav.navbar ul.navbar-nav a:active {
background: #000;
}

nav.navbar ul.navbar-nav a {
text-decoration: none;
position: relative;
display: inline-block;
}
nav.navbar ul.navbar-nav a::after {
content: '';
width: 80%;
position: absolute;
top: 100%;
left: 10%;
height: 2px;
background-color: #FF7D36;
transform: scaleX(0);
transition: transform .35s ease;
}
nav.navbar ul.navbar-nav a:hover::after {
transform: scaleX(1);
}

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Basic Bootstrap Template</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Optional Bootstrap theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
</head>
<body>
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Navigation ein-/ausblenden</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Projekt-Titel</a>
</div>
<div id="navbar" class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="active"><a href="#">Start</a></li>
<li><a href="#ueber">Über</a></li>
<li><a href="#kontakt">Kontakt</a></li>
</ul>
</div><!--/.nav-collapse -->
</div>
</nav>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</body>
</html>




Answer

I commented line in your snippet:

nav.navbar ul.navbar-nav a {
    text-decoration: none;
    position: relative;
    /* display: inline-block; */
}

With that line, you overrode this bootstrap rule:

.nav > li > a {
    position: relative;
    display: block;
    padding: 10px 15px;
}

You have ran in problem of CSS specificity.

Here is corrected snippet:

/* Navbar */
.navbar-fixed-top{
    top: 80px;
}
.navbar-default{ /* assigning the top bar to the entire navbar div element */
    background-color: #4F4F4F;
}
nav.navbar {
    box-shadow: 0 0 2px 0 #ccc;
}
nav.navbar a {
    color: #FF7D36;
}
nav.navbar ul.navbar-nav a {
    color: #FF7D36;
    border-style: solid;
    border-width: 0 0 2px 0;
    border-color: #000;
}
nav.navbar ul.navbar-nav a:hover,
nav.navbar ul.navbar-nav a:visited,
nav.navbar ul.navbar-nav a:focus,
nav.navbar ul.navbar-nav a:active {
    background: #000;
}

nav.navbar ul.navbar-nav a {
    text-decoration: none;
    position: relative;
    /* display: inline-block; */
}
nav.navbar ul.navbar-nav a::after {
    content: '';
    width: 80%;
    position: absolute;
    top: 100%;
    left: 10%;
    height: 2px;
    background-color: #FF7D36;
    transform: scaleX(0);
    transition: transform .35s ease;
}
nav.navbar ul.navbar-nav a:hover::after {
    transform: scaleX(1);
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Basic Bootstrap Template</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Optional Bootstrap theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
</head>
<body>
    <nav class="navbar navbar-inverse navbar-fixed-top">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
            <span class="sr-only">Navigation ein-/ausblenden</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="#">Projekt-Titel</a>
        </div>
        <div id="navbar" class="collapse navbar-collapse">
          <ul class="nav navbar-nav">
            <li class="active"><a href="#">Start</a></li>
            <li><a href="#ueber">Über</a></li>
            <li><a href="#kontakt">Kontakt</a></li>
          </ul>
        </div><!--/.nav-collapse -->
      </div>
    </nav>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</body>
</html>