You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Bootstrap navbar-right类无法将导航项右对齐问题求助

Fixing Right-Aligned Nav Items in Bootstrap 4 Navbar

Hey there! The problem you're hitting is that navbar-right is a Bootstrap 3-specific class, but your code is using Bootstrap 4 (you can spot this from classes like navbar-expand-md and the modern navbar structure). Bootstrap 4 switched to flexbox for layout, so that old class no longer works.

The Solution: Use ml-auto Instead

Replace the navbar-right class with ml-auto on your second <ul> element. The ml-auto utility class adds margin-left: auto to the element, which flexbox uses to push the entire list to the far right of the navbar container.

Modified Code

<!-- Navbar -->
<nav class="navbar navbar-expand-md navbar-dark fixed-top bg-dark">
  <a class="navbar-brand" href="/index.php"><img src="/assets/img/logo_white.png" style="height: 35px;"></a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <div class="collapse navbar-collapse" id="navbarNavDropdown">
    <ul class="nav navbar-nav">
      <li class="nav-item <?php if ($sect == 'home') { echo 'active';} ?>">
        <a class="nav-link" href="/index.php">Home</a>
      </li>
      <li class="nav-item dropdown <?php if ($sect == 'about') { echo 'active';} ?>">
        <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
          About
        </a>
        <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
          <a class="dropdown-item <?php if ($page == 'about-us') { echo 'active';} ?>" href="/about/about-us.php">About Us</a>
          <a class="dropdown-item <?php if ($page == 'staff') { echo 'active';} ?>" href="/about/staff.php">Staff Team</a>
          <a class="dropdown-item <?php if ($page == 'partners') { echo 'active';} ?>" href="/about/partners.php">Partners</a>
        </div>
      </li>
      <li class="nav-item dropdown <?php if ($sect == 'resources') { echo 'active';} ?>">
        <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
          Resources
        </a>
        <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
          <a class="dropdown-item <?php if ($page == 'downloads') { echo 'active';} ?>" href="/resources/downloads.php">Downloads</a>
          <a class="dropdown-item <?php if ($page == 'charts') { echo 'active';} ?>" href="/resources/charts.php">Charts</a>
          <a class="dropdown-item <?php if ($page == 'metar') { echo 'active';} ?>" href="/resources/metar.php">METAR Decoder</a>
        </div>
      </li>
      <li class="nav-item <?php if ($sect == 'academy') { echo 'active';} ?>">
        <a class="nav-link" href="/academy/index.php">Academy</a>
      </li>
      <li class="nav-item <?php if ($sect == 'fly') { echo 'active';} ?>">
        <a class="nav-link disabled" href="#">Fly</a>
      </li>
      <li class="nav-item <?php if ($sect == 'forums') { echo 'active';} ?>">
        <a class="nav-link" href="https://forums.multicrew.co.uk/">Forums</a>
      </li>
      <li class="nav-item <?php if ($sect == 'book') { echo 'active';} ?>">
        <a class="nav-link" href="https://multicrew.setmore.com/">Book</a>
      </li>
    </ul>
    <!-- Replaced navbar-right with ml-auto here -->
    <ul class="nav navbar-nav ml-auto">
      <li class="nav-item">
        <a class="nav-link" href="https://portal.multicrew.co.uk/"><i class="fa fa-sign-in"></i> Login</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="https://portal.multicrew.co.uk/register"><i class="fa fa-user"></i> Register</a>
      </li>
    </ul>
  </div>
</nav>
<!--/END Navbar-->

Why This Works

Bootstrap 4's navbar uses flexbox for layout. The ml-auto class tells the flex container to allocate all available space to the left of this <ul>, which pushes it all the way to the right. This is the official recommended way to align nav items to the right in Bootstrap 4+, replacing the deprecated navbar-right class from Bootstrap 3.

内容的提问来源于stack exchange,提问作者Calum Shepherd

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:44:31