如何在引导固定导航栏中移动项目

时间:2019-03-25 19:10:31

标签: html css twitter-bootstrap

我正在尝试使用Bootstrap 4创建一个固定的导航栏,但是我很难按照自己的喜好对齐LOGO和MENU项。

我知道这里有ml-auto,mr-auto,我是否可以向其中添加像素,以便按照自己的意愿精确地移动它?

这是我的导航栏的样子:

enter image description here

我希望将LOGO和MENU项目移到屏幕中间。可能从左右两边的100px开始,因此LOGO和MENU靠得很近。这是我的代码:

<nav class="navbar navbar-expand-lg navbar-dark sticky-dark bg-dark">
        <a class="navbar-brand" href="#">LOGO</a>
        <button class="navbar-toggler " type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div id="navbarNavDropdown" class="navbar-collapse collapse">

            <ul class="navbar-nav ml-auto"> 
                <li class="nav-item">
                    <a class="nav-link" href="#">HOME</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">ABOUT</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">SERVICES</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">CONTACT</a>
                </li>
            </ul>
        </div>
    </nav>

1 个答案:

答案 0 :(得分:0)

您可以将填充添加到主要.navbar元素中。然后使用媒体查询控制间距。

.navbar {
  padding: 0.5em 100px !important;
}

@media only screen and (max-width: 768px) {
  .navbar {
    padding: 0.5em 1em !important;
  }
}
<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.3.1/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.min.css" />
<nav class="navbar navbar-expand-lg navbar-dark sticky-dark bg-dark">
  <a class="navbar-brand" href="#">LOGO</a>
  <button class="navbar-toggler " type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation"><span class="navbar-toggler-icon"></span></button>
  <div id="navbarNavDropdown" class="navbar-collapse collapse">

    <ul class="navbar-nav ml-auto">
      <li class="nav-item">
        <a class="nav-link" href="#">HOME</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">ABOUT</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">SERVICES</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">CONTACT</a>
      </li>
    </ul>
  </div>
</nav>