调整浏览器大小时,引导Navbar链接重叠

时间:2018-10-28 23:31:10

标签: javascript html css twitter-bootstrap navbar

我在中间做了Bootstrap Navbar徽标。这是结果的图片:

enter image description here

但是我有1个问题:  调整浏览器大小时,导航链接似乎相互重叠。

enter image description here

我正在寻找的是当我调整浏览器的大小时,链接保持相对彼此并且直到重叠在引导切换按钮中折叠才重叠。这里的解决方案是什么?

这是HTML和CSS

html,
body {
  height: 100%;
  width: 100%;
  font-family: 'Arial', 'Poppins', sans-serif;
  background-color: #2d2d2d;
  margin: 0;
}

.navbar {
  width: 100%;
  z-index: 999;
  background: #2d2d2d;
  ;
  margin-top: 0;
  padding: 2em;
  /* display: flex; */
}

.navbar .nav-link:hover {
  color: rgba(255, 185, 197, 0.986) !important;
}

.navbar-brand {
  transform: translateX(-50%);
  left: 50%;
  top: .1%;
  position: absolute;
}

.navbar-nav li {
  padding-right: 40px;
  font-family: 'Myriad Pro';
  font-weight: bold;
  /* display: inline-block; */
}

#mt-nav-left {
  position: absolute;
  transform: translateX(60%);
  right: 60%;
}

#mt-nav-right {
  position: absolute;
  transform: translateX(-65%);
  left: 65%;
}

#banner {
  overflow: auto;
  z-index: 998;
}

#banner img {
  width: 100%;
  object-fit: cover;
}

@media (max-width: 1024px) {
  #mt-nav-right {
    position: static;
  }
}

@media (max-width: 991px) {
  #mt-nav-left {
    position: relative;
  }
  #mt-nav-right {
    position: relative;
  }
}
<nav class="navbar navbar-expand-lg navbar-dark sticky-top">
  <button class="navbar-toggler ml-auto custom-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
                    <span class="toggle">
                      <i class="fas fa-bars" style="font-size: 21px;"></i>
                    </span>
                  </button>

  <a class="navbar-brand" href="index.html">
    <img class="img-responsive" src="img/logo.png" width="85px" height="85px" class="d-inline-block align-top">
  </a>
  </div>
  <div class="collapse navbar-collapse" id="navbarNav">
    <ul class="nav navbar-nav ml-auto" id="mt-nav-left">
      <li class="nav-item" id="mt-navbar-link">
        <a class="nav-link scroll" href="index.html">HOME <span class="sr-only">(current)</span></a>
      </li>
      <li class="nav-item" id="mt-navbar-link">
        <a class="nav-link scroll" href="#">ABOUT</a>
      </li>
    </ul>

    <ul class="nav navbar-nav ml-auto" id="mt-nav-right">
      <li class="nav-item" id="mt-navbar-link">
        <a class="nav-link scroll" href="#">CONTACT</a>
      </li>
      <li class="nav-item" id="mt-navbar-link">
        <a class="nav-link scroll" href="#">VACANCIES</a>
      </li>
    </ul>
  </div>

</nav>

2 个答案:

答案 0 :(得分:0)

您可以使用flex-box解决它。想法是在两个具有以下属性的列表之间放置一个空框

flex-shrink: 0;
flex-basis: 100px //(not less then the width of your logo);

看看我的片段。

.navbar-brand {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
}

.menu {
  width: 100%;
}
.menu .navbar-nav {
  flex-grow: 1;
}


/*
  THIS CLASS PREVENTS OVERLAPING
*/

.menu .separator {
  flex-grow: 1;
  flex-shrink: 0;
  flex-basis: 100px;
  max-width: 250px;
  
  background: orange;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>

<nav class="navbar navbar-expand-sm navbar-light bg-light">
      <a class="navbar-brand" href="#">Navbar</a>
      <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>
    
      <div class="collapse navbar-collapse" id="navbarSupportedContent">
        <div class="d-flex menu">
          <ul class="navbar-nav mr-auto justify-content-end">
            <li class="nav-item active">
              <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="#">Link</a>
            </li>
          </ul>
          <!-- Box that prevents overlaping -->
          <div class="separator">
          </div>
          <ul class="navbar-nav mr-auto">
            <li class="nav-item active">
              <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="#">Link</a>
            </li>
          </ul>
        </div>
        
      </div>
    </nav>

答案 1 :(得分:0)

这是我使用Bootstrap 4使用导航栏指定规格的方法。

1)有两个 navbars-collapse 项目,第一个项目的两端对齐,第二个项目的默认端对齐(我们将分别称为 A C

2)品牌(称为 B )将使用类 mx-auto 居中,接近导航栏的效果使用下一种样式(仅在非折叠模式下使用):

@media (min-width: 768px) {

  .navbar {
    max-height: 75px;
  }

  .navbar-brand {
    bottom: -30px;
    position: relative;
  }
}

3)我们使用Order classes管理项目( A,B和C )的顺序,在小屏幕设备上,该顺序为 B-> A-> C ,在大屏幕设备上将为 A-> B-> C 。我们还使用Spacing utilities p classes在项目之间添加一些填充。

这只是我的主意,您可以检查下一个示例(在全屏模式下)并使用浏览器的宽度播放。希望对您有帮助...

.navbar {
  z-index: 999;
  background: #2d2d2d;
}

.navbar .nav-link:hover {
  color: rgba(255, 185, 197, 0.986) !important;
}

.navbar-nav li {
  font-family: 'Myriad Pro';
  font-weight: bold;
}

@media (min-width: 768px) {

  .navbar {
    max-height: 75px;
  }

  .navbar-brand {
    bottom: -30px;
    position: relative;
  }
}
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.4.2/css/all.css" integrity="sha384-/rXc/GQVaYpyDdyxK+ecHPVYJSN9bmVFBvjA/9eOB+pb3F2w2N6fc5qB9Ew5yIns" crossorigin="anonymous">

<nav class="navbar navbar-expand-md navbar-dark sticky-top w-100">

  <button class="navbar-toggler custom-toggler" type="button" data-toggle="collapse" data-target=".dual-nav">
    <span class="toggle">
      <i class="fas fa-bars" style="font-size:21px;"></i>
    </span>
  </button>

  <div class="navbar-collapse collapse dual-nav justify-content-end order-1 order-md-1">
    <ul class="navbar-nav">
      <li class="nav-item p-2">
        <a class="nav-link scroll" href="index.html">
          HOME <span class="sr-only">(current)</span>
        </a>
      </li>
      <li class="nav-item p-2" id="mt-navbar-link">
        <a class="nav-link scroll" href="#">ABOUT</a>
      </li>
    </ul>
  </div>

  <a class="navbar-brand mx-auto order-0 order-md-2 p-3" href="index.html">
    <img class="img-responsive" src="https://upload.wikimedia.org/wikipedia/commons/d/d0/Newscycle-Circle.png" width="85px" height="85px">
  </a>

  <div class="navbar-collapse collapse dual-nav order-2 order-md-3">
    <ul class="navbar-nav">
      <li class="nav-item p-2" id="mt-navbar-link">
        <a class="nav-link scroll" href="#">CONTACT</a>
      </li>
      <li class="nav-item p-2" id="mt-navbar-link">
        <a class="nav-link scroll" href="#">VACANCIES</a>
      </li>
    </ul>
  </div>

</nav>