The dropdown menu is experiencing issues with displaying sub-menus correctly, as they are appearing within the main menu instead of below it

Description: I am currently working on designing a navbar using the Bootstrap 4 framework. The issue I am facing is that when trying to implement a custom dropdown menu, it appears within the main menu bar instead of outside it. As a result, the height of the main menu bar increases. How can I display a sub-menu outside the main menu bar?

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-css lang-css prettyprint-override"><code>    *{
        box-sizing: border-box;
        overflow: hidden;
        margin: 0;
        padding: 0;
    }

    ul{
        list-style: none;
        width: auto;
        height: 100%;
    }

    ul li{
        margin: 0;
        /* border: 1px solid turquoise; */
        width: 9em;
        padding: 5px 0;
        text-align: center;
        position: relative;
    }

    /* Showing border bottom animation for the main-menu */
    ul li::after{
        content: '';
        position: absolute;
        border-bottom: 3px solid #f3007f;
        transform: scale(0, 1);
        transition: transform .5s ease;
        width: 100%;
        left: 0;
    }

    ul li:hover::after{
        transform: scale(1, 1);
    }

    /* Showing background: black and font color:white when hovering over the main-menu */
    ul li a {
        display: block;
        color: white;
    }

    ul li a:hover{
        background-color: black;
        color: white;
    }


    .logo {
        width: 4vw;
    }

    /* Styling the main menu */
    .navbarbg {
        background: #10044a;
        color: white;
        font-weight: bold;
    }

    /* Centering the main-menu elements */
    .listpos{
        justify-content: center;   
    }

    /* This code is for hiding sub-menus */
    ul li ul li {
        display: none;
    }

    /* This code is for showing sub-menus when hovering over main menu */
    ul li:hover ul li {
        display: block;
        color: white;
    }
    <!DOCTYPE html>
    <html lang="en">

    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="style.css">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
            integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
        <script src="https://kit.fontawesome.com/f3e31b33a9.js" crossorigin="anonymous"></script>
        <title>Document</title>
    </head>

    <body>
        <div class="container-fluid">
            <div class="row">
                <div class="col-md-12">
                    <nav class="navbar navbar-expand-lg navbarbg">
                        <div class="col-md-2">
                            <img src="./images/ICC-Mens-T20-World-Cup-2021.jpg" class="img-fluid logo" alt="">
                        </div>
                        <div class="collapse navbar-collapse col-md-8 listpos">
                            <ul class="navbar-nav">
                                <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="#">Matches</a></li>
                                <li class="nav-item"><a class="nav-link" href="#">Standings</a></li>

                                <!-- This is the dropdown code Starting -->
                                <li class="nav-item">
                                    <a class="nav-link">Dropdown</a>
                                    <ul class=""  aria-labelledby="navbarDropdown">
                                      <li><a class="" href="#">Action</a></li>
                                      <li><a class="" href="#">Another action</a></li>
                                    </ul>
                                </li>
                                <!-- This is the dropdown code Ending -->

                                <li class="nav-item"><a class="nav-link" href="#">News</a></li>
                                <li class="nav-item"><a class="nav-link" href="#">Videos</a></li>
                            </ul>
                        </div>

                        <div class="col-md-2 text-right">
                            <b>Search</b>
                            <span class="ml-1">
                                <i class="fa-solid fa-magnifying-glass"></i>
                            </span>
                        </div>
                    </nav>
                </div>
            </div>

        </div>
    </body>

    </html>

Answer №1

Located at the very top of your code, the dropdown section is about to begin...

An essential dropdown class is not included in the li element. It should be written as

<li class="nav-item dropdown">
.

Furthermore, each link within the dropdown should also have a class added. All links should include class="dropdown-item"

Hopefully, these adjustments will resolve any issues, but it's possible that others may notice additional improvements.

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

Error: webpack is failing to load the style and CSS loaders

I'm currently experimenting with the FullCalendar plugin from fullcalendar.io. They recommended using Webpack as a build system, which is new to me. I managed to set up the calendar functionality after some research, but I'm facing issues with th ...

Styling divs to be proportionally larger or smaller within a layout

Is there a way to design a layout that automatically scales without relying on the height property? Currently, I'm using a float layout between two divs, as depicted in the image below. The issue arises when the content within these boxes varies in si ...

JQuery HTML not functioning properly with dynamically inserted elements

Initially, I perform this action var columns = row.getElementsByTagName('td'); columns[2].innerHTML ='<Button class="btn btn-success" style="margin-right:4px;" onclick="savecategory(this.parentNode,this)"> ...

Incorporate my personalized CSS file into the Bootstrap 4 development tools

Is it possible to customize Bootstrap 4 variables with build tools as outlined in this guide? I am wondering how I can create a separate CSS file named MyCustomStyles.css at the end of the build process by running npm build dist. I have created a _MyCusto ...

Is there a way to initiate keyframe animations on a div but only after a high resolution image within the div has finished loading?

I am facing an issue with the keyframe animation fadeIn on my page. Currently, it runs immediately after the page is loaded, but I want it to run only after the high resolution image inside the .image div has finished loading. How can I achieve this? Is t ...

Efficiently utilizing CSS classes

My dilemma involves styling a textfield in two different locations on my website, each with its own parent element. The second location requires an additional margin-top that the first does not. What is a clever way to adjust the original margin-top withou ...

W3C does not provide validation for the Viewport

Why is W3C not validating my viewport meta tag? <title>DIDIx13</title> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" /> <!-- I'm getting a warning "Consider ...

Differences in line spacing can be observed between the INPUT and LABEL elements

What could be causing this unusual behavior when setting line-height to match font-size: While the height of label is 16px, the height of input is 18px... Why is that? If I adjust line-height: 18px or higher, the heights suddenly align. But why does this ...

What's the simplest method for adjusting the default font in Reactstrap?

Currently working on a MERN project and I want to customize the font used by Reactstrap for my React frontend. While I've come across solutions for altering Bootstrap 4's default fonts, I'm unsure how to implement them in Reactstrap. Any gui ...

A guide on activating Effect in Div using just css

I have tried all the questions and answers related to this topic. Additionally, I attempted to solve related questions but was not successful. Please read my question thoroughly. What I Want: When I click on the click me Div, the second hiddendiv Div i ...

Can you create a seamless transition from a horizontal gradient to a vertical gradient in order to conceal something?

I am currently working on achieving a unique effect with three background gradients, including a horizontal mask, top border, and bottom border to seamlessly blend borders into the background: In order to achieve this effect, here is the code snippet I ha ...

Generating unique identifiers and names for duplicated input fields in a form, which change dynamically

Innovative approach: When dealing with multiple replicated inputs, changing the id and name attributes can be a hassle. How can we ensure that each input remains unique, especially in a real project with several inputs like component_date and component_own ...

Show a row of pictures with overflow capabilities

I am looking to develop my own image viewer that surpasses the capabilities of Windows. My goal is to design an HTML page featuring all my images laid out horizontally to maximize screen space. I also want the images to adjust in size and alignment as I z ...

Access specific data from a jQuery event

How can I retrieve the value of a custom attribute called itemID in order to include it in the URL just like I do with id? Here is the JavaScript code: <script type="text/javascript"> $(document).ready(function(){ $('.eventImage').cl ...

Retrieving Form Validation Error Value in AngularJS

Incorporating AngularJS 1.2 RC2 and utilizing Bootstrap for CSS, I have constructed a straightforward form as shown below: <form class="form-horizontal" name="form" novalidate> <label class="col-lg-2 control-label" for="name">Name</labe ...

Add a React component to the information window of Google Maps

I have successfully integrated multiple markers on a Google Map. Now, I am looking to add specific content for each marker. While coding everything in strings works fine, I encountered an issue when trying to load React elements inside those strings. For ...

Trouble arises with the jQuery keydown function

Currently, I am encountering an issue with a straightforward jQuery code implementation. In addition to the problem description, I have included the HTML structure below, The current functionality works as expected in setting focus on the input field upo ...

Having trouble with jQuery div height expansion not functioning properly?

Having a bit of trouble with my jQuery. Trying to make a div element expand in height but can't seem to get it right. Here's the script I'm using: <script> $('.button').click(function(){ $('#footer_container').anim ...

Extracting HTML element from PHP string and placing it in a different page

In a PHP string, there is an HTML structure including a table within it: <html> <head> <!-- not much going on here --> </head> <body> <table border="0" align="center" cellpadding="0" cellspacing="10 ...

Customizing padding and margin in material-UI styles

Having issues with excessive padding and margin inside my tab component, even after trying to override it. I've followed some suggestions on StackOverflow but none seem to work. Here's how it looks: https://i.stack.imgur.com/Bgi3u.png Here is th ...