Bootstrap 4下拉菜单显示在父级的右侧

时间:2019-07-05 17:42:27

标签: css bootstrap-4

我是Bootstrap的新用户,对我的导航栏有疑问。 这是我的测试站点。 TEST-DOMAIN

我目前正在将Bootstrap 4.3.1与JQuery 3.4.1结合使用。

菜单的外观和功能在桌面上都很棒。下拉出现按预期方式。我在下拉菜单中添加了列,以进行各种超级菜单。

当网站过渡到移动网站时,下拉菜单显示在父菜单项的“右侧”。有没有办法使它显示在父项的下方,并将菜单的其余部分向下推到其下方?

我尝试将下拉列表设置为Display:absolute,这在其他关于此的文章中找到。但是,尽管将下拉菜单如我所愿地向左移动,但它却将其浮动在菜单其余部分的上方。我希望它向左对齐,同时将菜单的其余部分推到下面。

感谢您的帮助!

*编辑-添加代码*

<!--Navbar-->
<nav id="navbar" class="navbar navbar-expand-lg navbar-dark nav-transparent sticky-top">
<div class="container">
    <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
    </button>
    <a class="navbar-brand" href="#" style="margin-top: -5px;"><strong>Test Website</strong></a>
    <div class="collapse navbar-collapse" id="navbarNav">
        <ul class="navbar-nav mr-center">
            <li class="nav-item active">
                <a class="nav-link" href="home.html">Home <span class="sr-only">(current)</span></a>
            </li>

            <li class="nav-item dropdown btn-group">
                <a class="nav-link dropdown-toggle" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Services</a>
                <div class="dropdown-menu dropdown" aria-labelledby="dropdownMenu1">
                    <div class="container">
                        <div class="row">
                            <div class="col-md-4">
                                <a class="dropdown-item" href="another-page.html">ANOTHER PAGE</a>
                                <a class="dropdown-item" href="pools.html">POOLS</a>
                                <a class="dropdown-item" href="test-10.html">TEST 10</a>
                                <a class="dropdown-item" href="test-5.html">TEST 5</a>
                            </div>
                            <div class="col-md-4">
                                <a class="dropdown-item" href="test-6.html">TEST 6</a>
                                <a class="dropdown-item" href="test-7.html">TEST 7</a>
                                <a class="dropdown-item" href="test-8.html">TEST 8</a>
                                <a class="dropdown-item" href="test-9.html">TEST 9</a>
                            </div>
                            <div class="col-md-4">
                                <a class="dropdown-item" href="yet-another-page.html">YET ANOTHER PAGE</a>
                                <a class="dropdown-item" href="awesome-page.html">AWESOME PAGE</a>
                            </div>
                        </div>
                    </div>
                </div>
            </li>
            <li class="nav-item"><a class="nav-link" href="about.html">About Us</a></li>
            <li class="nav-item"><a class="nav-link" href="gallery.html">Gallery</a></li>
            <li class="nav-item"><a class="nav-link" href="faq.html">FAQ</a></li>
            <li class="nav-item"><a class="nav-link" href="kitchen-vision.html">KITCHEN VISION</a></li>
            <li class="nav-item"><a class="nav-link" href="contact.html">Contact Us</a>
            </li>            
        </ul>
    </div>
</div>

我已经删除了所有用于NavBar的自定义CSS,以确保我所做的编辑没有做错任何事情。

1 个答案:

答案 0 :(得分:0)

@media screen and (max-width: 768px) {
  .mx-auto.d-flex {
    margin: 0!important;
    justify-content: space-between;
    display: flex;
    width: 100vw;
  }
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">


<nav class="navbar navbar-expand-lg navbar-light bg-light sticky-top">
  <div class="mx-auto d-flex">
    <a class="navbar-brand" href="#">Test Website</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">
      <ul class="navbar-nav ">
        <li class="nav-item active">
          <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
        </li>
        <li class="nav-item dropdown">
          <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
          Services
        </a>
          <div class="dropdown-menu" aria-labelledby="navbarDropdown">
            <div class="container">
              <div class="row">
                <div class="col-md-4">
                  <a class="dropdown-item" href="another-page.html">ANOTHER PAGE</a>
                  <a class="dropdown-item" href="pools.html">POOLS</a>
                  <a class="dropdown-item" href="test-10.html">TEST 10</a>
                  <a class="dropdown-item" href="test-5.html">TEST 5</a>
                </div>
                <div class="col-md-4">
                  <a class="dropdown-item" href="test-6.html">TEST 6</a>
                  <a class="dropdown-item" href="test-7.html">TEST 7</a>
                  <a class="dropdown-item" href="test-8.html">TEST 8</a>
                  <a class="dropdown-item" href="test-9.html">TEST 9</a>
                </div>
                <div class="col-md-4">
                  <a class="dropdown-item" href="yet-another-page.html">YET ANOTHER PAGE</a>
                  <a class="dropdown-item" href="awesome-page.html">AWESOME PAGE</a>
                </div>
              </div>
            </div>
          </div>
        </li>
        <li class="nav-item"><a class="nav-link" href="about.html">About Us</a></li>
        <li class="nav-item"><a class="nav-link" href="gallery.html">Gallery</a></li>
        <li class="nav-item"><a class="nav-link" href="faq.html">FAQ</a></li>
        <li class="nav-item"><a class="nav-link" href="kitchen-vision.html">KITCHEN VISION</a></li>
        <li class="nav-item"><a class="nav-link" href="contact.html">Contact Us</a>
        </li>
      </ul>
    </div>
  </div>
</nav>