Bootstrap 4 - 折叠外的导航栏项目

问题描述 投票:0回答:1

好吧,我不得不承认,即使在 BS3 中,我也一直在努力正确地工作。但我希望在折叠容器外部保持持久的导航栏链接。

这就是我现在拥有的:

<nav class="navbar fixed-top navbar-expand-lg navbar-template">
    <a class="navbar-brand" href="#">Navbar</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavDropdown">
        <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarNavDropdown">
        <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="#">Features</a></li>
            <li class="nav-item"><a class="nav-link" href="#">Pricing</a></li>
            <li class="nav-item dropdown">
                <a class="nav-link dropdown-toggle" href="http://example.com" id="navbarDropdownMenuLink" data-toggle="dropdown">Dropdown link</a>
                <div class="dropdown-menu dropdown-menu-right">
                    <a class="dropdown-item" href="#">Action</a>
                    <a class="dropdown-item" href="#">Another action</a>
                    <a class="dropdown-item" href="#">Something else here</a>
                </div>
            </li>
        </ul>
    </div>
    <ul class="navbar-nav">
        <li class="nav-item"><a class="nav-link" href="#"><span class="fa fa-facebook"></span></a></li>
        <li class="nav-item"><a class="nav-link" href="#"><span class="fa fa-twitter"></span></a></li>
        <li class="nav-item"><a class="nav-link" href="#"><span class="fa fa-youtube"></span></a></li>
        <li class="nav-item"><a class="nav-link" href="#"><span class="fa fa-linkedin"></span></a></li>
    </ul>
</nav>

这将一直有效,直到第一个导航处于折叠状态。当折叠处于活动状态时,它看起来像这样:

有人找到一个干净的解决方案吗?

css twitter-bootstrap bootstrap-4 navbar
1个回答
23
投票

最简单的方法是使用 flexbox 实用程序类,因此不需要额外的 CSS。将您始终想要显示的项目保留在

navbar-collapse
div 之外。

https://codeply.com/go/TWZGiy3VGw

<nav class="navbar fixed-top navbar-light navbar-expand-lg navbar-template">
    <a class="navbar-brand" href="#">Navbar</a>
    <div class="d-flex flex-row order-2 order-lg-3">
        <ul class="navbar-nav flex-row">
            <li class="nav-item"><a class="nav-link px-2" href="#"><span class="fa fa-facebook"></span></a></li>
            <li class="nav-item"><a class="nav-link px-2" href="#"><span class="fa fa-twitter"></span></a></li>
            <li class="nav-item"><a class="nav-link px-2" href="#"><span class="fa fa-youtube"></span></a></li>
            <li class="nav-item"><a class="nav-link px-2" href="#"><span class="fa fa-linkedin"></span></a></li>
        </ul>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavDropdown">
            <span class="navbar-toggler-icon"></span>
        </button>
    </div>
    <div class="collapse navbar-collapse order-3 order-lg-2" id="navbarNavDropdown">
        <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="#">Features</a></li>
            <li class="nav-item"><a class="nav-link" href="#">Pricing</a></li>
            <li class="nav-item dropdown">
                <a class="nav-link dropdown-toggle" href="http://example.com" id="navbarDropdownMenuLink" data-toggle="dropdown">Dropdown link</a>
                <div class="dropdown-menu dropdown-menu-right">
                    <a class="dropdown-item" href="#">Action</a>
                    <a class="dropdown-item" href="#">Another action</a>
                    <a class="dropdown-item" href="#">Something else here</a>
                </div>
            </li>
        </ul>
    </div>
</nav>

请注意,使用响应式

order-*
类可确保折叠菜单项在折叠/移动断点时保持在最后 (
order-3
)。

© www.soinside.com 2019 - 2024. All rights reserved.