两个项目无法在 boostrap 的导航栏中位于右侧

问题描述 投票:0回答:1

enter image description here

我试图将登录和注册项对齐到导航栏的右侧,但它不起作用。
有人可以告诉我这是为什么吗?

 <!DOCTYPE html>
 <html lang="en">
 <head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
    <style>
        
    </style>
 </head>
 <body>
  <nav class="navbar navbar-expand-lg navbar-light bg-light mb-3">
    <div class="container-fluid">
      <a class="navbar-brand" href="#">StayPedia</a>
      <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>
      <div class="collapse navbar-collapse" id="navbarNavAltMarkup">
        <div class="navbar-nav">
          <a class="nav-link active" aria-current="page" href="/">Home</a>
          <a class="nav-link" href="/hotels">Hotels</a>
          <a class="nav-link" href="/hotels/new">New Hotel</a>
          
          
          <ul class="navbar-nav ml-auto" >
            <% if (!currentUser){ %>  
              <a class="nav-link " href="/login">Login</a>
              <a class="nav-link " href="/register">Register</a>
            <% } else { %>
              <a class="nav-link " href="/wishlist">My Wishlist</a>
              <a class="nav-link " href="/logout">Logout</a>
            <% } %>
            </ul>

        </div>
      </div>
    </div>
  </nav> 
 </body>
 </html>
 

我试图将登录和注册项对齐到导航栏的右侧,但它不起作用。
有人可以告诉我这是为什么吗?

css-selectors navbar
1个回答
0
投票

您已将

<ul class="navbar-nav ml-auto" >
嵌套在 navbar-nav 类中 关闭导航栏导航,然后添加
<ul class="navbar-nav ml-auto" >

    <nav class="navbar navbar-expand-lg navbar-light bg-light mb-3">
  <div class="container-fluid">
    <a class="navbar-brand" href="#">StayPedia</a>
    <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarNavAltMarkup">
      <div class="navbar-nav">
        <a class="nav-link active" aria-current="page" href="/">Home</a>
        <a class="nav-link" href="/hotels">Hotels</a>
        <a class="nav-link" href="/hotels/new">New Hotel</a>
      </div>
      <div class="navbar-nav ms-auto">
        <% if (!currentUser){ %>  
          <a class="nav-link" href="/login">Login</a>
          <a class="nav-link" href="/register">Register</a>
        <% } else { %>
          <a class="nav-link" href="/wishlist">My Wishlist</a>
          <a class="nav-link" href="/logout">Logout</a>
        <% } %>
      </div>
    </div>
  </div>
</nav>
© www.soinside.com 2019 - 2024. All rights reserved.