How to make a mobile menu with columns in Bootstrap5? - html

I have a typical bootstrap navbar that collapses to a dropdown menu on smaller devices. As default menus do, it shows a vertical list of items, which takes up too much space, so I would like to group items in two columns. Here's a drawing of what I mean:
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<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">
<a class="nav-link" href="#">Item 2</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Item 3</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Item 4</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Item 5</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Item 6</a>
</li>
</ul>
</div>
</div>
</nav>
CodeSandbox code here
I tried grouping it using <div class="col-sm-4"> [three items] </div> but it failed, because I misunderstood how cols work. It grouped items only on the normal navbar, leaving hamburger menu untouched. Unfortunately, all solutions I could find refer to dropdown menus, but it doesn't translate to hamburgers, or at least I can't make it work.
I have no problems using JS if it isn't possible using pure B5/HTML/CSS. Also, I'd like not to change anything in how the standard navbar works on bigger devices, it shouldn't be grouped in any way - I want those columns to be present only on a hamburger menu.

The content of the navbar can be dynamic and there's a lot of ways to show two columns inside the navbar menu.
As a simple solution I used the a row container and columns just to illustrate the possibilities. You can add a divider that shows in mobile which is what is drawn.
Hope this helps and answers your question.
This is just one way to do this (codepen):
<div class="collapse navbar-collapse" id="navbarNav">
<div class="row">
<div class="col">
<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">
<a class="nav-link" href="#">Item 2</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Item 3</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Item 4</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Item 5</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Item 6</a>
</li>
</ul>
</div>
<div class="col">
<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">
<a class="nav-link" href="#">Item 2</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Item 3</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Item 4</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Item 5</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Item 6</a>
</li>
</ul>
</div>
</div>
</div>
Another possibility is just wrap the <ul> lists with a flex div which will also show the menu as two columns (codepen). I think this is a better solution I just wanted you to have more than one possibility.

Use the grid (row/cols) in the navbar-nav. Use the row-cols-* classes to set the number of columns. This way you won't have to split the columns in your markup.
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav flex-row row g-0 row-cols-2 row-cols-lg-6">
<li class="nav-item col">
<a class="nav-link active" aria-current="page" href="#">Item 1</a>
</li>
<li class="nav-item col">
<a class="nav-link" href="#">Item 2</a>
</li>
<li class="nav-item col">
<a class="nav-link" href="#">Item 3</a>
</li>
<li class="nav-item col">
<a class="nav-link" href="#">Item 4</a>
</li>
<li class="nav-item col">
<a class="nav-link" href="#">Item 5</a>
</li>
<li class="nav-item col">
<a class="nav-link" href="#">Item 6</a>
</li>
</ul>
</div>
Responsive demo

Related

Navbar disappears on resizing

My Navigation bar disappears when the web page is shrinks, even clicking on the toggler when the page shrinks doesn't display the links at all. I am using bootstrap 4.0 and linking to cdn links
for both the css and other JavaScript needed and it keeps disappearing.
<nav class="navbar navbar-expand-lg navbar-light bg-light" id="navbar">
<div class="container-fluid">
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse ml-2 top-bar" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">Home </a>
</li>
<li class="nav-item collections">
<a class="nav-link collections" href="#">Services</a>
<ul class="dropdown-menu dropdown-firstmenu">
<li class="dropdown-submenu">
<a tabindex="-1" href="#">2014 <span class="dropdown-caret"><i class="fa fa-caret-right"></i></span></a>
</li>
<li class="dropdown-submenu">
<a tabindex="-1" href="#">2014 <span class="dropdown-caret"><i class="fa fa-caret-right"></i></span></a>
</li>
<li class="dropdown-submenu">
<a tabindex="-1" href="#">2014 <span class="dropdown-caret"><i class="fa fa-caret-right"></i></span></a>
</li>
<li class="dropdown-submenu">
<a tabindex="-1" href="#">2014 <span class="dropdown-caret"><i class="fa fa-caret-right"></i></span></a>
</li>
</ul>
</li>
<li class="nav-item collections">
<a class="nav-link collections" href="#">Products</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Support</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Downloads</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Blog</a>
</li>
<!-- drop down -->
<li class="nav-item collections">
<a class="nav-link collections" href="#">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Contact</a>
</li>
<!-- end drop down -->
</ul>
<ul class="navbar-nav ">
<li class="nav-item">
<a class="nav-link" href="#">My Account</a>
</li>
</ul>
I was able to fix this by downloading the bootstrap 4.0 css and changing the navbar display from display:none to display:block

Can you make a fixed-top navbar scrollable from device with short height

I am working in Bootstrap 4 and looking to see if it is possible to have a fixed-top navbar that is scrollable. I'm working on a mega menu and portions of it are not visible on a phone depending on what link you select and there is no way to scroll to see the bottom of the menu.
Using a sticky top nav works well but requires you to scroll to the very bottom of the page before the menu scrolls. Also, IE11 doesn't support position: sticky.
Here are some screenshots where scrolling doesn't allow you to see the full menu:
Expanded menu from the top of page
Expanded menu from the bottom of page
https://codepen.io/carboncopylabs/pen/BaQgjzY
It seems the Bootstrap team made the decision that you can have a fixed navbar with a short menu or a long menu, but not with a fixed navbar (based on this and this issue reports).
Fortunately, the discussion did include a solution, which kind of worked (the bottom of the menu when dropped down was right at the edge of the display) – I modified the code.
function setCss() {
$('.navbar .navbar-collapse').css('max-height', 'calc(100vh - ' + $('.navbar').outerHeight() + 'px' + ')');
}
setCss();
$(window).resize(function() {
setCss();
});
<link href="https://cdn.jsdelivr.net/npm/bootstrap#4.5.3/dist/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap#4.5.3/dist/js/bootstrap.bundle.min.js"></script>
<style>
body {
height: 3000px
}
.navbar .navbar-collapse {
overflow-y: auto;
}
.container {
margin-top: 100px
}
</style>
<nav class="navbar navbar-expand-md navbar-dark fixed-top bg-dark">
<a class="navbar-brand" href="#">Fixed navbar</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarCollapse">
<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 1</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link 2</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link 3</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link 4</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link 5</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link 6</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link 7</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link 8</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link 9</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link 10</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="http://example.com" id="dropdown01" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dropdown</a>
<div class="dropdown-menu" aria-labelledby="dropdown01">
<a class="dropdown-item" href="#">Dropdown link 1</a>
<a class="dropdown-item" href="#">Dropdown link 2</a>
<a class="dropdown-item" href="#">Dropdown link 3</a>
<a class="dropdown-item" href="#">Dropdown link 3</a>
<a class="dropdown-item" href="#">Dropdown link 4</a>
<a class="dropdown-item" href="#">Dropdown link 5</a>
</div>
</li>
</ul>
</div>
</nav>
<main role="main" class="container">
<div class="jumbotron">
<h1>Navbar example</h1>
<p class="lead">This example is a quick exercise to illustrate how fixed to top navbar works. As you scroll, it will remain fixed to the top of your browser's viewport.</p>
<a class="btn btn-lg btn-primary" href="https://getbootstrap.com/docs/4.0/components/navbar/" role="button">View navbar docs »</a>
</div>
</main>

Can't get aligned right nav items with Bootstrap 4 to appear in responsive view

I've been trying to figure this out for a while now. I'm a bit of a noob when it comes to Bootstrap, so please allow me the probably simple question.
I'm trying to create a navbar with bootstrap 4, with the majority of menu items on the left, but with the login button on the right. I've figured some roundabout way of doing it (which could be wrong) but whenever I view the page in phone or tablet view, the 'login' item of the menu doesn't appear when one clicks the burger menu icon.
It appears up to a point (think it's about 760px) and then just disappears from view.
Here's the markup to this point, any help at all would be appreciated.
<a class="navbar-brand" href="#"><img src="imgs/branding/nerve logo_small.png" width="50" height="50" alt=""></a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item active">
<a class="nav-link" href="index.php">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Features</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Placeholder 1</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Placeholder 2</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Placeholder 3</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Placeholder 4</a>
</li>
</ul>
</div>
<ul class="navbar-nav flex-row ml-md-auto d-none d-md-flex">
<li class="nav-item text-right">
<a class="nav-link " href="#">Login</a>
</li>
</ul>
</nav>
I think, the problem is here
<ul class="navbar-nav flex-row ml-md-auto d-none d-md-flex">
<li class="nav-item text-right">
<a class="nav-link " href="#">Login</a>
</li>
</ul>
d-none d-md-flex - this part means, that this element will be hidden (d-none) until 768px (d-md-flex), and then will be displayed as flex. Try to remove d-none
UPDATED
The best way is to remove a standalone login ul and add Login as a part of a global ul list.
<ul class="navbar-nav w-100">
<li class="nav-item active">
<a class="nav-link" href="index.php">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Features</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Placeholder 1</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Placeholder 2</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Placeholder 3</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Placeholder 4</a>
</li>
<li class="nav-item flex-sm-grow-1 text-sm-right">
<a class="nav-link " href="#">Login</a>
</li>
</ul>
Look closer to flex-sm-grow-1 text-sm-right in Login li. And don't forget to add w-100 to ul.

Why my element needs to be clicked on in order for CSS to work?

My element has to be clicked on for CSS to work.
I tried using bootstrap for a nav bar. With 1 logout link on the top right.
I used navbar-nav ml-auto for it.
After that, I tried to override spacing of bootstrap by adding margin-right: 50% !important;
When I refresh the page, it is not applied immediately. If I clicked on the link, it will "jump" to the left as I expected.
Here is what I've tried:
<nav class="navbar navbar-expand-sm bg-light">
<!-- Links -->
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Category</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Blog posts</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Role Management</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">User Management</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">File and Folder Management</a>
</li>
</ul>
<ul class="navbar-nav ml-auto">
<li>
<a class="nav-item" id="logout-btn" href="#"> Logout </a>
</li>
</ul>
</nav>
CSS:
* {
/*display: none;*/
box-sizing: border-box;
}
#logout-btn.nav-item {
margin-right: 50% !important;
}
Can you help me explain why, or is it my browser's fault?
I find it quite weird, already cleared cached.
Remove css style for logout-btn.nav-item which you override. this get applied to the anchor tag. we need to add margin to the ul tag not to a tag. so we have bootstrap 4 in-built margin classes, so we can make use of it. so remove overrided styles and add mr-4 class to the second ul which contains the logout link.The modified code is given below.
<nav class="navbar navbar-expand-sm bg-light">
<!-- Links -->
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Category</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Blog posts</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Role Management</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">User Management</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">File and Folder Management</a>
</li>
</ul>
<ul class="navbar-nav ml-auto mr-4">
<li>
<a class="nav-item" id="logout-btn" href="#"> Logout </a>
</li>
</ul>
</nav>

How do I give external or local html doc link to bootstrap nav-pills?

I need to link my another webpage to my pictures nav menu.
Ex: if I click my pictures menu , it opens google.com.
Framework: bootstrap 4
div class="row row2">
<div class="col-md-12">
<ul class="nav nav-pills justify-content-center nav-justified" role="tablist">
<li class="nav-item">
<a class="nav-link" data-toggle="pill" href="#">About me</a>
</li>
<li class="nav-item">
<a class="nav-link" data-toggle="pill" href="#">My pictures</a>
</li>
<li class="nav-item">
<a class="nav-link" data-toggle="pill" href="#">My music</a>
</li>
<li class="nav-item">
<a class="nav-link" data-toggle="pill" href="#">My files</a>
</li>
<li class="nav-item">
<a class="nav-link" data-toggle="pill" href="#">My videos</a>
</li>
</ul>
</div>
</div>
This will work :)
<div class="row row2">
<div class="col-md-12">
<ul class="nav nav-pills justify-content-center nav-justified" role="tablist">
<li class="nav-item">
<a class="nav-link" data-toggle="pill" href="#">About me</a>
</li>
<li class="nav-item">
<a class="nav-link" target="_blank" href="http://google.com">My pictures</a>
</li>
<li class="nav-item">
<a class="nav-link" data-toggle="pill" href="#">My music</a>
</li>
<li class="nav-item">
<a class="nav-link" data-toggle="pill" href="#">My files</a>
</li>
<li class="nav-item">
<a class="nav-link" data-toggle="pill" href="#">My videos</a>
</li>
</ul>
</div>
</div>
This is the correct code for my question. i solve this with your help. thankyou
Html
<div class="row row2">
<div class="col-md-12">
<ul class="nav nav-pills justify-content-center nav-justified" role="tablist">
<li class="nav-item">
<a class="nav-link" href="about_me.html">About me</a>
</li>
<li class="nav-item">
<a class="nav-link" href="my_pictures.html">My pictures</a>
</li>
<li class="nav-item">
<a class="nav-link" href="my_music.html">My music</a>
</li>
<li class="nav-item active">
<a class="nav-link" href="my_files.html">My files</a>
</li>
<li class="nav-item">
<a class="nav-link" href="my_videos.html">My videos</a>
</li>
</ul>
</div>
</div>
CSS
.active {
background-color: #39a0c9 !important;
color: #fff !important;
}