When the bootstrap 5 dropdown is activated, flex wrap functionality triggers vertical scrolling

Every time I click on the dropdown, it causes a vertical scroll instead of popping out like a regular dropdown. I want to maintain the horizontal scroll while fixing this issue.

Any suggestions on how to solve this problem?

Demo: https://jsfiddle.net/d3jy68q7/

HTML:

<nav class="navbar navbar-expand navbar-dark bg-dark">
    <div class="container-fluid">
      <div class="nav-scroller bg-body" id="navbarsExample03">
        <ul class="nav navbar-nav me-auto mb-2 mb-sm-0">
          <li class="nav-item">
            <a class="nav-link active" aria-current="page" href="#">Home</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#">Link</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" aria-disabled="true">Link 2</a>
          </li>
          <li class="nav-item dropdown">
            <a class="nav-link dropdown-toggle" href="#" data-bs-toggle="dropdown" aria-expanded="false">Dropdown</a>
            <ul class="dropdown-menu">
              <li><a class="dropdown-item" href="#">Action</a></li>
              <li><a class="dropdown-item" href="#">Another action</a></li>
              <li><a class="dropdown-item" href="#">Something else here</a></li>
            </ul>
          </li>
          <li class="nav-item">
            <a class="nav-link" aria-disabled="true">Link 3</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" aria-disabled="true">Link 4</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" aria-disabled="true">Link 5</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" aria-disabled="true"&
            gt;Link 6</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" aria-disabled="true"&
            >Link7</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" aria-disabled="true"&
            >Link8</a>
          </li>
        </ul>
      </div>
    </div>
  </nav>

CSS:

.nav-scroller {
  position: relative;
  z-index: 2;
  height: 2.75rem;
  overflow-y: hidden;
  background-color:#212529 !important;
}
.nav-scroller .nav {
  display:flex;
  flex-wrap:nowrap;
  padding-bottom:1rem;
  margin-top:-1px;
  overflow-x:auto;
  text-align:center;
  white-space:nowrap;
  -webkit-overflow-scrolling:touch;
}
.navbar nav.nav-link,.navbar-nav.nav-link.active,.navbar-nav.nav-link.show{
  color:#fff;
}

Answer ā„–1

The reason for the scrollbar appearing is due to the overflow of the dropdown's parent container when it opens, as its overflow-y property is not set to hidden. This causes the dropdown menu to be hidden behind the parent's parent container that has overflow set to hidden.

To ensure the dropdown menu shows properly, you can move it out of containers with hidden overflow.

This can be achieved using JavaScript by getting the position of the dropdown container and assigning it to the dropdown menu. Add the height of the dropdown to the top position to place it at the bottom. Additionally, use a fixed position so it can pop out of the overflow-hidden containers. You can also add a scroll listener to adjust the menu accordingly.

Here's an example:

const myDropdown = document.querySelector('.dropdown');

function adjustDropdownMenu(event) {

  requestAnimationFrame(() => {

    const dropdownContPos = myDropdown.getBoundingClientRect();

    document.querySelector(".dropdown-menu").style.cssText = `
        position: fixed;
        left: ${dropdownContPos.left}px;
        top: ${dropdownContPos.height + dropdownContPos.top}px;
    `;
  });
}

myDropdown.addEventListener('show.bs.dropdown', adjustDropdownMenu);
document.querySelector('.navbar-nav').addEventListener('scroll', adjustDropdownMenu);
<nav class="navbar navbar-expand navbar-dark bg-dark">
  <div class="container-fluid">
    <div class="nav-scroller bg-body" id="navbarsExample03">
      <ul class="nav navbar-nav me-auto mb-2 mb-sm-0">
        <li class="nav-item">
          <a class="nav-link active" aria-current="page" href="#">Home</a>
        </li>
        ... (additional nav items)
      </ul>
    </div>
  </div>
</nav>

For Multiple Dropdowns:

If you have multiple dropdowns, loop through each one to assign event listeners. Pass the dropdown element, get its position, and assign it to the dropdown menu. On scroll, find the currently opened dropdown, get its parent element position, and update the dropdown menu accordingly.

const dropdowns = document.querySelectorAll('.nav-scroller .dropdown');

function position(dropdownMenu, containerPos) {
  dropdownMenu.style.cssText = `
            position: fixed;
            left: ${containerPos.left}px;
            top: ${containerPos.height + containerPos.top}px;
        `;
}

... (adjustOnScroll and adjustDropdownMenu functions)

dropdowns.forEach(dropdown => {
  dropdown.addEventListener('show.bs.dropdown', adjustDropdownMenu(dropdown));
});

document.querySelector('.navbar-nav').addEventListener('scroll', adjustOnScroll);
<nav class="navbar navbar-expand navbar-dark bg-dark">
  <div class="container-fluid">
    <div class="nav-scroller bg-body" id="navbarsExample03">
      <ul class="nav navbar-nav me-auto mb-2 mb-sm-0">
        <li class="nav-item">
          <a class="nav-link active" aria-current="page" href="#">Home</a>
        </li>
        ... (additional nav items)
      </ul>
    </div>
  </div>
</nav>

Answer ā„–2

There is no horizontal scrolling in the scenario you provided.

To eliminate the scrolling effect, you can remove the overflow property from both the .nav-scroller and .nav-scroller .nav selectors :

.nav-scroller {
  position: relative;
  z-index: 2;
  height: 2.75rem;
  /* overflow-y: hidden; */
  background-color: #212529 !important;
}

.nav-scroller .nav {
  display: flex;
  flex-wrap: nowrap;
  padding-bottom: 1rem;
  margin-top: -1px;
  /* overflow-x: auto; */
  text-align: center;
  white-space: nowrap;
  -webkit-overflow-scrolling: touch;
}
.navbar-nav .nav-link, .navbar-nav .nav-link.active, .navbar-nav .nav-link.show {
  color: #fff;
}

By replicating the changes mentioned above, your navbar should no longer have the scrolling behavior.

If you wish to view a demonstration of this solution, click on this working demo link

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

I require displaying the initial three letters of the term "basketball" and then adding dots

Just starting out with CSS and struggling with the flex property. Seems to work fine at larger sizes, but when I reduce the screen size to 320px, I run into issues... Can anyone help me display only the first three letters of "basketball ...

Is there a way to automatically scroll to the bottom of a div when it first

Looking to enhance my application with a chat feature that automatically scrolls to the bottom of the chat page to display the latest messages. Utilizing VueJs: <template> <div id="app"> <div class="comments" ...

Display a single image on each slide when viewing Media Slider Carousel BS3 on a mobile device

Currently, I am implementing the Media Slider Carousel BS3 from bootsnipp on a webpage to create a compact slider. However, my goal is to customize it for mobile screens so that only one image displays per slide instead of three. For instance: https://i. ...

Issue with scrollable multilevel dropdown menu

Dealing with an extensive menu list, I aimed to restrict them using a scroll. In the demo on CodePen, everything works fine without the scroll. However, upon adding overflow-y:scroll; height:200px;, it leads to an overflow-x issue and hides all the dropd ...

Data displayed in a table-like format

Is there a way to achieve this layout view image here (currently done using tables) while maintaining semantic markup? I believe the best tag for this would be dl. Each cell should have the height of its corresponding row. EDIT: The left column contains d ...

Designing a visually appealing widget in a jQuery UI theme by floating two elements neatly inside

My code looks like this: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Testing PasteHTML.co ...

What is the best way to insert an HTML data attribute string into a SCSS mixin using attr()?

I'm currently working on setting up a color scheme in SCSS that allows me to use the following HTML structure: <div class="swatch" data-bg="green">...</div> In my SCSS file, I have defined a mixin like this: @function color($key: ' ...

Issue with Bootstrap dropdown menu not showing up/functioning

I've spent the entire day trying to find a solution and experimenting with various methods, but I'm still unable to get it to work. The navbar-toggle (hamburger menu) shows up when the window is resized, but clicking on the button doesn't di ...

Utilize JavaScript to apply the CSS -moz-transition

Creating a web application and using CSS3 to transform a div, but encountering a challenge with Firefox. Able to make Chrome, Opera, and IE work properly, except for Firefox. Here's how I'm setting up the working browsers: obj.style.WebkitTrans ...

Changing the class when a checkbox is selected using JQuery

Iā€™m working on a bootstrap switcher and I want to change the panel color from grey to green when the checkbox (switch) is checked. I had it working before, but after updating the switcher, it no longer functions properly. Here is the main code for the s ...

Toggle sideBar within an iFrame by implementing Media Queries

I have implemented media queries in my code to hide the .sideBar when the screen resolution is less than 768px, showing only the .main section. @media (max-width: 768px) { .sideBar { display: none !important; } } Now, I want to add a ha ...

Could Chrome be miscalculating em unit values in media queries?

I have defined my media query breakpoints as shown below: @media screen and (max-width:48em){ /* phone */ } @media screen and (min-width:48.063em){ /* tablet */ } After using a PX to EM calculator to get the 48.063em value (as I was advised to us ...

Is there a way to initiate a jquery function upon loading the page, while ensuring its continued user interaction?

On my webpage, there is a JavaScript function using jQuery that I want to automatically start when the page loads. At the same time, I want to ensure that users can still interact with this function. This particular function involves selecting a link tha ...

Maintain the horizontal alignment of CSS floats

What causes the div on the right to float higher than the two divs on the left? How can I ensure they are all aligned at the top? HTML <header> <div class="nav" style="width:100%;border:#900 thin solid;"> <ul id='nav-left' ...

The 'slide.bs.carousel' event in Bootstrap carousel is triggered just once

Take a look at my JavaScript code: $('#carousel-container').bind("slide.bs.carousel", function () { //reset the slideImg $('.slideImg',this).css('min-height', ''); //set the height of the slider var ...

Struggling to align the Bootstrap list-group container in the middle

Good day to you! I'm currently working on a sidebar layout using Bootstrap's list-group and I need to set a specific width to ensure it aligns properly with the other elements above and below it. However, when I try setting the width to 300px, t ...

Unwanted gap appears in the image slider

There seems to be a pesky little gap right below my image slider that I just can't seem to get rid of. I was hoping to spruce it up with a box-shadow, but when I tried it out on jsfiddle, it ended up looking pretty awful because of the annoying gap. I ...

The column on the right does not extend all the way to the bottom of the page

Hello, I currently have a website with the following design: I'm looking to extend the right column all the way down to the bottom of the body, even if there's not enough content to push it down. How can I accomplish this using CSS? Here is the ...

In every browser except for Safari on iPad, a white X is displayed in the grey box. I'm wondering if the error lies with me or with Safari

When using Safari on my iPad, I noticed that the white X appears on the right-hand side of the black screen. Since this is the only version of Safari that I have, I am unsure if this issue is specific to iPads, Safaris, or just my device. Visit this link ...

Tips for creating a web page with rounded screen corners, similar to those on an Android smartphone

I am looking to create rounded screen corners on a website similar to those found on an Android smartphone. Here is an example of what I am trying to achieve: I have searched for tutorials online but haven't had much luck finding the right solution. ...