徽章增加导航栏的高度

时间:2019-04-02 17:16:10

标签: css twitter-bootstrap twitter-bootstrap-3 navbar badge

我具有以下用于引导导航栏的HTML。

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
<style type="text/css">
/* Move down content because we have a fixed navbar that is 50px tall */

body {
  padding-top: 33px;
  padding-bottom: 20px;
}

.body {
  font-size: 16px;
  border-spacing: 2px;
  border-collapse: separate;
  text-align: center;
}

.body a {
  text-decoration: underline;
}

.navbar-fixed-top {
  background-color: #5b5b5b;
  min-height: 33px;
  border-style: none;
}

.navbar-toggle {
  margin-top: 2px;
  margin-bottom: 2px;
  padding-top: 7px;
  padding-bottom: 7px;
}

.navbar-nav>li>a {
  line-height: 3px;
  padding-top: 16px;
  padding-bottom: 14px;
}

div.navbar-header>img {
  height: 30px;
  margin-top: 2px;
}

.nav>li>a {
  font-size: medium;
  font-family: "Helvetica Bold", sans-serif;
  /*font-weight: bold;*/
  text-transform: uppercase;
}

.navbar-inverse .navbar-nav>li>a {
  color: #f0eed0;
}

.navbar-inverse .navbar-nav>li>a:hover {
  color: #f8dc54;
}

.nav li {
  display: inline;
}

#vugraph-menu.inprogress>a {
  color: black;
  background-color: #ecbe23;
}

#vugraph-menu.inprogress>a:hover,
#vugraph-menu.inprogress.open>a:not(:hover) {
  color: white;
}
</style>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<nav class="navbar navbar-inverse navbar-fixed-top">
  <div class="container">

    <div class="navbar-header">
      <img src="http://www.bridgebase.com/images/bbo_red.png">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
    </div>

    <div id="navbar" class="nav navbar-collapse collapse">
      <div class="container">
        <ul class="nav navbar-nav">
          <li id="vugraph-menu" class="nav-item dropdown" title="No tables in play">
            <a id="navbar-vugraph-menu-link" class="nav-link dropdown-toggle" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Vugraph <span id="vugraph-count" class="badge badge-notify"></span></a>
            <ul class="dropdown-menu" aria-labelledby="navbar-vugraph-menu-link">
              <li class="dropdown-item"><a href="//www.bridgebase.com/v3/?vugraph=y" target="_blank">Watch</a></li>
              <li class="dropdown-item"><a href="//www.bridgebase.com/vugraph/schedule.php" target="_blank">Schedule</a></li>
            </ul>
          </li>
          <li class="nav-item"><a class="nav-link" href="//www.bridgebase.com/myhands/index.php" target="_blank">Hand Records</a></li>
          <li class="nav-item"><a class="nav-link" href="//www.bridgebase.com/points/index.php" target="_blank">Masterpoints</a></li>
          <li class="nav-item"><a class="nav-link" href="https://www.bridgebase.com/purchase/pay.php" target="_blank">BB$</a></li>
        </ul>
      </div>
    </div>
  </div>
</nav>

没有徽章,它看起来像:

enter image description here

我有JavaScript,可以动态填充#vugraph-count徽章。包含内容时,导航栏的高度增加15个像素以上。 HTML变为:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
<style type="text/css">
/* Move down content because we have a fixed navbar that is 50px tall */

body {
  padding-top: 33px;
  padding-bottom: 20px;
}

.body {
  font-size: 16px;
  border-spacing: 2px;
  border-collapse: separate;
  text-align: center;
}

.body a {
  text-decoration: underline;
}

.navbar-fixed-top {
  background-color: #5b5b5b;
  min-height: 33px;
  border-style: none;
}

.navbar-toggle {
  margin-top: 2px;
  margin-bottom: 2px;
  padding-top: 7px;
  padding-bottom: 7px;
}

.navbar-nav>li>a {
  line-height: 3px;
  padding-top: 16px;
  padding-bottom: 14px;
}

div.navbar-header>img {
  height: 30px;
  margin-top: 2px;
}

.nav>li>a {
  font-size: medium;
  font-family: "Helvetica Bold", sans-serif;
  /*font-weight: bold;*/
  text-transform: uppercase;
}

.navbar-inverse .navbar-nav>li>a {
  color: #f0eed0;
}

.navbar-inverse .navbar-nav>li>a:hover {
  color: #f8dc54;
}

.nav li {
  display: inline;
}

#vugraph-menu.inprogress>a {
  color: black;
  background-color: #ecbe23;
}

#vugraph-menu.inprogress>a:hover,
#vugraph-menu.inprogress.open>a:not(:hover) {
  color: white;
}
</style>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<nav class="navbar navbar-inverse navbar-fixed-top">
  <div class="container">

    <div class="navbar-header">
      <img src="http://www.bridgebase.com/images/bbo_red.png">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
    </div>

    <div id="navbar" class="nav navbar-collapse collapse">
      <div class="container">
        <ul class="nav navbar-nav">
          <li id="vugraph-menu" class="nav-item dropdown" title="No tables in play">
            <a id="navbar-vugraph-menu-link" class="nav-link dropdown-toggle" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Vugraph <span id="vugraph-count" class="badge badge-notify">1</span></a>
            <ul class="dropdown-menu" aria-labelledby="navbar-vugraph-menu-link">
              <li class="dropdown-item"><a href="//www.bridgebase.com/v3/?vugraph=y" target="_blank">Watch</a></li>
              <li class="dropdown-item"><a href="//www.bridgebase.com/vugraph/schedule.php" target="_blank">Schedule</a></li>
            </ul>
          </li>
          <li class="nav-item"><a class="nav-link" href="//www.bridgebase.com/myhands/index.php" target="_blank">Hand Records</a></li>
          <li class="nav-item"><a class="nav-link" href="//www.bridgebase.com/points/index.php" target="_blank">Masterpoints</a></li>
          <li class="nav-item"><a class="nav-link" href="https://www.bridgebase.com/purchase/pay.php" target="_blank">BB$</a></li>
        </ul>
      </div>
    </div>
  </div>
</nav>

和呈现的导航栏如下:

enter image description here

(我不得不将自定义CSS内联到代码段的HTML框中,因为否则bootstrap.min.css会优先于自定义样式。)您需要使用“自定义样式”的“全页”选项堆栈片段以获取未折叠的导航栏。

该问题似乎与.navbar-nav>li>a样式line-height: 3px有关。当我删除它时,导航栏总是太高。这用于减少导航栏高度以匹配左侧的徽标。由于某种原因,添加徽章后,它会覆盖它(尽管在DOM检查器中并未删除)。

2 个答案:

答案 0 :(得分:2)

badge不会引起问题,您可以检查带有两个导航栏的下一个示例(一个导航栏带有一个填充有数字的徽章,另一个导航栏没有类似的标记),两个导航栏都具有相似的height(例如克里斯·巴已经指出)。所以:

1)某些自定义CSS可能是一个问题,在这种情况下,我建议使用检查器工具检查您的{{1 }}或badge

2)您可以做的另一件事是检查您是否具有Bootstrap Navbar中所述的结构良好的导航栏。

navbar

最后,我不认为更新到<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> <!-- Optional theme --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous"> <!-- Latest compiled and minified JavaScript --> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script> <nav class="navbar navbar-inverse navbar-fixed-top"> <div class="container"> <!-- Brand and collapse button --> <div class="navbar-header"> <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar1"> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="navbar-brand" href="#">Navbar 1</a> </div> <div id="navbar1" class="navbar-collapse collapse"> <ul class="nav navbar-nav"> <li id="vugraph-menu" class="nav-item dropdown" title="No tables in play"> <a id="navbar-vugraph-menu-link" class="nav-link dropdown-toggle" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> Vugraph <span id="vugraph-count" class="no-padding badge badge-notify">99</span> </a> <ul class="dropdown-menu" aria-labelledby="navbar-vugraph-menu-link"> <li class="dropdown-item"> <a href="//www.bridgebase.com/v3/?vugraph=y" target="_blank">Watch</a> </li> <li class="dropdown-item"> <a href="//www.bridgebase.com/vugraph/schedule.php" target="_blank">Schedule</a> </li> </ul> </li> <li class="nav-item"> <a class="nav-link" href="//www.bridgebase.com/myhands/index.php" target="_blank">Hand Records</a> </li> <li class="nav-item"> <a class="nav-link" href="//www.bridgebase.com/points/index.php" target="_blank">Masterpoints</a> </li> <li class="nav-item"> <a class="nav-link" href="https://www.bridgebase.com/purchase/pay.php" target="_blank">BB$</a> </li> </ul> </div> </div> </nav> <nav class="navbar navbar-inverse navbar-fixed-bottom"> <div class="container"> <!-- Brand and collapse button --> <div class="navbar-header"> <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar2"> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="navbar-brand" href="#">Navbar 2</a> </div> <div id="navbar2" class="navbar-collapse collapse"> <ul class="nav navbar-nav"> <li id="vugraph-menu" class="nav-item dropdown" title="No tables in play"> <a id="navbar-vugraph-menu-link" class="nav-link dropdown-toggle" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> Vugraph <span id="vugraph-count" class="no-padding badge badge-notify"></span> </a> <ul class="dropdown-menu" aria-labelledby="navbar-vugraph-menu-link"> <li class="dropdown-item"> <a href="//www.bridgebase.com/v3/?vugraph=y" target="_blank">Watch</a> </li> <li class="dropdown-item"> <a href="//www.bridgebase.com/vugraph/schedule.php" target="_blank">Schedule</a> </li> </ul> </li> <li class="nav-item"> <a class="nav-link" href="//www.bridgebase.com/myhands/index.php" target="_blank">Hand Records</a> </li> <li class="nav-item"> <a class="nav-link" href="//www.bridgebase.com/points/index.php" target="_blank">Masterpoints</a> </li> <li class="nav-item"> <a class="nav-link" href="https://www.bridgebase.com/purchase/pay.php" target="_blank">BB$</a> </li> </ul> </div> </div> </nav>会有所帮助,实际上,您必须将整个网站移植到新版本,因为有些事情已经从版本Bootstrap 4更改为3(了解更多here)。


更新

使用在元素上应用的自定义样式更新问题后,可能的解决方案是向4添加下一个样式:

badge

示例:

{line-height: 0; display: inline;}

答案 1 :(得分:1)

查看此快速测试,该测试可以测量带有和不带有徽章的导航项目的高度。差别很小,但差别不大。只有0.04像素。

您可能还有其他干扰CSS的CSS,使徽章更高了。如果将.badge的填充或行高设置得较小,则两个导航项的高度将相同。请参阅注释掉的CSS代码,取消注释可解决此处演示中的问题

document.querySelectorAll('.nav-item').forEach((el) => {
  console.log(el.innerText + ' Height: ' + el.getBoundingClientRect().height + 'px')
});
.nav-item {
  border: 1px solid red;
}

.nav-item .badge {
  /*line-height: 90%;*/
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css" />

<div id="navbar" class="nav navbar-collapse">
  <div class="container">
    <ul class="nav navbar-nav">
      
      <li class="nav-item dropdown" title="No tables in play">
        <a id="navbar-vugraph-menu-link" class="nav-link dropdown-toggle" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">No Badge</a>
      </li>
      
      <li class="nav-item dropdown" title="No tables in play">
        <a id="navbar-vugraph-menu-link" class="nav-link dropdown-toggle" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Has Badge <span id="vugraph-count" class="badge badge-notify">1</span></a>
      </li>
    </ul>
  </div>
</div>