Responsive dropdown menu in Bootstrap 5 navbar

Update: I successfully fixed the desktop version. Now, I just need to work on making it responsive for mobile.

The problem I am encountering is that the dropdown menu doesn't function properly on both mobile and desktop. Although the styling looks good for both interfaces, the functionality is lacking. I tried adding placeholder links to the dropdown items to test if they would display, but unfortunately, they did not.

Here is the code snippet for the menu in question:

<nav id="navbar"class="navbar navbar-expand-lg">
  …
</nav>
  • I can see the classes shifting between collapsed and collapsing but I'm unable to see any of the dropdown items displayed.

Does anyone have insight into why the menu items are not appearing? Could this be due to conflicting pre-existing CSS rules? (I've checked this to some extent already as my initial suspicion, but disabling obvious ones didn't help show the items.) Should I delve deeper into my custom CSS?

Additionally, I'm currently struggling to make the sticky navigation work again. I had it working for a simpler Bootstrap menu before so I'll figure it out. However, I'm open to any helpful advice or suggestions from the community.

Please find below my current compiled CSS:

/* Compiled CSS styles go here */
…

Answer №1

When in collapsed mode on mobile devices, the functionality of the hamburger icon relies on the necessary JavaScript bundle being included. For example, you can include https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js. Additionally, be cautious of any loose a tags (anchors) in your markup as they may have been unintentionally copied from examples on Bootstrap's website.

Review the following functional code:

<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3d5f5252494e494f5c4d7d08130c130e">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="54363b3b20272026352414617a657a67">[email protected]</a>/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
<nav id="navbar" class="navbar navbar-light navbar-expand-lg">
  <div class="container-fluid">
    <a class="navbar-brand" href="#"><img class="navbar-brand" src="[[++url_images]]/logos/logo.jpg" alt="Your image is not loading"></a>
    <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarScroll" aria-controls="navbarScroll" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon text-dark"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarScroll">
      <ul class="navbar-nav me-auto my-2 my-lg-0 navbar-nav-scroll" >
        <li class="nav-item">
          <a class="nav-link active home" aria-current="page" href="https://www.google.com">Home</a>
        </li>
        <li class="nav-item">
          <a class="nav-link about" href="https://www.google.com">About us</a>
        </li>
        <li class="nav-item">
          <a class="nav-link contact" href="https://www.google.com">Contact</a>
        </li>
        <li class="nav-item dropdown">
          <a class="nav-link dropdown-toggle" href="#" id="navbarScrollingDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
            Link
          </a>
          <ul class="dropdown-menu" aria-labelledby="navbarScrollingDropdown">
            <li><a class="dropdown-item" href="https://www.google.com">Action</a></li>
            <li><a class="dropdown-item" href="https://www.google.com">Another action</a></li>
            <li>
              <hr class="dropdown-divider">
            </li>
            <li><a class="dropdown-item" href="https://www.google.com">Something else here</a></li>
          </ul>
        </li>
      </ul>
      <form class="d-flex" role="search">
        <input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
        <button class="btn btn-outline-success" type="submit">Search</button>
      </form>
    </div>
  </div>
</nav>

Answer №2

After my recent update on desktop functionality, I successfully managed to make it work on mobile as well.

There are still some styling issues to address, particularly with the menu placement across different devices. Currently, my quick fix involved using a fixed position.

However, the main issue of it not working initially has been resolved by implementing the following code:

  <div class="container-fluid">
    <img class="navbar-brand" src="[[++url_images]]/logos/logo.jpg" alt="Your image is not loading"></a>
    <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarScroll" aria-controls="navbarScroll" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarScroll">
      <ul class="navbar-nav navbar-nav-scroll">
        <li class="nav-item">
          <a class="nav-link active home" aria-current="page" href="https://www.google.com">Home</a>
        </li>
        <li class="nav-item">
          <a class="nav-link about" href="https://www.google.com">About us</a>
        </li>
        <li class="nav-item">
          <a class="nav-link contact" href="https://www.google.com">Contact</a>
    </div>
  </div>
</nav>

Compiled CSS (current version excluding remaining style issues):

To see the visual representation, check out the DesktopMenu and Mobile Menu images.

Big thanks to everyone who offered assistance during this process.

Answer №3

A custom Bootstrap 5 menu with multilevel dropdowns and flexible configurations for left or right expansion. No additional CSS is needed as this menu is "naked."

Tested in Bootstrap 5.2.2, this menu includes Font Awesome icons for a hamburger toggle.

To see the demo, screenshots, and access the source code (including the necessary CSS), visit:

https://github.com/lasseedsvik/Bootstrap-5-Mega-menu

If you prefer not to use Font Awesome, the hamburger icon can easily be replaced with something else in the menu design.

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

Setting up a global CSS and SASS stylesheet for webpack, TypeScript, Phaser, and Angular: A step-by-step guide

A manual configuration has been set up to accommodate all the technologies mentioned in the title (webpack, typescript, phaser, and angular). While it works perfectly for angular component stylesheets, there seems to be an issue with including a global st ...

Change "Only" regular text into clickable links without altering any current hyperlinks

Looking to transform plain links into hyperlinks, while leaving existing hyperlinks untouched. A string of text is classified as a link if: It starts at the beginning of a line or after a space ((^|\s)) It is not part of an HTML hyperlink or any ot ...

Challenges with resizing images in SVG using D3

I have an SVG image created using d3. I am facing an issue where the svg is extending beyond its parent div container. Here is the code snippet: <div id="test" style="{width: 500px; height:500px;}"> <svg></svg> </div> ...

Error in Javascript: Null Object

I have created an upload page with a PHP script for AJAX, but I'm encountering errors in Firebug. Also, the upload percentage is not being returned properly. Currently, I can only do one upload in Firefox and the script doesn't work in Chrome. H ...

There seems to be an issue with the border displaying correctly within the div element

I am currently working on developing a tag input system, but I am encountering some CSS issues. What I am aiming for is to have a green border that surrounds and contains the 2 divs inside it, however, this is not happening as expected. You can see the fi ...

Highlighting table rows when hovering in IE with JQuery - compatibility across all versions

I have a table on my jsp page with multiple rows and columns. I want to ensure that visitors can easily follow along when they interact with the table - by highlighting the row or column they hover over with a different background color. Although the **hov ...

Trouble with image size transition not functioning properly

When attempting to enlarge an image with a transition in Chrome, it doesn't seem to be working properly. Here is the HTML code: <img src="foobar.png"> And the CSS code: img { float: left; margin-right ...

Move a sub-division horizontally within a parent element that has a full width of 100%

I am currently incorporating some jQuery features into my website. The concept involves expanding the parent div to 100% width for responsive design as you scroll down the page, which works perfectly. Simultaneously, I aim to translateX the child div so th ...

Using JavaScript and HTML, create a click event that triggers a drop-down text

Can anyone help me with creating a dropdown feature using JavaScript, HTML, and CSS? I want to be able to click on the name of a project and have information about that project show up. Any suggestions on how I can achieve this? Thanks in advance! ...

Choosing a single radio button value within a nested ng-repeat loop in AngularJS

Help needed with selecting only one radio button value in nested ng-repeat. Please review the source code and suggest any potential mistakes. <form ng-submit="save()"> <div ng-repeat="x in surveyLst"> <div class="row"> < ...

Learning how to implement the selection tag to upload data to a SQL database using PHP

As a coding beginner, I've been tackling a project involving data insertion into an SQL server with PHP. My current struggle lies in the process of inserting data and capturing select tag objects as variables. I keep encountering Undefined Index error ...

Creating a dynamic overlapping image effect with CSS and JavaScript

My fullwidth div has an image that overlaps it. When the browser is resized, more of the image is either shown or hidden without resizing the actual image itself. I managed to get this effect for the width, but how can I achieve the same result for the hei ...

single-use date availability checker

I'm currently facing an issue with my code. It seems to be functioning correctly, but only for the first iteration. Let me explain further. If I select a date and time that is already in the database, it correctly displays "date unavailable". Likewis ...

The CSS outline has a soft, rounded appearance rather than a sharp

https://i.sstatic.net/Mkb8Z.png Is there a way to prevent my outline from having rounded corners as it expands during the animation? Note: The issue seems to be present on Linux and MS Edge, but works fine on Mozilla Firefox. Any solutions for MS Edge us ...

Material UI drop down select position placement

Having an issue with the dropdown position when using select from material UI. It's not appearing in the correct location. Desired Dropdown Position: https://i.sstatic.net/arRIF.png Current Dropdown Position: when opening the dropdown https://i. ...

What Could Be Causing My Webfonts to Be Inaccessible on Windows Devices and iPhones?

I recently created a simple holding page on www.tomrankinmusic.com The Webfonts I embedded in the page display correctly in the latest versions of Firefox, Safari, and Chrome on Mac OSX Lion 10.7.4 but do not show up in Chrome and IE6 on Windows XP Pro, d ...

Selected Angular Radio Button

Back in the good ole days of basic HTML and CSS, I was able to achieve the following: input:checked+label { background-color: #f00; } <div class="col-xs-6"> <input type="radio" id="template-1" name="template" value="template1" checked> ...

Navigate to the top of the page using jQuery

Attempting to implement a simple "scroll jump to target" functionality. I've managed to set it up for all parts except the "scroll to top". The jumping works based on the tag's id, so it navigates well everywhere else, but not to the very top. He ...

The issue of exceeding margins

I am facing an issue with some CSS. Below is my HTML code: <body> <div id="cn"> <div id="hd"> <ul> <some li's> </ul> </div><!-- /#hd --> <div id="bd"> ...

I'm seeking guidance on how to delete a specific ul li element by its id after making an ajax request to delete a corresponding row in MySQL database. I'm unsure about the correct placement for the jQuery

I have created an app that displays a list of income items. Each item is contained within an li element with a unique id, along with the item description and a small trash icon. Currently, I have implemented code that triggers an AJAX call when the user cl ...