Bootstrap version 5 has a feature where the dropdown menu extends beyond the right side of the screen

I'm facing an issue with the default bootstrap 5 navbar. When I attempt to add a dropdown on the right side, the list of dropdown items extends beyond the screen on the right. You can see the problem here.

I've tried the solutions suggested in similar cases, such as:

  • Bootstrap 4: Dropdown menu is going off to the right of the screen
  • Bootstrap: Position of dropdown menu relative to Navbar item

(I have attempted using classes like dropdown-menu-right or pull-right without success...)

Here is the code snippet that's causing the trouble:

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

And here is the navbar section of my code:

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

<!-- Troublesome code section -->
<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>
<!-- ########################## -->

Thank you.

Answer №1

Ensure that you are referring to the correct documentation for your current version: https://getbootstrap.com/docs/5.0/components/dropdowns/#menu-alignment
In version 5, you will find that .dropdown-menu-end is used for aligning the menu to the right.

<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a3c1ccccd7d0d7d1c2d3e3968d938d938ec1c6d7c291">[email protected]</a>/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>

    <!-- The following section is experiencing issues -->
    <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/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="284a47475c5b5c5a4958681d06180618054a4d5c491a">[email protected]</a>/dist/js/bootstrap.bundle.min.js" integrity="sha384-b5kHyXgcpbZJO/tY9Ul7kGkf1S0CWuKcCD38l8YkeH8z8QjE0GmW1gYU5S9FOnJ0" crossorigin="anonymous"></script>

Answer №2

Include the following CSS class in your style.css:

.dropdown-menu-right {
   right: 0 !important;
   left: auto !important;
}

Then, implement the dropdown-menu-right class within your div element.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Challenges with iFrame Resizing on Internet Explorer

After following a method highlighted in a forum post to set up an iframe on my webpage, I encountered some issues. To display just a section of the page within the iframe, I utilized the zoom/scale feature available in different browsers. To target a spec ...

Aligning a series of images with individual captions in the center

I am currently working on centering a row made up of four images along with their respective captions underneath. The code I am using is as follows: <div class="clear" style="text-align: center;"> <div style="float: left; padding-right: 10px;"& ...

Retrieve the red, green, and blue components of a color in the RGB format

When I retrieve "rgb(18, 115, 224)" from a DOM element, I need to convert this color to hexadecimal in order to assign it to a span element. To get the hexadecimal equivalent of the color, I can use the following code snippet: "#" + componentToHex(r) + co ...

The autocomplete attribute is not compatible with GroceryCrud functionality

I attempted to implement the autocomplete feature using an example from w3schools. https://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_form_autocomplete Although I used jQuery to modify the form and add autocomplete="on" to both the form and input ...

Discover the magic of DevTools: Easily view real-time SCSS updates without the hassle of constantly saving your changes

The question at hand fails to provide a complete picture of the situation. While I am familiar with Workspaces and SASS source-maps, I can already track the line in the scss file where the specific rule is located that I wish to modify, thanks to the Style ...

What could possibly be causing the 500 server error in PHP?

It's frustrating - every time a user tries to enter their email and password, an error 500 message pops up on the server! But strangely enough, upon refreshing the page, everything seems to work just fine... So why does the server throw an error on th ...

Troubleshooting Full Screen Problems with Three.js

Despite my efforts to troubleshoot, I am still encountering a frustrating issue with the Three.js API. Even after thoroughly studying the API documentation, browsing through StackOverflow questions, and using debugging tools like firebug and chrome's ...

Tips for adjusting the width of items within the Box element in Material UI React

I am utilizing React Material UI along with the Box component to style my form. In this scenario, I have 4 items in each row except for the last row where I need to display only 3 items with the last item filling the entire row by merging two elements toge ...

What is the best method for retrieving text from elements designated by class?

I am currently using BeautifulSoup to scrape data from a website, but I am facing an issue with extracting only the text I need. The specific part of the website data that I want to grab is: <div content-43 class="item-name">This is the te ...

Can Django implement pagination without altering the URL structure?

I discovered the fundamentals of pagination in Django at this resource: Nevertheless, I am faced with a challenge - I wish to implement pagination without modifying the URL. Specifically, I need to paginate a single table among multiple tables on an HTML ...

Is there a way to create a responsive navbar using flexbox that automatically transitions into two rows at smaller screen sizes?

I've designed a sleek navbar located at the bottom of my webpage featuring a central logo leading to the home page, with two links on each side directing users to specific pages. Using @media CSS, I made it responsive by reducing its size as the scree ...

Establishing a color palette for various CSS classes using a gradient color spectrum

I am looking to create a gradient color scale and define classes for it based on a range of values. My scale ranges from 0 to 8.5, with increments of 0.25. This means I have a total of 34 different colors (8.5/0.25 = 34) to cover the entire spectrum. Each ...

Show a compact list of items neatly arranged in only two rows

For my Blazor WASM project, I am looking to showcase a list of items in two rows. Imagine the list contains 15 items and I want each row to display 5 items. Users should be able to navigate through the other 5 items using next/previous buttons. https://i. ...

Utilizing CSS to transform text with a sleek, animated writing effect

I have utilized CSS to create a captivating animation effect where text is written out in a smooth style. I am seeking assistance in replacing the current text with a different one while maintaining the same effect. HTML & CSS .typed-out{ overflow: ...

What is causing this JavaScript alert to malfunction?

My current project involves working with ASP.NET and in the view, I have a code snippet that is causing some issues. When I attempt to use Sweet Alert outside of the function, it works perfectly fine. Similarly, if I switch out Sweet Alert for a regular al ...

Which is the better option for opening a link in a button: using onclick or href?

What is the most effective way to open a link using a button? <button type="button" onclick="location='permalink.php'">Permalink</button> <button type="button" href="index.php">Permalink</button> ...

Eliminate the lower boundary of a divisional table

I'm having an issue with displaying images in a div table. Each image has a border at the bottom, which is around 2-5 pixels wide. Unfortunately, as a new user, I am unable to send any images. My main goal is to remove this unwanted border from the ...

CSS for mobile devices not loading until the device is rotated

My website, redkrypt.com, is functioning perfectly on my laptop. However, I am facing an issue where the css file will only load on my iPhone 4S after I rotate it once. Once I do this, the website works both vertically and horizontally. I have tried variou ...

Achieve the appearance of table-like alignment without the need for traditional tables

Any ideas on how to achieve the following layout: <table border="0"> <tbody> <tr> <td style="margin-right:5px;"> <h2 id="optiona" class="option optiona optiona2">a. aa </h2> </td> ...

How can you update the background image of a particular div using the 'onclick' feature in React.JS?

Thank you for helping me out here. I'm currently working with ReactJS and facing a challenge where I need to change the background of a div from a color to a specific image URL upon clicking a button in a modal. Despite my efforts, I keep encountering ...