Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
344 views
in Technique[技术] by (71.8m points)

navigation - Bootstrap 4 Navbar keep section on right even when toggled

I try to make the "login section" stay on the right corner even when the toggle is expanded. I have try to float the section but it did not work.

Before Expanding

After Expanding

Here is my code:

<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>

Thanks for considering my request.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use the flexbox ordering classes to adjust the position of the "Log In" div. For example place it before the navbar-collapse in the markup, and then use order-lg-last order-0 to position it last (3rd) on larger screens, then natural order (2nd) in smaller screens when the mobile navbar is toggled.

https://www.codeply.com/go/7Mz5XkMckc

<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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...