Re-positioning items on navbar collapse in Bootstr

2019-08-08 16:05发布

问题:

I am working on a navbar for a webpage. I was able to make a navbar with data aligned left and right, and Brand in center. However, when the navbar collapses, the alignment of the items becomes Left (over) Brand (over) Right. I want the Brand to stay up at top and other items to collapse.

Here is my code:

<html>

<head>
  <script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
  <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</head>

<body>
  <header class="sticky-top">
    <nav class="navbar navbar-expand-lg navbar-toggleable-md navbar-dark bg-primary">
        <div class="navbar-collapse collapse w-100 order-1 order-md-0 dual-collapse2">
            <ul class="navbar-nav mr-auto">
                <li class="nav-item home">
                    <a class="nav-link" href="/">Home</a>
                </li>
                <li class="nav-item about">
                    <a class="nav-link" href="/about">About</a>
                </li>
            </ul>
        </div>
        <div class="mx-auto order-0">
            <a class="navbar-brand mx-auto" href="/">Brand</a>
        </div>
        <div class="navbar-collapse collapse w-100 order-3 dual-collapse2">
            <ul class="navbar-nav ml-auto">
                <li class="nav-item">
                    <a class="nav-link" href="#">Login</a>
                </li>
            </ul>
        </div>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".dual-collapse2">
            <span class="navbar-toggler-icon"></span>
        </button>
    </nav>
</header>
</body>

</html>

The collapse functionality works fine everywhere except when the page is inspected using chrome dev tools. In chrome dev tools, the above-mentioned order is displayed (left->brand->right). Is this a bug in chrome, or I need to do some additional configuration?

回答1:

Use the order classes responsively like this...

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

<header class="sticky-top">
    <nav class="navbar navbar-expand-lg navbar-dark bg-primary">
        <div class="navbar-collapse collapse w-100 order-1 dual-collapse2">
            <ul class="navbar-nav mr-auto">
                <li class="nav-item home">
                    <a class="nav-link" href="/">Home</a>
                </li>
                <li class="nav-item about">
                    <a class="nav-link" href="/about">About</a>
                </li>
            </ul>
        </div>
        <div class="mx-auto order-0 order-lg-2">
            <a class="navbar-brand mx-auto" href="/">Brand</a>
        </div>
        <div class="navbar-collapse collapse w-100 order-3 dual-collapse2">
            <ul class="navbar-nav ml-auto">
                <li class="nav-item">
                    <a class="nav-link" href="#">Login</a>
                </li>
            </ul>
        </div>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".dual-collapse2">
            <span class="navbar-toggler-icon"></span>
        </button>
    </nav>
</header>