I’m trying to make a Bootstrap 5.2 Mega Menu with full page-wide background, but I’m runnin into issues.
I have a basic navigation:
<nav class="navbar navbar-expand">
<div class="container-fluid">
<div class="collapse navbar-collapse" id="navbarNavDropdown">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">Item 1</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
Dropdown
</a>
<div class="dropdown-menu" aria-labelledby="megaMenuDropdown" style="background-color: blue;">
<div class="container" style="background-color: red;">
<div class="row">
<div class="col-12">
COL 12 WITH INSIDE CONTAINER
</div>
</div>
</div>
</div>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Item 3</a>
</li>
</ul>
</div>
</div>
</nav>
Now, if I add position: static !important
, like most 7-9year old post here tell you to, I get this:
This is close, but I need the horizontal background NOT stop at the container with, but cover the whole page (colored blue in my example).
Now, I can get it to full page with by adding width: 100vw;
to .dropdown-menu
item, but then I get this:
It is 100% width, but it started from the trigger nav
element (not start of page) and goes outside the viewpoint and I can’t find a solution that would put it at the start. Or even kinda center it, so that it is covering the page nicely.
I found a few examples online, including this one and they all have the same issue as well: https://mdbootstrap.com/docs/standard/extended/mega-menu/
They start at the element and go outside the window.
Anyone knows how to fix this and make the menu full page with starting from left side of the page?
Has something changed that creates this behaviour, as I don’t recall having these probles before?
The project is based on Bootstrap and writing custom navigation would be the last thing I wish to do.
Thank you.
2
Answers
Dug a bit more and found out I can still force center it with the
transform
element:So the style addition, to
Bootstrap 5.2
default, looks like this:Probably can some of those replace with Bootstrap built in class, but just leaving this here for others, who might stumble on this issue.
Here’s a solution with using only BS utility classes:
dropdown
position static withposition-static
(to make dropdown menu not adjust to it)container-fluid
position relative withposition-relative
(to make dropdown menu adjust to it, instead of dropdown container)dropdown-menu
width 100% withw-100
demo: