How to Fix bootstrap navigation toggle button in center - css

I have a problem in bootstrap 4 navigation, I want to set left Logo/Brand and center Navigation button and right login/My Account button, I have tried to solve this, but when I click navigation button on mobile it suddenly goes to the right, Please look at codeplay http://codeply.com/go/8FOVzFRkfM
<nav class="navbar navbar-expand-md navbar-light" >
<div class="container"> <a class="navbar-brand text-primary" href="#">
<i class="fa d-inline fa-lg fa-stop-circle"></i>
<b> BRAND</b>
</a> <button class="navbar-toggler navbar-toggler-right border-0" type="button" data-toggle="collapse" data-target="#navbar4">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbar4">
<ul class="navbar-nav ml-auto">
<li class="nav-item bg-white border-success"> <a class="nav-link" href="#">Features</a> </li>
<li class="nav-item"> <a class="nav-link" href="#">Pricing</a> </li>
<li class="nav-item"> <a class="nav-link" href="#">About</a> </li>
<li class="nav-item"> <a class="nav-link" href="#">FAQ</a> </li>
</ul>
</div>
<span> <a class="btn navbar-btn ml-md-2 btn-light bg-info">My Account</a></span>
</div>
</nav>

You just need to use on order-md-0 order-last the navbarcollapse so that it is positioned last on mobile screen widths (when the Navbar is collapsed)...
<div class="collapse navbar-collapse order-md-0 order-last" id="navbar4">
<ul class="navbar-nav ml-auto">
<li class="nav-item bg-white border-success"> <a class="nav-link" href="#">Features</a> </li>
<li class="nav-item"> <a class="nav-link" href="#">Pricing</a> </li>
<li class="nav-item"> <a class="nav-link" href="#">About</a> </li>
<li class="nav-item"> <a class="nav-link" href="#">FAQ</a> </li>
</ul>
</div>
https://www.codeply.com/go/KkYoATqtFy
Related: Bootstrap NavBar with left, center or right aligned items

The container inside the navbar is a flex having justify-content: space-between, Hence navbar-collpase which is initially hidden gets displayed on toggle gets pushed below.
Solution:
Making the navbar-collpase positioned absolute to the navbar will fix the problem.
Example:
#media only screen and (max-width: 600px) {
.navbar-collapse{
position: absolute;
width: 100%;
top: 56px; /*height of navbar in mobile*/
}
}

Related

How to get horizontal scroll to navbar in bootstrap 4 when there is lots of nav item

How to scroll navbar items in bootstrap menu when there is lot of items.
my code is
<nav class="navbar navbar-expand-lg navbar-dark bg-info fixed-top">
<a class="navbar-brand" href="/">Brand name</a>
<a class="btn btn-danger shadow-sm mr-sm-2 text-white" role="button" href="/demo"> Button</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">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
....
</ul>
Search
</div>
</nav>
I tried
.navbar-collapse {
max-height: 100vh;
overflow-y: scroll;
}
But I get a vertical scrollbar in navbar like this.
The trick here is to change the overflow-x property from scroll to auto.
overflow-x: auto; would put the scroll bar only when needed why overflow-x: scroll; will have
the scrollbar always.
JSFiddle
if you want to have horizontal scroll bar change overflow-y:scroll to overflow-x :scroll or overflow-x:auto.
overflow-x:autoadds scrollbar if content overflows

Bootstrap 4 – navbar search form min width

How to make 100% width search form with min-width in Bootstrap 4 navbar?
My markup here: https://jsfiddle.net/bfsrjo23/
<nav class="navbar navbar-expand-md navbar-light bg-faded">
Brand
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbar5">
<span class="navbar-toggler-icon"></span>
</button>
<div class="navbar-collapse collapse" id="navbar5">
<ul class="navbar-nav">
<li class="nav-item active">
<a class="nav-link" href="#">LongLink1 <span class="sr-only">Home</span></a>
</li>
<li class="nav-item"><a class="nav-link" href="#">LongLink2</a></li>
<li class="nav-item"><a class="nav-link" href="#">LongLink3</a></li>
<li class="nav-item"><a class="nav-link" href="#">LongLink4</a></li>
<li class="nav-item"><a class="nav-link" href="#">LongLink5</a></li>
<li class="nav-item"><a class="nav-link" href="#">LongLink6</a></li>
<li class="nav-item"><a class="nav-link" href="#">LongLink7</a></li>
</ul>
</div>
<form class="w-100 py-2">
<input type="text" class="form-control border" placeholder="Search...">
</form>
I put form outside collapse to make it always visible (laptop in 1st line, mobile in 2nd line). It's work fine exept Tablet mode (Viewport width around 880px), the search box too short. I want to add "min-width" and let form go in second line if size not pass.
Use css like this:
#yourformId > .input-group {
display: inline;
}
Just add class d-inline in nav, like this:
<nav class="navbar navbar-expand-md navbar-light d-inline bg-faded">
Brand
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbar5">
<span class="navbar-toggler-icon"></span>
</button>
<div class="navbar-collapse collapse" id="navbar5">
<ul class="navbar-nav">
<li class="nav-item active">
<a class="nav-link" href="#">LongLink1 <span class="sr-only">Home</span></a>
</li>
<li class="nav-item"><a class="nav-link" href="#">LongLink2</a></li>
<li class="nav-item"><a class="nav-link" href="#">LongLink3</a></li>
<li class="nav-item"><a class="nav-link" href="#">LongLink4</a></li>
<li class="nav-item"><a class="nav-link" href="#">LongLink5</a></li>
<li class="nav-item"><a class="nav-link" href="#">LongLink6</a></li>
<li class="nav-item"><a class="nav-link" href="#">LongLink7</a></li>
</ul>
</div>
<form class="w-100 py-2">
<input type="text" class="form-control border" placeholder="Search...">
</form>
Found acceptable solution - key is remove flex-wrap:wrap from css, for example with #media(min-width:992px), search form must have "display:flex;flex-basis:100%" or "width:100%". This will move search in second line.

How to keep navbar-brand Grouped with Links

I have this code and cant figure out how to keep the navbar-brand together with the links.
<nav class="mb-1 navbar navbar-expand-sm navbar-light fixed-top">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler collapsed"
type="button" data-toggle="collapse"
data-target="#navbarSupportedContent-5">
<span class="navbar-toggler-icon"></span>
</button>
<div class="navbar-collapse collapse"
id="navbarSupportedContent-5" style="">
<ul class="navbar-nav m-auto">
<li class="nav-item active">
<a class="nav-link" href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link"
href="#">
Features
</a>
</li>
<li class="nav-item">
<a class="nav-link"
href="#">Pricing</a>
</li>
</ul>
</div>
</nav>
The class nav-bar brand will not respond to "m-auto" or "text-align: center"
When I try to wrap them all in a div to center the whole group, it doesnt work and breaks the collapse button.
I can only find info on how to center the navbrand with links on either side but nothing about keeping the navbrand grouped with the links when centered.
I'd ideally like to create a fixed top navbar that has col-7 on left and col-5 on right, where each side is centered within its own column which is easy to do with divs, but this navbar css is not responding in the way I am used to.
I'm not entirely sure what you're trying to achieve with the col-5 and col-7, but to group the navbar brand and nav items, wrap both in a single div and use the flexbox util classes to center...
https://www.codeply.com/go/un2DWN8OTR
keep navbar-brand grouped with links
<nav class="mb-1 navbar navbar-expand-sm navbar-light fixed-top">
<div class="mx-auto d-sm-flex d-block flex-sm-nowrap">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler collapsed" type="button" data-toggle="collapse" data-target="#navbarSupportedContent-5">
<span class="navbar-toggler-icon"></span>
</button>
<div class="navbar-collapse collapse" id="navbarSupportedContent-5" style="">
<ul class="navbar-nav align-items-center">
<li class="nav-item active">
<a class="nav-link" href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">
Features
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Pricing</a>
</li>
</ul>
</div>
</div>
</nav>
Note: There is no m-auto class. Use mx-auto for horizontal (x-axis) auto margins to center.
Related:
Center an element in Bootstrap 4 Navbar
In order to have all the nav items together one the left side, remove the m-auto class from navbar-nav list.
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.0/css/bootstrap.css" rel="stylesheet"/>
<nav class="mb-1 navbar navbar-expand-sm navbar-light fixed-top">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler collapsed" type="button" data-toggle="collapse" data-target="#navbarSupportedContent-5">
<span class="navbar-toggler-icon"></span>
</button>
<div class="navbar-collapse collapse" id="navbarSupportedContent-5" style="">
<ul class="navbar-nav">
<li class="nav-item active">
<a class="nav-link" href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">
Features
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Pricing</a>
</li>
</ul>
</div>
</nav>
Check this pen on codepen
To align all the items at the center of the page, follow these three steps.
Override the flex-grow property of navbar-collapse. At the moment, it is flex-grow: 1.
.flex-grow-zero {
flex-grow: 0 !important;
}
Use the justify-content-center class to override justify-content:space-between property of navbar. As you may know, it aligns the content of a flex element at its center.
Remove the m-auto class from navbar-nav list.
.flex-grow-zero {
flex-grow: 0 !important;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.0/css/bootstrap.css" rel="stylesheet"/>
<nav class="mb-1 navbar navbar-expand-sm navbar-light fixed-top justify-content-center">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler collapsed" type="button" data-toggle="collapse" data-target="#navbarSupportedContent-5">
<span class="navbar-toggler-icon"></span>
</button>
<div class="navbar-collapse collapse flex-grow-zero" id="navbarSupportedContent-5" style="">
<ul class="navbar-nav">
<li class="nav-item active">
<a class="nav-link" href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">
Features
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Pricing</a>
</li>
</ul>
</div>
</nav>
Check this pen on Codepen
Remember to use the latest version of Bootstrap.

Bootstrap 4 - prevent links from breaking lines on navbar

When I have too many links in the navbar, when I start to zoom in, then the links start to break lines. I want them to stay the same line before they collapse.
In the snippet below(view full size), There are 6 links, and when you zoom in, there is a certain zoom where the links break lines (before the navbar collapses)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet"/>
<nav class="navbar navbar-expand-lg navbar-light fixed-top">
<div class="container">
<a class="navbar-brand mx-auto" href="#">
Logo
</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo02" aria-controls="navbarTogglerDemo02" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse text-right" id="navbarTogglerDemo02">
<ul class="navbar-nav ml-auto mt-2 mt-lg-0">
<li class="nav-item main-nav">
<a class="nav-link" href="#">Navbar Link Number 1</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Navbar Link Number 2</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Navbar Link Number 3</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Navbar Link Number 4</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Navbar Link Number 5</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Navbar Link Number 6</a>
</li>
</ul>
</div>
</div>
</nav>
Use the text-nowrap utility class on the links...
https://www.codeply.com/go/tKwvJnDJ42
<nav class="navbar navbar-expand-lg navbar-light fixed-top">
<div class="container">
<a class="navbar-brand mx-auto" href="#">
Logo
</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo02" aria-controls="navbarTogglerDemo02" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse text-right" id="navbarTogglerDemo02">
<ul class="navbar-nav ml-auto mt-2 mt-lg-0">
<li class="nav-item main-nav">
<a class="nav-link text-nowrap" href="#">Navbar Link Number 1</a>
</li>
<li class="nav-item">
<a class="nav-link text-nowrap" href="#">Navbar Link Number 2</a>
</li>
<li class="nav-item">
<a class="nav-link text-nowrap" href="#">Navbar Link Number 3</a>
</li>
<li class="nav-item">
<a class="nav-link text-nowrap" href="#">Navbar Link Number 4</a>
</li>
<li class="nav-item">
<a class="nav-link text-nowrap" href="#">Navbar Link Number 5</a>
</li>
<li class="nav-item">
<a class="nav-link text-nowrap" href="#">Navbar Link Number 6</a>
</li>
</ul>
</div>
</div>
</nav>
For anyone coming from Google, don't use as mentioned in another solution, that makes content unmanageable and uses markup to dictate presentation.
You can mimic what Bootstrap does with an easy class such as;
.text-nowrap {
white-space: nowrap !important;
}
As its a utility class, it is ok to use !important.
Hope that helps
You could hide overflown links if the users who zoom in are few, better than wrapping menu to 2 lines:
<style>
#media (min-width: 1201px) {
.navbar-nav {
overflow: hidden !important;
max-height: 50px;
}
}
</style>

Bootstrap 4 center navbar brand with collapse

Bootstrap 4 Alpha 6
I cant seem to figure out how to get both the right links and left link to both collapse. Right now only the right hand side link collapses.
<nav id="topNav" class="navbar fixed-top navbar-toggleable-sm navbar-inverse bg-inverse">
<button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target=".navbar-collapse">
Menu
</button>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
</ul>
</div>
<a class="navbar-brand mx-auto" href="#">NavBar</a>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link" href="#">About</a>
</li>
</ul>
</div>
You should wrap both the menus ul in one navbar-collapse div. Toggle Menu button will only target one collapse div not multiple. As earlier there were two navbar-collapse.
Here is code you should use.
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>
<nav id="topNav" class="navbar fixed-top navbar-toggleable-sm navbar-inverse bg-inverse">
<button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target=".navbar-collapse">
Menu
</button>
<a class="navbar-brand mx-auto" href="#">NavBar</a>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
</ul>
<ul class="nav navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link" href="#">About</a>
</li>
</ul>
</div>
</nav>
Currently in alpha 6, the collapse only supports a single target..
Update Bootstrap 4.1
You can absolute position the brand on larger screens, and use a single collapse for the 2 navbars.
#media (min-width: 768px) {
.navbar-brand
{
position: absolute;
left: 50%;
transform: translateX(-50%);
}
}
http://www.codeply.com/go/Sh05HDqIh1
Also see:
Center an element in Bootstrap 4 Navbar
How to center nav-items in Bootstrap?
<nav id="topNav" class="navbar fixed-top navbar-toggleable-sm navbar-inverse bg-inverse">
<button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target=".navbar-collapse">
Menu
</button>
<a class="navbar-brand mx-auto" href="#">NavBar</a>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
</ul>
<ul class="nav navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link" href="#">About</a>
</li>
</ul>
</div>
The code posted by ZimSystem will overlap the menu items due to having 100% width and might clash with click events etc. A better option would be to use left instead of width.
.navbar-brand
{
display: block;
left: 50%;
position: absolute;
text-align: center;
}

Resources