获得Bootstrap 4导航栏品牌徽标以独立于导航栏切换按钮进行对齐

时间:2019-06-06 22:34:49

标签: javascript css bootstrap-4 hamburger-menu

我正在尝试使导航栏品牌居中对齐;

对于移动设备,我已经右对齐的navbar-toggler正在将徽标偏离中心。

是否有可能使这两个类彼此独立对齐,如果可以,如何?

<nav class="navbar navbar-expand-md navbar-light fixed-top" id="mainNav">
    <a href="index.php#page-top" class="navbar-brand mx-auto PElogo">
        <img src="https://www.theharbourhousehotel.co.uk/img/LOGO-HH-PNG.png" alt="Harbour House Hotel Logo" title="Harbour House Hotel">
        <h1>Harbour House Hotel</h1>
        <h2>Portpatrick</h2>
    </a>

    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#HHnavbar" aria-controls="HHnavbar" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
    </button>

    <div id="HHnavbar" class="navbar-collapse collapse">
        <ul class="navbar-nav mr-auto">
            <li class="nav-item"><a class="nav-link js-scroll-trigger" href="#About">About</a></li>
            <li class="nav-item"><a class="nav-link js-scroll-trigger" href="#Rooms">Rooms</a></li>
        </ul>
    </div>
</nav>

JS Fiddle在这里: https://jsfiddle.net/nathonjones/va9g03tj/3/

谢谢。

1 个答案:

答案 0 :(得分:0)

在切换按钮(或汉堡菜单)上,我们知道:

  • 汉堡菜单的宽度为30px
  • 在左侧和右侧分别填充12px
  • 每个左/右边框为1 px

所以...如果我们将这30px + 2 *(12px)+ 2 *(1px)= 56px ... 向左添加56px填充,将确保我们的品牌始终处于导航的“真实”中间...

下面的工作片段

.navbar-brand img {
  width: 90px;
}

.PElogo {
  color: #000;
  text-align: center;
  text-transform: uppercase;
  margin-top: 30px;
  padding-left: 56px !important;
}

.PElogo img {
  width: 90px;
}

.PElogo h1 {
  font-size: 1.1rem;
  margin: .5rem 0 0;
}

.PElogo h2 {
  font-size: .675rem;
}
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>

<body>
  <nav class="navbar navbar-expand-md navbar-light fixed-top" id="mainNav">
    <a href="index.php#page-top" class="navbar-brand mx-auto PElogo">
      <img src="https://www.theharbourhousehotel.co.uk/img/LOGO-HH-PNG.png" alt="Harbour House Hotel Logo" title="Harbour House Hotel">
      <h1>Harbour House Hotel</h1>
      <h2>Portpatrick</h2>
    </a>

    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#HHnavbar" aria-controls="HHnavbar" aria-expanded="false" aria-label="Toggle navigation">
			<span class="navbar-toggler-icon"></span>
		</button>

    <div id="HHnavbar" class="navbar-collapse collapse">
      <ul class="navbar-nav mr-auto">
        <li class="nav-item"><a class="nav-link js-scroll-trigger" href="#About">About</a></li>
        <li class="nav-item"><a class="nav-link js-scroll-trigger" href="#Rooms">Rooms</a></li>
        <li class="nav-item"><a class="nav-link js-scroll-trigger" href="#Restaurant">Restaurant</a></li>
        <li class="nav-item"><a class="nav-link js-scroll-trigger" href="#Attractions">Attractions</a></li>
        <li class="nav-item"><a class="nav-link js-scroll-trigger" href="#EventsNewsOffers">News</a></li>
        <li class="nav-item"><a class="nav-link js-scroll-trigger" href="#Location">Location</a></li>
        <li class="nav-item"><a class="nav-link js-scroll-trigger" href="#Gallery">Gallery</a></li>
        <li class="nav-item"><a class="nav-link js-scroll-trigger" href="contact-harbour-house-hotel.php">Contact</a></li>
      </ul>
    </div>
  </nav>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/js/bootstrap.bundle.min.js"></script>
</body>

</html>