自定义高度导航栏的Bootstrap 4折叠问题

时间:2019-12-10 11:42:37

标签: html css twitter-bootstrap bootstrap-4

我在Bootstrap 4中使用带有品牌徽标图像(150px x 33px)的导航栏。

我需要将导航栏的高度增加到80px。我使用min-height: 80px;实现了这一目标。


<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <title>Title</title>
  <link href="../system/bootstrap/dist/css/bootstrap.css" rel="stylesheet">

  <style>
    .navbar {
      min-height: 80px;
      background-color: #FFFFFF;
      border-bottom-color: #D8D8DD;
      border-bottom-style: solid;
      border-bottom-width: 1px;
    }
  </style>
</head>

<body>
  <nav class="navbar navbar-expand-lg fixed-top navbar-light">

    <a class="navbar-brand" href="#"><img src="logo-placeholder-150px-33px.png" ></a>

    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navigation01">
      <span class="navbar-toggler-icon"></span>
    </button>

    <div class="collapse navbar-collapse" id="navigation01">
      <ul class="navbar-nav">
        <li class="nav-item"><a class="nav-link active " href="#">Item 1</a></li>
        <li class="nav-item"><a class="nav-link" href="#">Item 2</a></li>
        <li class="nav-item"><a class="nav-link" href="#">Item 3</a></li>
      </ul>
    </div>
  </nav>

<script src="../system/jquery/dist/jquery.js"></script>
<script src="../system/popper.js/dist/popper.js"></script>
<script src="../system/bootstrap/dist/js/bootstrap.js"></script>

</body>
</html>

在较小的视口中,菜单正确折叠,并且汉堡包图标可见。

但是,当我通过单击汉堡包图标打开菜单时,徽标的Y位置和汉堡包图标会更改。他们向顶部稍微移动。

enter image description here

如何解决这个问题?

2 个答案:

答案 0 :(得分:2)

使用填充来增加导航栏的高度,而不是最小高度。它会解决您的问题。增加导航栏类的填充。

.navbar {
    padding: .6rem 1rem;
    background-color: #FFFFFF;
    border-bottom-color: #D8D8DD;
    border-bottom-style: solid;
    border-bottom-width: 1px;
}

答案 1 :(得分:1)

您想改为更改navbar-brand的最小高度,并从navbar删除填充(py-0),以获取80px的确切最小高度...

<nav class="navbar navbar-expand-lg fixed-top navbar-light py-0">
    <a class="navbar-brand align-items-center d-flex" href="#"><img src="//placehold.it/150x33"></a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navigation01">
        <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navigation01">
        <ul class="navbar-nav">
            <li class="nav-item"><a class="nav-link active " href="#">Item 1</a></li>
            <li class="nav-item"><a class="nav-link" href="#">Item 2</a></li>
            <li class="nav-item"><a class="nav-link" href="#">Item 3</a></li>
        </ul>
    </div>
</nav>

Demo