Bootstrap 5:下拉菜单在屏幕右侧消失

Posted

技术标签:

【中文标题】Bootstrap 5:下拉菜单在屏幕右侧消失【英文标题】:Bootstrap 5: Dropdown menu go out off to the right of the screen 【发布时间】:2021-05-18 17:46:40 【问题描述】:

我有一个默认的 bootstrap 5 导航栏,但是当我尝试在右侧放置一个下拉菜单时,下拉项目列表会从右侧的屏幕中消失。这个https://ibb.co/jf3GHfK。

我已经尝试过提供给其他人的解决方案,例如这里: Bootstrap 4: Dropdown menu is going off to the right of the screen

这里: Bootstrap: Position of dropdown menu relative to navbar item

(我尝试过下拉菜单右或拉右,但没有任何工作对我有用....)

这是不好的地方:

<ul class="navbar-nav ml-auto">
  <li class="nav-item dropdown">
    <a
      class="nav-link dropdown-toggle"
      href="#"
      id="navbarDropdown"
      role="button"
      data-bs-toggle="dropdown"
      aria-expanded="false"
    >
      Username
    </a>

    <div
      class="dropdown-menu dropdown-menu-right"
      aria-labelledby="navbarDropdown"
    >
      <a class="dropdown-item dropdown" href="#"> Edit settings </a>
      <a class="dropdown-item" href="#"> Edit profile page </a>
      <a class="dropdown-item" href="#"> Show profile page </a>
      <a class="dropdown-item" href="#"> Create profile page </a>
    </div>
  </li>
</ul>

这是我的导航栏代码:

<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<div class="container-fluid">
<a class="navbar-brand" href="#">Blog</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 dropdown">
      <a
        class="nav-link dropdown-toggle"
        href="#"
        id="navbarDropdown"
        role="button"
        data-bs-toggle="dropdown"
        aria-expanded="false"
      >
        Categories
      </a>

      <ul class="dropdown-menu" aria-labelledby="navbarDropdown">
        <li>
          <a class="dropdown-item" href="#">Cat1</a>
        </li>
        <li>
          <a class="dropdown-item" href="#">Cat2</a>
        </li>
        <li>
          <a class="dropdown-item" href="#">Cat3</a>
        </li>
      </ul>
    </li>

    <li class="nav-item">
      <a class="nav-link" href="#">Add post</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Add category</a>
    </li>

    <li class="nav-item">
      <a class="nav-link" href="#">Logout</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Sign in</a>
    </li>
  </ul>
</div>

<!-- This is what is not working well -->
<ul class="navbar-nav ml-auto">
  <li class="nav-item dropdown">
    <a
      class="nav-link dropdown-toggle"
      href="#"
      id="navbarDropdown"
      role="button"
      data-bs-toggle="dropdown"
      aria-expanded="false"
    >
       user.first_name   user.last_name 
    </a>

    <div
      class="dropdown-menu dropdown-menu-right"
      aria-labelledby="navbarDropdown"
    >
      <a class="dropdown-item dropdown" href="#"> Edit settings </a>
      <a class="dropdown-item" href="#"> Edit profile page </a>
      <a class="dropdown-item" href="#"> Show profile page </a>
      <a class="dropdown-item" href="#"> Create profile page </a>
    </div>
  </li>
</ul>
<!-- ################################ -->

问候。

【问题讨论】:

【参考方案1】:

确保您正在阅读您所使用版本的正确文档:https://getbootstrap.com/docs/5.0/components/dropdowns/#menu-alignment 您会看到 v5 使用 .dropdown-menu-end 来作为右对齐菜单。

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BmbxuPwQa2lc/FVzBcNJ7UAyJxM6wuqIj61tLrc4wSX0szH/Ev+nYRRuWlolflfl" crossorigin="anonymous">

<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
  <div class="container-fluid">
    <a class="navbar-brand" href="#">Blog</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 dropdown">
          <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
        Categories
      </a>

          <ul class="dropdown-menu" aria-labelledby="navbarDropdown">
            <li>
              <a class="dropdown-item" href="#">Cat1</a>
            </li>
            <li>
              <a class="dropdown-item" href="#">Cat2</a>
            </li>
            <li>
              <a class="dropdown-item" href="#">Cat3</a>
            </li>
          </ul>
        </li>

        <li class="nav-item">
          <a class="nav-link" href="#">Add post</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">Add category</a>
        </li>

        <li class="nav-item">
          <a class="nav-link" href="#">Logout</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">Sign in</a>
        </li>
      </ul>
    </div>

    <!-- This is what is not working well -->
    <ul class="navbar-nav ml-auto">
      <li class="nav-item">
        <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
      John Doe
    </a>

        <div class="dropdown-menu dropdown-menu-end" aria-labelledby="navbarDropdown">
          <a class="dropdown-item dropdown" href="#"> Edit settings </a>
          <a class="dropdown-item" href="#"> Edit profile page </a>
          <a class="dropdown-item" href="#"> Show profile page </a>
          <a class="dropdown-item" href="#"> Create profile page </a>
        </div>
      </li>
    </ul>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta2/dist/js/bootstrap.bundle.min.js" integrity="sha384-b5kHyXgcpbZJO/tY9Ul7kGkf1S0CWuKcCD38l8YkeH8z8QjE0GmW1gYU5S9FOnJ0" crossorigin="anonymous"></script>

【讨论】:

以上是关于Bootstrap 5:下拉菜单在屏幕右侧消失的主要内容,如果未能解决你的问题,请参考以下文章

Bootstrap 导航栏下拉菜单项右侧

Bootstrap 导航栏下拉菜单项右侧

Bootstrap 4 Navbar:右对齐下拉菜单在右侧打开:溢出[重复]

bootstrap怎么让下拉菜单从右侧弹出呢,或是

Twitter bootstrap 下拉菜单出现在屏幕之外

如何在小于 600 像素的屏幕上“打开”页面加载时的 Bootstrap 下拉菜单