Cannot get centered logo to reposition to the right on collapse

浪子不回头ぞ 提交于 2019-12-24 07:07:08

问题


I am having trouble getting the logo to stay in the top right side of the navbar on collapse. See images for issue.

Collapsed content

Navbar before collapse

Code for review:

    <nav class="navbar navbar-expand-md fixed-top navbar-light blue-grey lighten-5 mx-auto">
    <div class="container">

    <!-- Collapse Button -->
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#mobileNav" aria-controls="mobileNav" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
    </button>

    <!-- Collapsible Content -->
    <div class="collapse navbar-collapse justify-content-md-center font-weight-bold" id="mobileNav">

        <!-- Links Left -->
        <ul class="navbar-nav">
            <li class="nav-item">
                <a class="nav-link" href="#">Internet</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#">Building Managers</a>
            </li>
        </ul>

        <!-- Navbar Brand -->
        <a id="logo" class="navbar-brand" href="index.html">
            <img class="hidden-sm-down" src="img/logoNav.png" alt="Lone Star Fiber"> 
        </a>

        <!-- Links Right -->
        <ul class="navbar-nav">
            <li class="nav-item">
                <a class="nav-link" href="#">Business Internet</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#">Developers</a>
            </li>
        </ul>
    </div>
</nav>

回答1:


I think the approach should be similar to: Bootstrap NavBar with left, center or right aligned items

Use 2 separate nav-collapse with the same .mobile-nav target. Use the order-* classes to show the logo before the mobile menu on the right...

<nav class="navbar navbar-expand-md fixed-top navbar-light blue-grey lighten-5 mx-auto">
    <div class="container">

        <!-- Collapse Button -->
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".mobile-nav" aria-controls="mobileNav" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>

        <!-- Collapsible Content -->
        <div class="collapse navbar-collapse justify-content-md-center font-weight-bold mobile-nav order-2">

            <!-- Links Left -->
            <ul class="navbar-nav">
                <li class="nav-item">
                    <a class="nav-link" href="#">Internet</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">Building Managers</a>
                </li>
            </ul>
            </div>
            <!-- Navbar Brand -->
            <a id="logo" class="navbar-brand order-1 order-md-2 text-center" href="index.html">
                <img class="" src="//placehold.it/130x30?text=Lone Star Fiber" alt="Lone Star Fiber">
            </a>
            <div class="collapse navbar-collapse justify-content-md-center font-weight-bold mobile-nav order-2">

            <!-- Links Right -->
            <ul class="navbar-nav">
                <li class="nav-item">
                    <a class="nav-link" href="#">Business Internet</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">Developers</a>
                </li>
            </ul>
        </div>
    </div>
</nav>

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




回答2:


Use d-none d-md-block to hide image on sm screen size and d-md-none d-sm-block to show the image not inside collapse on sm size.

See fiddle:https://jsfiddle.net/khz5uv1r/25/

img{
  height:50px;
  width:50px;
}
  <nav class="navbar navbar-expand-md fixed-top navbar-light blue-grey lighten-5 mx-auto">
    <div class="container">

    <!-- Collapse Button -->
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#mobileNav" aria-controls="mobileNav" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
    </button>

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
  <!-- Collapsible Content -->

          <div class="pic d-md-none d-sm-block">
        <a id="logo"  href="index.html">
            <img src="https://material.angular.io/assets/img/examples/shiba1.jpg" alt="Lone Star Fiber"> 
        </a>
        </div>
    <nav class="collapse navbar-collapse justify-content-md-center font-weight-bold" id="mobileNav">
        <ul class="navbar-nav">
            <li class="nav-item">
                <a class="nav-link" href="#">Internet</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#">Building Managers</a>
            </li>
        <li class="nav-item pic d-none d-md-block">
        <a id="logo"  href="index.html">
            <img src="https://material.angular.io/assets/img/examples/shiba1.jpg" alt="Lone Star Fiber"> 
        </a>
        </li>
            <li class="nav-item">
                <a class="nav-link" href="#">Business Internet</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#">Developers</a>
            </li>
        </ul>
    </div>
    </div>
</nav>


来源:https://stackoverflow.com/questions/51055136/cannot-get-centered-logo-to-reposition-to-the-right-on-collapse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!