Boostrap 4不可折叠项目下拉菜单扩展了Navbar

时间:2019-03-07 13:21:01

标签: html css bootstrap-4

我从问题Bootstrap 4 - Navbar items outside the collapse中摘录了一段代码,该代码段帮助我在视口缩小时创建了几个不可折叠的项目。但是,在这些不可折叠项目之一中有一个下拉菜单,当该项目被展开时,它会展开整个导航栏,并且不会在其上方弹出。您可以在下面的示例int链接中看到它

http://codeply.com/go/U0GQYJd2JO

导航栏代码:

<nav class="navbar navbar-expand-lg fixed-top navbar-light bg-light" id="mainNav">
    <a class="navbar-brand" href="/">
        MY Site
    </a>
    <div class="d-flex flex-row order-2 order-lg-3">
        <ul class="navbar-nav flex-row">
            <li style="width:250px;">
                <select data-placeholder="Jump to site..." id="quickjump-menu" class="form-control form-control-chosen">
                    <option value=""></option>
                    <option value="/sites/site/139/">protein</option>
                    <option value="/sites/site/138/">shake</option>
                    <option value="/sites/site/159/">soup</option>
                    <option value="/sites/site/115/">test 1</option>
                    <option value="/sites/site/116/">test 2</option>
                </select>
            </li>
            <li class="nav-item dropdown">
                <a class="nav-link p-2 dropdown-toggle" data-toggle="dropdown" href="#" role="button"
                    aria-haspopup="true" aria-expanded="false">
                    <i class="fa fa-user fa-lg"></i>
                </a>
                <div class="dropdown-menu dropdown-menu-right">
                    <div class="dropdown-header text-center">
                        <strong>Admin</strong>
                    </div>
                    <a class="dropdown-item" href="/admin/" target='_blank'>
                        <i class="fa fa-user fa-fw"></i> Admin
                    </a>
                    <a class="dropdown-item" href="/user_info/">
                        <i class="fa fa-users fa-fw"></i> User Info
                    </a>
                    <div class="dropdown-header text-center">
                        <strong>Settings</strong>
                    </div>
                    <a class="dropdown-item" href="/app_settings/">
                        <i class="fa fa-gears fa-fw"></i> App Settings
                    </a>
                    <div class="dropdown-header text-center">
                        <strong>User</strong>
                    </div>
                    <a class="dropdown-item" href="/edit_profile/1/">
                        <i class="fa fa-user fa-fw"></i> User Profile
                    </a>
                    <a class="dropdown-item" href="/logout/?next=/login">
                        <i class="fa fa-gear fa-fw"></i> Log out
                    </a>
                </div>
            </li>
        </ul>
        <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse"
            data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false"
            aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>
    </div>
    <div class="collapse navbar-collapse" id="navbarResponsive">
        <ul class="navbar-nav navbar-sidenav" id="menu">
            <li>...</li>
        </ul>
    </div>
</nav>

视口缩小时的样本图像 enter image description here

3 个答案:

答案 0 :(得分:1)

只需将 position-absolute 类添加到dropdown-menu不需要额外的CSS。

<div class="dropdown-menu dropdown-menu-right position-absolute">
   ...
</div>

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

正如您在此Bootstrap 4 GitHub issue中所看到的那样,“移动”内部下拉列表的预期行为(一旦达到了expand- *断点以下),Navbar将包含下拉列表,并且不要漂浮在上面。

要改善导航栏布局,您可能还想在flex-row flex-nowrap上添加navbar-nav,以使其不会堆叠并使导航栏过高。

答案 1 :(得分:1)

您可以通过覆盖下拉菜单中设置的响应position: static来解决此问题:

css:

.navbar-expand-lg .navbar-nav .dropdown-menu {
  position: absolute;
}

答案 2 :(得分:1)

您需要覆盖引导程序默认CSS。请检查以下示例

@media (max-width: 991px) {
    .custom-navbar .navbar-nav .dropdown-menu {
        position: absolute;
    }
}
<!DOCTYPE html>
<html>
<head>
    <title></title>
    <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">
    <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.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</head>
<body>
    <nav class="navbar navbar-expand-lg fixed-top navbar-light bg-light custom-navbar" id="mainNav">
        <a class="navbar-brand" href="/">
            MY Site
        </a>
        <div class="d-flex flex-row order-2 order-lg-3">
            <ul class="navbar-nav flex-row">
                <li style="width:250px;">
                    <select data-placeholder="Jump to site..." id="quickjump-menu" class="form-control form-control-chosen">
                        <option value=""></option>
                        <option value="/sites/site/139/">protein</option>
                        <option value="/sites/site/138/">shake</option>
                        <option value="/sites/site/159/">soup</option>
                        <option value="/sites/site/115/">test 1</option>
                        <option value="/sites/site/116/">test 2</option>
                    </select>
                </li>
                <li class="nav-item dropdown">
                    <a class="nav-link p-2 dropdown-toggle" data-toggle="dropdown" href="#" role="button"
                        aria-haspopup="true" aria-expanded="false">
                        <i class="fa fa-user fa-lg"></i>
                    </a>
                    <div class="dropdown-menu dropdown-menu-right">
                        <div class="dropdown-header text-center">
                            <strong>Admin</strong>
                        </div>
                        <a class="dropdown-item" href="/admin/" target='_blank'>
                            <i class="fa fa-user fa-fw"></i> Admin
                        </a>
                        <a class="dropdown-item" href="/user_info/">
                            <i class="fa fa-users fa-fw"></i> User Info
                        </a>
                        <div class="dropdown-header text-center">
                            <strong>Settings</strong>
                        </div>
                        <a class="dropdown-item" href="/app_settings/">
                            <i class="fa fa-gears fa-fw"></i> App Settings
                        </a>
                        <div class="dropdown-header text-center">
                            <strong>User</strong>
                        </div>
                        <a class="dropdown-item" href="/edit_profile/1/">
                            <i class="fa fa-user fa-fw"></i> User Profile
                        </a>
                        <a class="dropdown-item" href="/logout/?next=/login">
                            <i class="fa fa-gear fa-fw"></i> Log out
                        </a>
                    </div>
                </li>
            </ul>
            <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse"
                data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false"
                aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
        </div>
        <div class="collapse navbar-collapse" id="navbarResponsive">
            <ul class="navbar-nav navbar-sidenav" id="menu">
                <li>...</li>
            </ul>
        </div>
    </nav>
</body>
</html>