css 如何固定导航项位置和下拉位置?

6yoyoihd  于 2022-12-24  发布在  其他
关注(0)|答案(1)|浏览(291)

所以我一直在使用 Bootstrap 下拉菜单,但由于某种原因,它没有正确对齐。我尝试使用多种解决方案,如更新我的 Bootstrap 版本,但它们根本不起作用。
下面是我的代码:

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/js/bootstrap.bundle.min.js"></script>
  <link rel="preconnect" href="https://fonts.googleapis.com"><link rel="preconnect" href="https://fonts.gstatic.com" crossorigin><link href="https://fonts.googleapis.com/css2?family=Montserrat&display=swap" rel="stylesheet">
    <link href='https://css.gg/add.css' rel='stylesheet'>
    <link rel="stylesheet" href="/static/css/dashboard.css">
</head>
<!DOCTYPE html>
<html lang="en">
<head>
  <link rel="shortcut icon" type="image/x-icon" href="/static/img/AdiAvi.png" />
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
  <script src="/static/js/dashboard.js"></script>
  <link rel="preconnect" href="https://fonts.googleapis.com">
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  <link href="https://fonts.googleapis.com/css2?family=Montserrat&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="/static/css/dashboard.css">
    <link rel="stylesheet" href="/static/css/base.css">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Avyukt&#39;s Dashboard</title>
    
    <style>
      .trigger {
        display: none !important;
      }
      #loginButton {
        display: none !important;
      }
    </style>
    
</head>
<body>
  <nav class="navbar navbar-expand-lg navbar-dark" style="background-color: rgb(0, 0, 0);">
    <a class="navbar-brand" href="/">
      <img class="ms-3" src="/static/img/AdiAvi.png" width="30" height="30" class="d-inline-block align-top" alt="">
      AdiAvi
    </a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarNav">
      <ul class="navbar-nav ms-auto mb-2 mb-lg-0 align-items-start align-items-lg-center">
        <li class="nav-item">
          <a   class="nav-link active"  class="nav-link"  href="/">Home</a>
        </li>
        <li class="nav-item">
          <a  class="nav-link" href="/contact">Contact</a>
        </li>
        
        
        
        <li class="nav-item dropdown">
          <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" role="button" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
            <img src="/static/profilePictures/A.png" width="50" height="50" class="rounded-circle mt-8">
          </a>
          <div class="dropdown-menu dropdown-menu-end" aria-labelledby="navbarDropdownMenuLink">
            <a class="dropdown-item" href="/dashboard">Dashboard</a>
            <a class="dropdown-item" href="/account">Edit Profile</a>
            <a class="dropdown-item" href="/logout">Log Out</a>
          </div>
        </li>
           
      </ul>
    </div>
  </nav>

有人知道如何解决这个问题吗?我正在使用bootstrap 5,我想我正在使用最新版本的bundle。
截图:https://ibb.co/pQPVdx2
就像我甚至不能点击下拉菜单。

mi7gmzs6

mi7gmzs61#

您需要更新代码以使用正确版本的bootstrap.js和popper.js(或使用bootstrap.bundle.js)。然后,您需要修改所有特定于引导的data-*属性名称,以使用BS 5+的新格式:data-bs-* .
通过将.dropdown-menu-end添加到.dropdown-menu,可以解决下拉菜单对齐的问题。
.nav-item的垂直对齐方式可以通过向父级.navbar-nav添加.align-items-*类来控制。请注意,这也会影响移动的折叠导航栏上的水平对齐方式。使用与导航栏断点(.navbar-expand-lg)匹配的响应变量(.align-items-lg-center)来控制展开状态下的对齐方式。
以下是.nav-item的最小示例,折叠的导航栏左对齐,展开的导航栏垂直居中:

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">

<nav class="navbar navbar-expand-lg navbar-dark bg-black">
  <a class="navbar-brand" href="/">
    <img class="ms-3" src="https://via.placeholder.com/30" width="30" height="30" class="d-inline-block align-top" alt=""> AdiAvi
  </a>
  <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>
  <div class="collapse navbar-collapse" id="navbarNav">
    <ul class="navbar-nav ms-auto mb-2 mb-lg-0 align-items-start align-items-lg-center">
      <li class="nav-item">
        <a class="nav-link active" class="nav-link" href="/">Home</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="/contact">Contact</a>
      </li>

      <li class="nav-item dropdown">
        <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" role="button" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
          <img src="https://via.placeholder.com/50" width="50" height="50" class="rounded-circle mt-8">
        </a>
        <div class="dropdown-menu dropdown-menu-end" aria-labelledby="navbarDropdownMenuLink">
          <a class="dropdown-item" href="/dashboard">Dashboard</a>
          <a class="dropdown-item" href="/account">Edit Profile</a>
          <a class="dropdown-item" href="/logout">Log Out</a>
        </div>
      </li>

    </ul>
  </div>
</nav>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/js/bootstrap.bundle.min.js"></script>

备注:

与上面的代码段和文档一样,bootstrap.js应该包含在结尾处,就在结束body标记之前。
包括Bootstrap的CSS和JS。将<link>标记放在<head>中用于CSS,将<script>标记用于JavaScript包(包括用于定位下拉菜单、弹出窗口和工具提示的Popper)放在</body>结束标记之前。
--网址:http:getbootstrap.com/docs/5.2/getting-started/introduction/#quick-start

相关问题