如何在导航栏中将导航项对齐到右侧? [复制]

Posted

技术标签:

【中文标题】如何在导航栏中将导航项对齐到右侧? [复制]【英文标题】:How to align nav item to the right side in a navbar? [duplicate] 【发布时间】:2020-06-17 18:34:47 【问题描述】:

如何在特定位置对齐导航项?我希望我的品牌名称在左侧,我的登录按钮在导航栏的右端,我是新手,请帮助我解决这个问题。

.navbar-brand 
  font-weight: bold;
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<div class="container">
  <ul class="nav">
    <li class="nav-item">
      <a class="nav-link navbar-brand active" href="#">NotePro</a>
    </li>
    <li class="nav-item ">
      <a class="nav-link" href="#">LogIn</a>
    </li>
  </ul>
</div>

Result pic

【问题讨论】:

你的 html 是错误的。您正在使用&lt;ul&gt;&lt;li&gt; 定位您的品牌并登录。用 div 分隔它们 【参考方案1】:

将此代码添加到container

.container 
  display: flex;
  justify-content: space-between;

并将品牌移到ul 标签之外

.navbar-brand 
  font-weight: bold;


.container 
  display: flex;
  justify-content: space-between;
<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
  <meta charset="utf-8">
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
  <link rel="stylesheet" href="new.css">
  <title>NotePro</title>
</head>

<body>
  <div class="container">
    <a class="nav-link navbar-brand active" href="#">NotePro</a>
    <ul class="nav">
      <li class="nav-item ">
        <a class="nav-link" href="#">Home</a>
      </li>
      <li class="nav-item ">
        <a class="nav-link" href="#">about us</a>
      </li>
      <li class="nav-item ">
        <a class="nav-link" href="#">LogIn</a>
      </li>
    </ul>
  </div>
</body>

</html>

【讨论】:

【参考方案2】:

<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>


<!--here is your code below-->

<nav class="navbar navbar-expand-lg navbar-light bg-light">
        <a class="navbar-brand" href="#">Navbar</a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-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 ml-auto">
                <li class="nav-item active">
                    <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">Link</a>
                </li>
                <li class="nav-item dropdown">
                    <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                Dropdown
              </a>
                    <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                        <a class="dropdown-item" href="#">Action</a>
                        <a class="dropdown-item" href="#">Another action</a>
                        <div class="dropdown-divider"></div>
                        <a class="dropdown-item" href="#">Something else here</a>
                    </div>
                </li>
                <li class="nav-item">
                    <a class="nav-link disabled" href="#">Disabled</a>
                </li>
            </ul>
            <form class="form-inline my-2 my-lg-0">
                <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
                <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
            </form>
        </div>
    </nav>

在你的 UL 上使用 ml-auto

【讨论】:

【参考方案3】:

您可以将引导类 d-flex 和 justify-content-between 用于 ul 元素。 更多详情可查看here

.navbar-brand 
  font-weight: bold;
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<div class="container">
  <ul class="nav d-flex justify-content-between">
    <li class="nav-item">
      <a class="nav-link navbar-brand active" href="#">NotePro</a>
    </li>
    <li class="nav-item ">
      <a class="nav-link" href="#">LogIn</a>
    </li>
  </ul>
</div>

【讨论】:

以上是关于如何在导航栏中将导航项对齐到右侧? [复制]的主要内容,如果未能解决你的问题,请参考以下文章

如何在引导程序中左右对齐导航栏的列表项

iPhone:在代码中的导航栏中将“添加信息”按钮添加为右侧栏按钮项

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

我如何正确对齐我的导航栏项目(ul)

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

如何在 CSS 中将网站导航栏中的文本居中对齐?