代码之家  ›  专栏  ›  技术社区  ›  Nilbo Swaggins

如何使用Bootstrap-4将可折叠按钮菜单显示在屏幕右侧而不是左侧

  •  2
  • Nilbo Swaggins  · 技术社区  · 6 年前

    嗨,我是网络开发新手,还在学习。我的导航栏在 flexbox collapsable 屏幕左侧导航栏上打开的按钮,到目前为止,我还没有尝试任何东西使其移动。

        <nav class="navbar sticky-top navbar-expand-md navbar-dark bg-dark">   
      <a class "navbar-brand justify-content-start" href="#">
          <img src="https://pbs.twimg.com/profile_images/91427004/brule_400x400.jpg" alt="Logo" style="width: 60px;">
        </a>
        <div class="d-flex w-80 ml-auto">
          <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#collapsingNavbar">
                <span class="navbar-toggler-icon"></span>
            </button>     
        </div>
        <div class="navbar-collapse collapse" id="collapsingNavbar">
          <ul class="navbar-nav navbar-right ml-auto">
    
            <li class="nav-item active expandMenu">
              <a class="nav-link" href="#">Portfolio</a>
            </li>
            <li class="nav-item active expandMenu">
              <a class="nav-link" href="#">Portfolio</a>
            </li>
            <li class="nav-item active expandMenu">
              <a class="nav-link" href="#">Contact</a>
            </li>
          </ul>
        </div>
      </nav>
    

    以下代码是CSS:

    nav {
      width: 100%;
      height: 70px;
      font-size: 175%;
    }
    
    .expandMenu {
    
      background: rgb(52, 58, 64);
      width: 50%;
      margin-top: -5px;
      text-align: center;
    
    }
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Dhaval Jardosh    6 年前

    为类指定宽度 navbar-right 至50%并移除 width 50%来自 .expandMenu

    nav {
      width: 100%;
      height: 70px;
      font-size: 175%;
    }
    
    .expandMenu {
      background: rgb(52, 58, 64);
      margin-top: -5px;
      margin-left 100%;
      text-align: center;
    }
    
    .navbar-right {
      width: 50%;
    }
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" 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>
    
    <nav class="navbar sticky-top navbar-expand-md navbar-dark bg-dark">
      <a class "navbar-brand justify-content-start" href="#">
        <img src="https://pbs.twimg.com/profile_images/91427004/brule_400x400.jpg" alt="Logo" style="width: 60px;">
      </a>
      <div class="d-flex w-80 ml-auto">
        <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#collapsingNavbar">
                <span class="navbar-toggler-icon"></span>
            </button>
      </div>
      <div class="navbar-collapse collapse" id="collapsingNavbar">
        <ul class="navbar-nav navbar-right ml-auto">
    
          <li class="nav-item active expandMenu">
            <a class="nav-link" href="#">Portfolio</a>
          </li>
          <li class="nav-item active expandMenu">
            <a class="nav-link" href="#">Portfolio</a>
          </li>
          <li class="nav-item active expandMenu">
            <a class="nav-link" href="#">Contact</a>
          </li>
        </ul>
      </div>
    </nav>