When the position of the Bootstrap 4 mega menu drop down is set to fixed, it fails to remain within its container

In Bootstrap 4, I've created a "mega menu" within the navbar that uses the fixed-top class to always stay on screen. The menu has a child div with the class container for centering its contents.

To create the mega menu, I've used the following CSS classes:

.megamenu-li {
    position: static;
}

.megamenu {
    position: absolute;
    width: 100%;
    left: 0;
    right: 0;
    padding: 15px;
}

The issue is that when the mega menu opens, its content expands beyond the window width as shown here:

Example Image

I have tried adjusting the megamenu class to control the width and alignment within the menu, but it does not align properly with the container class. You can view my attempted solution in this fiddle:

Attempted Solution

I also created a demo where the mega menu stays within the bootstrap container, but it still has some issues like the navbar background not expanding correctly and the navbar not being fixed. Here's the link to that demo:

Container Demo

If anyone has suggestions on how to achieve the desired effect or improvements on my current attempts, I would really appreciate the help. Thank you!

Answer №1

To control the width of your mega menu, you can insert a .container within your .megamenu. You will need to include an additional <div> inside it. By using the provided HTML/CSS code below, you can set the default background of the parent megamenu as transparent and customize the background/border colors for the child <div>, labeled as .megamenu-inner.

https://i.sstatic.net/iatNZ.gif

Check out the updated fiddle here: https://jsfiddle.net/q15dxj2t/1/

<div class="dropdown-menu megamenu" aria-labelledby="navbarDropdown">
    <div class="container">
        <div class="megamenu-inner">
            <div class="row">
                <div class="col-6">
                    <a class="dropdown-item" href="#">Action</a>
                    <a class="dropdown-item" href="#">Another action</a>
                    <a class="dropdown-item" href="#">Something else here</a>
                </div>
                <div class="col-6">
                    <a class="dropdown-item" href="#">Action</a>
                    <a class="dropdown-item" href="#">Another action</a>
                    <a class="dropdown-item" href="#">Something else here</a>
                </div>
            </div>
        </div>
    </div>
</div>
.megamenu {
    position: absolute;
    width: 100%;
    left: 0;
    right: 0;
    padding: 0;
    background: transparent;
    border: none;
}

.megamenu-inner {
    background: red;
    padding: 15px;
    width: 100%;
}

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 permissible to use DEFER or ASYNC when including a stylesheet?

I am curious if script files are able to utilize the DEFER and ASYNC keywords on a resource include. Are these keywords also compatible with stylesheet (CSS) includes? The syntax I imagine would be: <link rel="stylesheet" type="text/css" href="./path/ ...

When you click on the text, it vanishes, but when you enter new text,

As I create my very first landing page, I aim for a simple and not too professional design to familiarize myself with building web pages independently. However, I am facing an issue where the text disappears when a user clicks on the textbox. The problem ...

Getting the value of elements with the same id in JavaScript can be achieved by utilizing the getElement

When I click on the first delete link, I want to display the value from the first input box. Similarly, when I click on the second delete link, I want to show the value from the second input box. Currently, it is always showing the value from the first del ...

Tips for evenly spacing items in a navigation bar

I'm having trouble centering the logo on the navbar as it always leans more towards the left. Is there a way to resolve this problem using Bootstrap classes? The navbar consists of a dropdown toggle button on the left, the logo in the center, and two ...

Include the circle.css file in the Angular 4 project by loading it in the head section of the

I am currently working with Angular 4 and would like to incorporate the circle.css styles from cssscript. After downloading the file from the provided link, I placed the circle.css within my something component file. However, when attempting to use &l ...

Incorporate a background image with the JavaScript CSS property

I'm having trouble adding a background image using the Javascript CSS property in my code. When I directly add the 'url', it works fine. Could the issue be with the 'weatherImage' variable? Javascript var OpenWeatherKey = ' ...

Tips for utilizing the jQuery selectbox plugin on multiple elements within a single page

On my webpage, there are 3 select boxes that I want to customize using a jQuery selectbox plugin. However, the issue I'm encountering is that the plugin only works on the first select box and the others remain unchanged. Does anyone know why this mig ...

Interactive HTML button capable of triggering dual functionalities

I am working on a project where I need to create a button using HTML that can take user input from a status box and display it on another page. I have successfully implemented the functionality to navigate to another page after clicking the button, but I ...

Obtain the precise X and Y coordinates of a <li> element regardless of where the mouse is clicked using Typescript

I'm stuck and could really use some assistance! Hey there, I have a simple question but I seem to be missing a crucial element. Here's my fiddle where I can detect the clicked item but that's as far as I've gotten: http://jsfiddle.ne ...

Launch an Android application directly from a web browser upon the webpage's loading

When a user visits www.example.com/myApp, I want my app to open automatically without any click required. I have attempted the following methods: window.onload = function () { window.location.replace("intent://something#Intent;scheme=myapp;packag ...

What is the best way to navigate from one div to a particular slide within a slider?

Let's say I have two web pages: index.html and page.html. On page.html, there is a slider with 9 slides created using the plugin StackSlider. Now, when a link on index.html is clicked, I want to navigate to a specific slide, like slide number 4, on pa ...

Recognize different HTML components when an event occurs

My application is equipped with a multitude of buttons, inputs, and other elements that trigger different events. I am looking for a way to easily differentiate between each element and the event it triggers. For instance, consider the following snippet f ...

Styling dropdown menus with CSS

I have encountered an issue with the dropdownlist control on my asp.net page. I set the width to 150px, but in Mozilla browser, the list items appear expanded while in IE, they are restricted to the width of the dropdown list. How can I achieve a consisten ...

What is the best way to align the button correctly beside the cluster of checkboxes within bootstrap framework?

Here is a snippet of HTML code that I am working with: <div class="container"> <form name ="queryForm"> <div class="form-group"> <p class="checkbox-inline"> <input type="checkbox" name="optionsRadios" id="checkOne" ...

Custom CSS styles for purchasing stocks using percentages

Can someone assist me in fixing the CSS for this custom card within a modal box? <!--Card--> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <input type="text" id="market-searc ...

Having trouble displaying Django CreateView forms on your HTML page?

I have encountered several issues with this particular topic. Unfortunately, I have not been able to find a suitable solution thus far. The CreateView form from models is not loading. models.py from django.db import models from django.contrib.auth.models ...

What is the reason for the disparity between CSS box-sizing content-box and border-box?

Consider the following example where I only changed the CSS box-sizing property from content-box to border-box. The resulting output difference is significant. Give this code a try: .box1 { width: 300px; height: 100px; border: 1px solid blue; pa ...

The dropdown menu adjusts its value based on the selected radio button

When I select the "12 hour" radio button, the drop-down values change to 1 am - 12 am and 1 pm - 12 pm. If I select 24 hours, then the values change to 1-24. Below is my code: <script src="http://code.jquery.com/jquery-1.7.2.min.js"></script> ...

Overriding the !important declaration in inline styles within various CSS IDs and classes

I need to find a way to override an inline !important declaration that is nested inside multiple CSS ids and classes. Let's analyze the following situation: <div class="A"> <div class="B"> <div class="C"> < ...

Is it possible to nest an HTML <span> inside a label tag in a Rails form_for?

Is it possible to nest a span element inside a form_for label tag? I am trying to achieve this in order to style a specific part of the label using CSS, such as making the text red. While it seems like valid HTML based on my research, it is causing some is ...