Dropdown menu with submenu positioned beneath navigation element in full width

I am looking to customize the Bootstrap Navbar submenu to display below each navigation item by default, while also scaling the background of the submenu to fill the width of the browser window.

Despite trying various solutions, I have been unsuccessful in achieving this effect. Currently, my attempt involves using :before to set a background, but even that has not worked. Is there a viable solution for this issue?

You can review my code progress here: https://codepen.io/cray_code/pen/QXNVLK

In the provided code, you will notice that there is a full-width dropdown solution available. However, my goal is to have the submenu items positioned below their parent menu item, rather than at the left side.

The following CSS is specifically for achieving the full-width version, with no alterations made to the HTML structure of the navbar:

.navbar-full .dropdown-menu {
    position: absolute !important;
    left: 0 !important;
    right: 0 !important;
}

.navbar-full .dropdown, .navbar-full .navbar-nav .nav-item.dropdown {
    position: static !important;
}

Answer №1

  1. To make your navbar responsive, you can utilize media queries. For example,
    @media only screen and (min-width: 768px)
    can be used to achieve this.
  2. An alternative approach is to streamline your code by incorporating Bootstrap classes. Assign w-100 to the dropdown-menu element, and include the position-static class in its parent dropdown element.
  3. Update: If you are unable to manually control the menu items, a solution involving jQuery can be implemented.
$('a.nav-link').hover(function () {
  if ($(window).width() >= 768) {
    var myId = $(this).attr("id");
    if ($("#" + myId).length > 0) {
      var pos = Math.round($('#' + myId).position().left) + 12;
      $("a.dropdown-item").css('paddingLeft', pos + 'px');
    }
  }
})

$('a.nav-link').hover(function() {
  if ($(window).width() >= 768) {
    var myId = $(this).attr("id");
    if ($("#" + myId).length > 0) {
      var pos = Math.round($('#' + myId).position().left) + 12;
      $("a.dropdown-item").css('paddingLeft', pos + 'px');
    }
  }
})
li.dropdown:hover>.dropdown-menu {
  display: block;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css" integrity="sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf" crossorigin="anonymous">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
</head>

<body>
  <nav class="navbar navbar-full navbar-expand-lg navbar-light bg-light">
    <a class="navbar-brand" href="#">Navbar</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-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 mr-auto">
        <li class="nav-item active">
          <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">Link</a>
        </li>
        <li class="nav-item dropdown position-static" id="parent">
          <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
            Dropdown 1
          </a>
          <div class="dropdown-menu w-100" aria-labelledby="navbarDropdown">
            <a class="dropdown-item mx-auto" href="#">Action</a>
            <a class="dropdown-item" href="#">Another action</a>
            <a class="dropdown-item" href="#">Another action</a>
            <a class="dropdown-item" href="#">Another action</a>
            <div class="dropdown-divider"></div>
            <a class="dropdown-item" href="#">Something else here</a>
          </div>
        </li>
        <li class="nav-item dropdown position-static" id="parent1">
          <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown1" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
              Dropdown 2
            </a>
          <div class="dropdown-menu w-100" aria-labelledby="navbarDropdown1">
            <a class="dropdown-item mx-auto" href="#">Hello</a>
            <a class="dropdown-item" href="#">New</a>
            <div class="dropdown-divider"></div>
            <a class="dropdown-item" href="#">Something else here</a>
          </div>
        </li>
        <li class="nav-item">
          <a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
        </li>
      </ul>
      <form class="form-inline my-2 my-lg-0">
        <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
        <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
      </form>
    </div>
  </nav>
</body>

</html>

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

Is it better to store CSS and JavaScript in separate files or within the main HTML document

While the best practice is to place JavaScript and CSS code in separate files (such as .js and .css), many popular websites like Amazon, Facebook, etc. often include a large portion of their JavaScript and CSS code directly within the main HTML page. Whic ...

How can I make the divs resize automatically and have them equal in height when the window is resized?

I need to adjust the height of three column divs so that they are all equal. Initially, I was able to achieve this using a function found on SO (apologies for not being able to provide attribution at this time, will update if possible): function resizeIt( ...

Highlighting a Table Column with a Radio Button

I am struggling with controlling the highlight of a table using only radio buttons. When I try to change the selector to input:radio, it doesn't work as expected. It seems to only work with the selector provided in my code snippet. $("th").on("clic ...

Tips on eliminating certain text from a hyperlink

I need assistance with removing the text  Title from my link while preserving the image. <tr id="group0"> <td colspan="100" nowrap="" class="ms-gb"> <a href="javascript:" onclick="javascript:ExpCollGroup('28-1_', ...

The height of the inner div is not extending all the way down to 100% size

I can't seem to get the green div's height to cover the red div by setting it to 100%. Other solutions I've tried like setting parent div heights to 100% haven't worked either. Can someone spot what's wrong with my code? Appreciate ...

Utilize various styles from within a specified font family in CSS

My website is currently using the font-family Roboto with two different font types (400 and 500i). <link href="https://fonts.googleapis.com/css?family=Roboto:400,500i" rel="stylesheet"> Even though I have both fonts available, I am struggling to im ...

Angular.js' Element.prop('scrollHeight') consistently gives identical values for various list items

I have been attempting to determine the heights and widths of the items in my list. Despite exploring various methods for obtaining the height and width of an item, none of them seemed to provide the scrollHeight directly from the element. Upon inspecting ...

The functionality of $j is not available

I am dealing with a simple jQuery event. Within the HTML document, I have the following code: <div class="opsContainer"></div> To utilize jQuery functionality, the following script is added: <script src="https://cdnjs.cloudflare.com/ajax ...

Using javascript to quickly change the background to a transparent color

I am in the process of designing a header for my website and I would like to make the background transparent automatically when the page loads using JavaScript. So far, I have attempted several methods but none seem to be working as desired. The CSS styles ...

JavaScript and jQuery validation issues persisting

Here is the HTML code I am using: <script src="js/validate.js"></script> <label>FIRST NAME:</label> <td> <input type="text" class="firstname" id="firstname" onKeyUp="firstname()" /> </td> <td> <label id=" ...

Prevent button activation when a different button has already been clicked using jQuery

I'm currently working on a project that involves a form allowing users to upload images, videos, and YouTube videos. However, I want to restrict users from uploading photos, videos, and YouTube videos simultaneously. I am struggling with disabling the ...

Use jQuery to add and remove classes while opening and closing div elements individually

I am working on a page that contains hidden fullscreen divs. My goal is to be able to open these divs individually by clicking on the corresponding button and close them by clicking the "close" button inside the div. Currently, I am able to open a div suc ...

Difficulty enforcing mandatory checkboxes

Seeking assistance on making checkboxes required. I am creating a form using bootstrap 4 and validating it using the JS example from bootstrap documentation. I have added "novalidate" to the form, "required" to inputs, and included the following script: ...

What is the best way to create a board game layout inspired by Monopoly using HTML and CSS?

My goal is to create a square layout with smaller squares outlining it, similar to the tiles on a monopoly board. CSS isn't my forte, but I'm hoping to get the basic layout set up first and then play around to make it look nice. ...

"Hovering over one div does not trigger the display of another div

I seem to have forgotten my CSS skills, but I am facing an issue. Here is the code snippet: <div class="footer_container"> <div class="website_logo_to_footerexpand"></div> <div class="info_cont"> <div class ...

Is it possible to achieve the full image when utilizing double buffering for drawing on canvas?

I have a code where I am using two canvas elements to draw an image in order to prevent flickering. However, I am facing an issue where I cannot get the full image to display when using this method. When I use just one canvas, the image displays fine. It ...

Presenting MongoDB data in a Jinja2 HTML table

Currently, I am trying to find a way to display multiple documents one by one from my MongoDB collection. The code I have written to fetch the data is as follows: @app.route('/shops', methods=['GET', 'POST']) @login_required d ...

When viewing my project on GitHub pages, the images within the card are not appearing

After running my project page link at https://nayaksofia.github.io/RestaurantReviewTest1/, I've noticed that the images inside the card are not displaying. However, when I run the same project on my local server localhost:8000, the images appear just ...

What causes the fading effect on my navigation bar when the slideshow is in use?

I recently created a navigation bar at the top of my webpage, placed above a picture slideshow. However, I noticed that as the slideshow transitions between images, the background color of the navigation bar fades to transparent. How can I prevent this and ...

When you print, transfer HTML tags to a separate page

I need help with writing a report that pulls data from a dynamic database. I added code to force a page break after every second div tag, but since the content is constantly changing, some div tags end up spanning across pages when printed. Is there a way ...