为啥导航栏不会在 Bootstrap 5 中与右侧对齐?

Posted

技术标签:

【中文标题】为啥导航栏不会在 Bootstrap 5 中与右侧对齐?【英文标题】:Why won't Navbar align to the right side in Bootstrap 5?为什么导航栏不会在 Bootstrap 5 中与右侧对齐? 【发布时间】:2022-01-16 00:42:21 【问题描述】:

我希望将“帐户”导航栏定位在最远处 右侧,但 ms-auto 似乎没有按我的意愿工作:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
    <title>Music Store!</title>
  </head>
    <body>
        <div class="container-xl">
            <nav class="navbar navbar-expand-lg navbar-light bg-light">
              <div class="container-fluid">
                <a class="navbar-brand" href="#">Website</a>
                <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
                  <span class="navbar-toggler-icon"></span>
                </button>
                <div class="collapse navbar-collapse" id="navbarSupportedContent">
                  <ul class="navbar-nav me-auto mb-2 mb-lg-0">
                    <li class="nav-item">
                      <a class="nav-link active" aria-current="page" href="#">Home</a>
                    </li>
                    <li class="nav-item">
                      <a class="nav-link" href="about.php">About</a>
                    </li>
                    <li class="nav-item">
                      <a class="nav-link" href="contact.php">Contact</a>
                    </li>
                    
                    <?php
                    session_start();
                    if(isset($_SESSION['loggedIn']))
                        
                        ?>
                          <li class="nav-item">
                            <a class="nav-link" href="viewStock.php">Shop</a>
                          </li>    

                          <ul class="navbar-nav ms-auto">
                                <li class="nav-item dropdown">
                                    <a class="nav-link dropdown-toggle" role="button" data-bs-toggle="dropdown" aria-expanded="false">Account</a>
                                    <ul class="dropdown-menu dropdown-menu-end" aria-labelledby="navbarDropdown">
                                        <li><a class="dropdown-item" href="viewOrder.php">Order History</a></li>
                                        <li><a class="dropdown-item" href="viewListing.php">My Listings</a></li>
                                        <li><a class="dropdown-item" href="logout.php">Logout</a></li>
                                    </ul>
                                </li>
                            </ul>

                        <?php
                        
                        else
                        ?>
                          <ul class="navbar-nav ms-auto">
           

                 <li class="nav-item dropdown">
                                    <a class="nav-link dropdown-toggle" role="button" data-bs-toggle="dropdown" aria-expanded="false">Account</a>
                                    <ul class="dropdown-menu dropdown-menu-end" aria-labelledby="navbarDropdown">
                                        <li><a class="dropdown-item" href="login.php">Login</a></li>
                                        <li><a class="dropdown-item" href="createUser.php">Register</a></li>
                                    </ul>
                                </li>
                            </ul>
                        <?php
                        
                        ?>    
                    </ul>
                </div>
              </div>
            </nav>
        </div> <!-- End of container -->
        <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
    </body>
</html>

【问题讨论】:

【参考方案1】:

将此添加到您的 css 中

.navbar-collapse
  justify-content: end;

并从&lt;ul class="navbar-nav me-auto mb-2 mb-lg-0"&gt; 中删除me-auto

这应该可以解决问题

这是小提琴https://jsfiddle.net/8zn9jkfs/

【讨论】:

谢谢,但我想要左边的 Home、About、Contact all 和最右边的 Account。

以上是关于为啥导航栏不会在 Bootstrap 5 中与右侧对齐?的主要内容,如果未能解决你的问题,请参考以下文章

Bootstrap 3 Navbar 左侧和右侧导航栏项目的左侧折叠菜单

Bootstrap 导航栏下拉菜单项右侧

Bootstrap 导航栏下拉菜单项右侧

Bootstrap 4将两个导航栏菜单按钮对齐到右侧[重复]

Bootstrap 4导航栏,带有品牌中心和左侧,中心和右侧的链接[重复]

Bootstrap 4 - 使用切换按钮将链接移动到导航栏的右侧[重复]