即使切换,Bootstrap 4 Navbar也会将部分保持在右侧

时间:2018-04-30 11:31:20

标签: twitter-bootstrap navigation bootstrap-4 navbar

即使展开切换,我也会尝试让“登录部分”保持在右上角。我试图浮动该部分,但它没有用。

Before Expanding

After Expanding

这是我的代码:

<nav class="navbar navbar-expand-lg bg-dark navbar-dark fixed-top">
        <div class="container">
            <div class="navbar-header">
                <button class="navbar-toggler"
                        type="button"
                        data-toggle="collapse"
                        data-target="#mymenu">
                    <span class="sr-only">toggle navigation</span>
                    <span class="navbar-toggler-icon"></span>
                </button>
                <a href="#" class="navbar-brand">App</a>

            </div>
            <div class="collapse navbar-collapse" id="mymenu"> 
                <!-- nav body -->
                <ul class="nav navbar-nav mr-auto">
                    <li class="active nav-item"><a class="nav-link" href="index.html">Home</a></li>
                    <li class="nav-item"><a class="nav-link" href="#">Item 1</a></li>
                    <li class="nav-item"><a class="nav-link" href="#">Item 2</a></li>
                    <li class="nav-item dropdown">
                        <a  class="nav-link dropdown-toggle"
                           data-toggle="dropdown" role="button"
                           href="#">Item 3
                        </a>
                        <ul class="dropdown-menu">
                            <li><a class="dropdown-item" href="#">Item 3.1</a></li>
                            <li><a class="dropdown-item" href="#">Item 3.2</a></li>
                            <li><a class="dropdown-item" href="#">Item 3.3</a></li>
                        </ul>
                    </li>
                </ul>
                <!-------------------Search---------------------->
                <form class="form-inline my-2 my-lg-0 position-relative">
                    <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
                    <button class="btn btn-outline-light my-2 my-sm-0" type="submit">Search</button>
                </form>


            </div>
            <!--------------------User---------------------->
            <div class="ml-lg-5">
                <a class="navbar-brand" href="#">Log In</a>
            </div>
        </div>
    </nav>

感谢您考虑我的要求。

1 个答案:

答案 0 :(得分:2)

您可以使用flexbox https://2heng.xin/调整“登录”div的位置。例如,将其放在标记中的navbar-collapse之前,然后使用order-lg-last order-0将其定位在较大屏幕上的最后(第3个),然后在移动导航栏切换时使用较小屏幕中的自然顺序(第2个)。

ordering classes

<nav class="navbar navbar-expand-lg bg-dark navbar-dark fixed-top">
    <div class="container">
        <div class="navbar-header">
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#mymenu">
                <span class="sr-only">toggle navigation</span>
                <span class="navbar-toggler-icon"></span>
            </button>
            <a href="#" class="navbar-brand">App</a>
        </div>
        <div class="ml-lg-5 order-lg-last order-0">
            <a class="navbar-brand" href="#">Log In</a>
        </div>
        <div class="collapse navbar-collapse" id="mymenu"> 
            <ul class="nav navbar-nav mr-auto">
                <li class="active nav-item"><a class="nav-link" href="index.html">Home</a></li>
                <li class="nav-item"><a class="nav-link" href="#">Item 1</a></li>
                <li class="nav-item"><a class="nav-link" href="#">Item 2</a></li>
                <li class="nav-item dropdown">
                    <a class="nav-link dropdown-toggle" data-toggle="dropdown" role="button" href="#">Item 3
                    </a>
                    <ul class="dropdown-menu">
                        <li><a class="dropdown-item" href="#">Item 3.1</a></li>
                        <li><a class="dropdown-item" href="#">Item 3.2</a></li>
                        <li><a class="dropdown-item" href="#">Item 3.3</a></li>
                    </ul>
                </li>
            </ul>
            <form class="form-inline my-2 my-lg-0 position-relative">
                <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
                <button class="btn btn-outline-light my-2 my-sm-0" type="submit">Search</button>
            </form>
        </div>
    </div>
</nav>