响应式Bootstrap 4导航栏下拉项显示为导航项

时间:2018-11-24 00:01:45

标签: drop-down-menu bootstrap-4 navbar responsive

您好,在此先感谢您的帮助。以下是指向相关网站的链接:caulfield.co/test/originals.html

我正在尝试创建一个响应式navbar,其中当前dropdown-items在折叠后显示为标准nav-items

查看此图片:

Dropdown displaying correctly on desktop view

dropdown-items在桌面上按预期显示。但是,在移动设备上,不需要单独的下拉列表。开箱即用的Bootstrap 4的显示方式如下:

Minimized Mobile navbar with unnecessary dropdown

是否有人知道一种方便或自定义的解决方案,一旦dropdown在移动设备等上使用,就删除了navbar-collapse,使dropdown-items看起来像navbar-items ?理想情况下,“更多” nav-link应该display:nonenav-items可以从“联系”无缝继续到“在显示器上工作”。

HTML:

<nav id="navHome" class="navbar navbar-expand-lg navbar-light sticky-top">
  <div class="container-fluid max-width-940">
  <a class="navbar-brand" href="index.html">
    <img src="images/mb.svg" alt="Margaret Biggs" width="220px"/>
  </a>
  <button class="navbar-toggler togglerNoBorder" 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 class="collapse navbar-collapse backgroundWhite" id="navbarNavDropdown">
    <ul class="navbar-nav ml-auto">
      <li class="nav-item active">
        <a class="nav-link" href="bio.html">Bio <span class="sr-only">(current)</span></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="originals.html">Originals</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="exhibition.html">Exhibition</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="prints.html">Prints</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="professionals.html">For Professionals</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="contact.html">Contact</a>
      </li>
      <li class="nav-item dropdown navDropdown">
        <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
          More
        </a>
        <div class="dropdown-menu navDropdown" aria-labelledby="navbarDropdownMenuLink">
          <a class="dropdown-item" href="works-on-display.html">Works on Display</a>
          <a class="dropdown-item" href="poetry.html">Poetry</a>
          <a class="dropdown-item" href="commissions.html">Commissions</a>
          <a class="dropdown-item" href="blog.html">Blog</a>
        </div>
      </li>
    </ul>
  </div>
</div>
</nav>

CSS:

.navbar-right {
  float:right;
}

.togglerNoBorder{
  border: 0px solid transparent;
}

.nav940{
  max-width:940px;
  margin:0 auto;
}

#navHome {
  color:rgb(34, 34, 34);
  font-size:13px;
  font-weight:400;
  line-height:16px;
  text-transform:uppercase;
  background-color:white;
  height:60px;
  box-shadow: 0 0 18px -4px #000;
}

#navHome a{
  color:rgb(34, 34, 34);
}

#navHome a:hover{
  color:#165fa5;
}

.navDropdown {
  font-size:13px;
}

.navDropdown a:hover {
  background-color:white;
}

.backgroundWhite{
  background-color:white;
}

.nav-item{
  padding-left:10px;
}

.dropdown-item {
  padding-top:10px;
}

.navbar-brand {
  margin-bottom:3px;
}

.dropdown-menu {
  top:45px;
}

2 个答案:

答案 0 :(得分:1)

使用@media移动查询(<992px)将dropdown-menu显示为常规导航链接...

@media (max-width: 992px) {
    .dropdown-toggle {
        display: none;
    }

    .dropdown-menu {
        display: block;
        position: relative;
        border-width: 0;
        padding: 0;
        margin: 0;
    }

    .dropdown-item {
        padding: .5rem 0rem;
        color: rgba(0,0,0,.5);
    }
}

https://www.codeply.com/go/lAAQOwhAx0

答案 1 :(得分:0)

基于@Zim 的出色回答,我稍微修改了样式以适合在深色导航栏中并排放置。用 Bootstrap 5 测试过,但我想它应该也适用于 Bootstrap 4。

@media (max-width: 992px) {
.dropdown-toggle {
    display: none;
}

.dropdown-menu {
    position: relative;
    display: contents;
    border-width: 0;
    padding: 0;
    margin: 0;
    color: transparent;
}

.dropdown-item {
    color: white;
    background-color: black;
    display: inline;
}

}