Sidebar on toggle, to push topbar - css

What I am trying to achieve is this:
Without using javascript/jquery.
I have a topbar and a sidebar. On smaller screens, I want the sidebar to collapse and have a button on top, and when the button is clicked the sidebar toggles horizontally, adding opacity to the background, putting the button on the topbar, and putting all content from within the topbar inside the sidebar.
Similar to when the screen is small for Youtube page, and the sign in option appears on the sidebar.
This is the code I have so far :JSFIDDLE
<header>
<nav class="navbar navbar-expand-sm fixed-top navbar-light bg-faded">
<button class="navbar-toggler " type="button" data-toggle="collapse" data-target="#sidebar" aria-controls="navbarsExampleDefault" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<img class="navbar-brand navbar-logo" src="" />
<div class="collapse navbar-collapse" id="navbarsExampleDefault">
<ul class="navbar-nav ml-auto user-info">
<li class="avatar">
<img src="">
</li>
<li class="nav-item dropdown">
<a class="nav-link ">Hello</a>
</li>
</ul>
</div>
</nav>
</header>
<div class="container-fluid">
<div id="mySidenav">
<nav class="col-sm-3 col-md-3 col-6 col-lg-2 d-sm-block sidebar collapse
width " id="sidebar">
<ul class="nav nav-pills flex-column">
<li class="nav-item collapsed side" data-toggle="collapse" data-target="#home">
<a class="nav-link">
<img src="" />Home </a>
</li>
<ul class="sub-menu collapse" id="home">
<li class="nav-item list-unstyled">
<a class="nav-link">Submenu1</a>
</li>
<li class="nav-item list-unstyled">
<a class="nav-link">Submenu2</a>
</li>
<li class="nav-item list-unstyled">
<a class="nav-link">Submenu3</a>
</li>
<li class="nav-item list-unstyled">
<a class="nav-link">Submenu4</a>
</li>
</ul>
<li class="nav-item side">
<a class="nav-link">
<img src="a" />Menu Item</a>
</li>
<li class="nav-item side">
<a class="nav-link">
<img src="" />Menu Item</a>
</li>
<li class="nav-item side">
<a class="nav-link">
<img src="" />Menu Item</a>
</li>
<li class="nav-item side">
<a class="nav-link">
<img src="" />Menu Item</a>
</li>
</ul>
</nav>
</div>
<main role="main" class="col-sm-9 ml-sm-auto col-md-9 col-lg-10 pt-3 content">
<h1>
Main Content here
</h1>
</main>
</div>

in your JSFiddle change css (top has to be 0px) : JsFiddle
.sidebar {
position: fixed;
top: 0px;
left: 0;
z-index: 1;
padding: 0;
overflow-x: hidden;
/* Scrollable contents if viewport is shorter than content. */
border-right: 1px solid #eee;
background-color: black;
height: 100%;
}

Related

Can you help me making more mobile responsive navbar?

I have tried making responsive navbar using Bootstrap 5, but my navbar-brand gives me problems. Letters overflow over other elements and my toggle-icon goes to new row and it should be in right corner in the same row as navbar header.
Here is codepen:
https://codepen.io/namename123/pen/VwQzRPL
.logo-iddeea {
font-family: 'Agency FB', sans-serif;
font-size: 3.5rem;
background-image: linear-gradient(to left,rgb(254, 168,100), rgb(240,104,69));
background-size: 100%;
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
-moz-background-clip: text;
-moz-text-fill-color: transparent;
}
.logo-image{
height:100px;
width:100px;
}
.portal-open-data{
font-size:1.3rem;
margin-top:-5%;
}
.nav-link {
color: #2c3474;
font-weight:bold;
}
.blue-color-font {
color: lightblue;
}
#media only screen and (max-width: 700px) {
.logo-slika{
width:80px;
height: 80px;
}
.logo-iddeea{
font-size:2.5rem;
}
.portal-otvorenih-podataka{
font-size:0.8rem;
}
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<link href="http://fonts.cdnfonts.com/css/agency-fb" rel="stylesheet">
<nav class="navbar navbar-expand-sm navbar-dark container-fluid bg-dark offset-0">
<div class="container-fluid">
<a class="navbar-brand " href="#">
<div class="row">
<div class="col-3"> <img src="https://shoutcart.com/uploads/gig/bd6f3a23cd5294a2370b09bc413f3331.png" alt="" class="d-inline-block logo-image"></div>
<div class="col-9">
<div class="row">
<div class="col-md-5 ps-4 ps-md-1 my-md-auto logo-iddeea">
AABBA
</div>
<div class="col-md-7 ps-sm-1 ps-4 blue-color-font my-md-auto portal-open-data ">
SOME COMPANY <div style="margin-top:-3%;"> TEXT</div>
</div>
</div>
</div>
</div>
</a>
<button type="button" class="navbar-toggler" data-bs-toggle="collapse" data-bs-target="#navbarCollapse">
<span class="navbar-toggler-icon"></span>
</button>
<div id="navbarCollapse" class="collapse navbar-collapse ms-5">
<ul class="nav navbar-nav">
<li class="nav-item ">
<a class="nav-link blue-color-font">Groups</a>
</li>
<li class="nav-item dropdown ">
Passwords
</li>
<li class="nav-item">
<a class="nav-link blue-color-font">Administrators</a>
</li>
<li class="nav-item ">
<a class="nav-link blue-color-font">Users</a>
</li>
<li class="nav-item ">
<a class="nav-link blue-color-font">Documentation</a>
</li>
<li class="nav-item ">
<a class="nav-link blue-color-font">Activity logs</a>
</li>
</ul>
<ul class="nav navbar-nav ms-auto">
<li class="nav-item dropdown card shadow">
Logged Username
<div class="dropdown-menu dropdown-menu-end orange-border">
Reports
Settings
<div class="dropdown-divider blue-color-font"></div>
<a>Logout</a>
</div>
</li>
</ul>
</div>
</div>
</nav>
<nav class="navbar navbar-expand-lg bg-dark navbar-dark">
<div class="container-fluid">
<a><img src="https://shoutcart.com/uploads/gig/bd6f3a23cd5294a2370b09bc413f3331.png" alt="" class="mx-2 img-responsive logo-image"></a>
<a class="navbar-brand logo-iddeea" href="#"><h1>AABBA</h1></a>
<span class="text-info"> SOME COMPANY TEXT</span>
<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" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">Groups</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
Dropdown
</a>
<ul class="dropdown-menu" aria-labelledby="navbarDropdown">
<li><a class="dropdown-item" href="#">Action</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
<li><hr class="dropdown-divider"></li>
<li><a class="dropdown-item" href="#">Something else here</a></li>
</ul>
</li>
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">User</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Administrators</a>
</li>
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">Documentation</a>
</li>
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">Activity logs</a>
</li>
</ul>
<form class="d-flex" role="search">
<button class="btn btn-light" type="submit">Search</button>
</form>
</div>
</div>
</nav>
use this code and remove all styles which are not required

Bootstrap 4 add vertical divider pipe

Hi i am trying to add some separator between nav-link but it does not work
i tried to add
nav-link:after {
content:"|";
}
but it is not working or it shows in the left side.
here is my code:
<nav class="navbar navbar-expand-md navbar-dark bg-color">
<div class="navbar-brand header-title">Title</div> <!-- this is in the middle -->
<div class="navbar-collapse collapse" id="collapsingNavbar">
<ul class="navbar-nav">
<li class="nav-item active">
<a class="navbar-brand" href="#">
<img src="logo.png" />
</a>
</li>
</ul>
<!-- Here between nav-item I would lik to add pipe speparator -->
<ul class="navbar-nav ml-auto">
<div class="nav-item nav-link mr-md-2">
<div> some test some test</div>
</div>
<div class="nav-item nav-link mr-md-2">
<div> some test</div>
</div>
<li class="nav-item nav-icons">
<a href="">
<span class="icon"> X </span>
</a>
</li>
<li class="nav-item nav-icons">
<a href="">
<span class="icon"> V </span>
</a>
</li>
</ul>
</div>
</nav>
You forgot . before class in css .nav-link and set position: absolute; to position it...
See fiddle:https://jsfiddle.net/3ah58br4/7/
body{background-color:black!important;}
.nav-link:after {
content: "|";
position: absolute;
top: 16px;
margin-left: -10px;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
<nav class="navbar navbar-expand-sm navbar-dark bg-color">
<div class="navbar-brand header-title">Title</div> <!-- this is in the middle -->
<div class="navbar-collapse collapse" id="collapsingNavbar">
<ul class="navbar-nav">
<li class="nav-item active">
<a class="navbar-brand" href="#">
<img src="logo.png" />
</a>
</li>
</ul>
<!-- Here between nav-item I would lik to add pipe speparator -->
<ul class="navbar-nav ml-auto">
<div class="nav-item nav-link mr-md-2">
<div> some test some test</div>
</div>
<div class="nav-item nav-link mr-md-2">
<div> some test</div>
</div>
<li class="nav-item nav-icons">
<a href="">
<span class="icon"> X </span>
</a>
</li>
<li class="nav-item nav-icons">
<a href="">
<span class="icon"> V </span>
</a>
</li>
</ul>
</div>
</nav>

Fixed navbar - don't scroll page behind, and make it scrollable

I have a couple of issues with Bootstrap 4 fixed-top navbar:
1) It scrolls the page behind it when in collapsed view - How to prevent that?
2) You can't scroll the navbar down if the links are out of the view, as you can see in the example below, I have 8 links, and added 35px paddings just to make them go out of the view. You cannot scroll down to them. Instead it scrolls the page behind it.
.navbar-nav > li > a {
padding:35px!important;
}
p {
padding-top:1000px; /* to emulate scroll */
}
<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.1/js/bootstrap.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet"/>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<nav class="navbar navbar-expand-lg navbar-light bg-light fixed-top">
<div class="container">
<a class="navbar-brand mx-auto" href="#">
<img src="https://loremflickr.com/320/240" width="35" height="35" alt="logo">
<span>Logo</span>
</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 py-0 text-center" href="#">Link 1</a>
</li>
<li class="nav-item main-nav">
<a class="nav-link py-0 text-center" href="#">Link 2</a>
</li>
<li class="nav-item main-nav">
<a class="nav-link py-0 text-center" href="#">Link 3</a>
</li>
<li class="nav-item main-nav">
<a class="nav-link py-0 text-center" href="#">Link 4</a>
</li>
<li class="nav-item main-nav">
<a class="nav-link py-0 text-center" href="#">Link 5</a>
</li>
<li class="nav-item main-nav">
<a class="nav-link py-0 text-center" href="#">Link 6</a>
</li>
<li class="nav-item main-nav">
<a class="nav-link py-0 text-center" href="#">Link 7</a>
</li>
<li class="nav-item main-nav">
<a class="nav-link py-0 text-center" href="#">Link 8</a>
</li>
</ul>
</div>
</div>
</nav>
<p class="text-center"><strong>I am content at the bottom</strong></p>
This issue you can solve it by 2 ways
you can define a height for the .navbar class for example it can be 80px and then the content behind it give it padding-top: 80px or margin-top: 80px with the same value or even more to have some more space
UPDATE
just add breakpoint #media screen and (max-width: 991px) { .navbar { height: auto } }
body { height: 2000px; }
.navbar {
height: 80px;
}
#media screen and (max-width: 991px) { .navbar { height: auto } }
p {
padding-top:85px; /* to emulate scroll */
}
<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.1/js/bootstrap.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet"/>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<nav class="navbar navbar-expand-lg navbar-light bg-light fixed-top">
<div class="container">
<a class="navbar-brand mx-auto" href="#">
<img src="https://loremflickr.com/320/240" width="35" height="35" alt="logo">
<span>Logo</span>
</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 py-0 text-center" href="#">Link 1</a>
</li>
<li class="nav-item main-nav">
<a class="nav-link py-0 text-center" href="#">Link 2</a>
</li>
<li class="nav-item main-nav">
<a class="nav-link py-0 text-center" href="#">Link 3</a>
</li>
<li class="nav-item main-nav">
<a class="nav-link py-0 text-center" href="#">Link 4</a>
</li>
<li class="nav-item main-nav">
<a class="nav-link py-0 text-center" href="#">Link 5</a>
</li>
<li class="nav-item main-nav">
<a class="nav-link py-0 text-center" href="#">Link 6</a>
</li>
<li class="nav-item main-nav">
<a class="nav-link py-0 text-center" href="#">Link 7</a>
</li>
<li class="nav-item main-nav">
<a class="nav-link py-0 text-center" href="#">Link 8</a>
</li>
</ul>
</div>
</div>
</nav>
<p class="spacing text-center"><strong>I am content at the bottom</strong></p>
this is more dynamic way if you don't want to have a fixed height and you can define it with js like that you just get the height of the header and then give it as padding-top to the content you want
var navH = $('.navbar').outerHeight();
console.log(navH);
var res = $('.spacing').css('padding-top', navH + 10); /* 10 in case you want more space*/
console.log(res);

Bootstrap Brand Image Showing too large

Perhaps its my css but my code should show the image in the header at a fixed size. However, it looks like it may be outside of the div. Heres the code.
<div class="row">
<div class="col-md-12 top navbar-fixed-top">
<div class="col-md-6">
<nav class="navbar navbar-default" role="navigation">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#"><img class="img-responsive" src="http://dev-httpwwwrestechsyscom.pantheonsite.io/sites/all/themes/myWay/logo.png"></a>
</div></nav></div>
<div class="col-md-6">
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<ul class="menu"><li class="first expanded dropdown active">About Us <span class="caret"></span><ul class="dropdown-menu"><li class="first leaf active">Mission Statement</li>
<li class="leaf">Meet Our Team</li>
<li class="leaf">Testimonials</li>
<li class="last leaf">Sample Athletes Profile</li>
</ul></li>
<li class="leaf">Eligibility</li>
<li class="expanded dropdown">Recruiting <span class="caret"></span><ul class="dropdown-menu"><li class="first leaf">Recruiting FAQ</li>
<li class="last leaf">Recruiting Info</li>
</ul></li>
<li class="last leaf">Services</li>
</ul><li class="active"></li>
</ul>
</div>
</div></div></div>
You can fix it in this way. Just add desired height to brand image and width will be auto based on height. For example:
.navbar-brand img {
height: 30px;
}
You can add a max-height: 100% to the .navbar-brand img element and it will contain the image inside of .navbar-brand. The height of .navbar-brand is 50px from bootstrap's css, so to make it bigger than that, change the height of .navbar-brand
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<style>
.navbar-brand img {
max-height: 100%;
}
/* add/change this if you want to make the navbar brand area bigger */
.navbar-brand {
height: 100px;
}
</style>
<div class="row">
<div class="col-md-12 top navbar-fixed-top">
<div class="col-md-6">
<nav class="navbar navbar-default" role="navigation">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#"><img class="img-responsive" src="http://dev-httpwwwrestechsyscom.pantheonsite.io/sites/all/themes/myWay/logo.png"></a>
</div>
</nav>
</div>
<div class="col-md-6">
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<ul class="menu">
<li class="first expanded dropdown active">About Us <span class="caret"></span>
<ul class="dropdown-menu">
<li class="first leaf active">Mission Statement</li>
<li class="leaf">Meet Our Team</li>
<li class="leaf">Testimonials</li>
<li class="last leaf">Sample Athletes Profile</li>
</ul>
</li>
<li class="leaf">Eligibility</li>
<li class="expanded dropdown">Recruiting <span class="caret"></span>
<ul class="dropdown-menu">
<li class="first leaf">Recruiting FAQ</li>
<li class="last leaf">Recruiting Info</li>
</ul>
</li>
<li class="last leaf">Services</li>
</ul>
<li class="active"></li>
</ul>
</div>
</div>
</div>
</div>

Bootstrap NavBar with left, center or right aligned items

In Bootstrap, what is the most platform-friendly way to create a navigation bar that has Logo A on the left, menu items in the center, and Logo B on the right?
Here is what I've tried so far, and it ends up being aligned so that Logo A is on the left, menu items next to the logo on the left and Logo B on the right.
<div class="navbar navbar-fixed-top navbar-custom ">
<div class="container" >
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#"><span class="navbar-logo">Logo_A</span></a>
</div>
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="active">Home</li>
<li>Menu Item 1</li>
<li>Menu Item 2</li>
<li>Menu Item 3</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><img src="images/Logo_B.png" class="img-responsive"></li>
</ul>
</div>
</div>
</div>
2021 Update
Bootstrap 5 (beta)
Bootstrap 5 also has a flexbox Navbar, and introduces new RTL support. For this reason the concept of "left" and "right" has been replaced with "start" and "end". Therefore the margin utilities changed for Bootstrap 5 beta:
ml-auto => ms-auto
mr-auto => me-auto
Also note that data-toggle and data-target have changed too:
data-toggle => data-bs-toggle
data-target => data-bs-target
Navbar Demo for Bootstrap 5
Bootstrap 4
Now that Bootstrap 4 has flexbox, Navbar alignment is much easier. Here are updated examples for left, right and center in the Bootstrap 4 Navbar, and many other alignment scenarios demonstrated here.
The flexbox, auto-margins, and ordering utility classes can be used to align Navbar content as needed. There are many things to consider including the order and alignment of Navbar items (brand, links, toggler) on both large screens and the mobile/collapsed views. Don't use the grid classes (row,col) for the Navbar.
Here are various examples...
Left, center(brand) and right links:
<nav class="navbar navbar-expand-md navbar-dark bg-dark">
<div class="navbar-collapse collapse w-100 order-1 order-md-0 dual-collapse2">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="#">Left</a>
</li>
<li class="nav-item">
<a class="nav-link" href="//codeply.com">Codeply</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>
<div class="mx-auto order-0">
<a class="navbar-brand mx-auto" href="#">Navbar 2</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".dual-collapse2">
<span class="navbar-toggler-icon"></span>
</button>
</div>
<div class="navbar-collapse collapse w-100 order-3 dual-collapse2">
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link" href="#">Right</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
</ul>
</div>
</nav>
http://codeply.com/go/qhaBrcWp3v
Another BS4 Navbar option with center links and overlay logo image:
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<div class="navbar-collapse collapse w-100 dual-collapse2 order-1 order-md-0">
<ul class="navbar-nav ml-auto text-center">
<li class="nav-item active">
<a class="nav-link" href="#">Link</a>
</li>
</ul>
</div>
<div class="mx-auto my-2 order-0 order-md-1 position-relative">
<a class="mx-auto" href="#">
<img src="//placehold.it/120/ccff00" class="rounded-circle">
</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".dual-collapse2">
<span class="navbar-toggler-icon"></span>
</button>
</div>
<div class="navbar-collapse collapse w-100 dual-collapse2 order-2 order-md-2">
<ul class="navbar-nav mr-auto text-center">
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
</ul>
</div>
</nav>
Or, these other Bootstrap 4 alignment scenarios:
brand left, dead center links, (empty right)
brand and links center, icons left and right
More Bootstrap 4 examples:
toggler left on mobile, brand right
center brand and links on mobile
right align links on desktop, center links on mobile
left links & toggler, center brand, search right
Also see: Bootstrap 4 align navbar items to the right
Bootstrap 4 navbar right align with button that doesn't collapse on mobile
Center an element in Bootstrap 4 Navbar
Bootstrap 3
Option 1 - Brand center, with left/right nav links:
<nav class="navbar navbar-default" role="navigation">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<a class="navbar-brand" href="#">Brand</a>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav navbar-left">
<li>Left</li>
<li>Left</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li>Right</li>
<li>Right</li>
</ul>
</div>
</nav>
.navbar-brand
{
position: absolute;
width: 100%;
left: 0;
text-align: center;
margin:0 auto;
}
.navbar-toggle {
z-index:3;
}
http://bootply.com/98314 (3.x)
Option 2 - Left, center and right nav links:
<nav class="navbar navbar-default" role="navigation">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>Left</li>
</ul>
<ul class="nav navbar-nav navbar-center">
<li>Center</li>
<li>Center</li>
<li>Center</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li>Right</li>
</ul>
</div>
</nav>
#media (min-width: 768px) {
.navbar-nav.navbar-center {
position: absolute;
left: 50%;
transform: translatex(-50%);
}
}
http://bootply.com/SGYC6BWeBK
Option 3 - Center both brand and links
.navbar .navbar-header,
.navbar-collapse {
float:none;
display:inline-block;
vertical-align: top;
}
#media (max-width: 768px) {
.navbar-collapse {
display: block;
}
}
http://codeply.com/go/1lrdvNH9GI
More examples:
Left brand, center links
Left toggler, center brand
For 3.x also see nav-justified: Bootstrap center navbar
Center Navbar in Bootstrap
Bootstrap 4 align navbar items to the right
Bootstrap 4
We have many ways to align navBars Items.
For Left Align
class = "navbar-nav mr-auto"
For Right Align
class = "navbar-nav ml-auto"
For Center Align
class = "navbar-nav mx-auto"
<nav class="navbar navbar-expand-sm bg-dark navbar-dark">
<a routerLink="/" class="navbar-brand" href="#">Bootstrap 4</a>
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link" href="home">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="about">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="contact">Contact us</a>
</li>
</ul>
</nav>
Bootstrap 4 (as of alpha 6)
Navbars are built with flexbox! Instead of floats, you’ll need flexbox and margin utilities.
For Align Right use justify-content-end on the collapse div:
<div class="collapse navbar-collapse justify-content-end">
<ul class="navbar-nav">
<li class="nav-item active">
<a class="nav-link" href="#">Home</a>
</li>
</ul>
</div>
Full example here: https://jsbin.com/kemawa/edit?output
I needed something similar (left, center and right aligned items), but with ability to mark centered items as active. What worked for me was:
http://www.bootply.com/CSI2KcCoEM
<nav class="navbar navbar-default" role="navigation">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li class="navbar-left">Left 1</li>
<li class="navbar-left">Left 2</li>
<li class="active">Center 1</li>
<li>Center 2</li>
<li>Center 3</li>
<li class="navbar-right">Right 1</li>
<li class="navbar-right">Right 2</li>
</ul>
</div>
</nav>
CSS:
#media (min-width: 768px) {
.navbar-nav {
width: 100%;
text-align: center;
}
.navbar-nav > li {
float: none;
display: inline-block;
}
.navbar-nav > li.navbar-right {
float: right !important;
}
}
In bootstrap 5 the classes are changed, so for
ml-auto is changes with ms-auto AND
mr-auto is changes with me-auto
For left we have to use s instead of l, And
For Right we have to use e instead of r
Smack my head, just reread my answer and realized the OP was asking for two logo's one on the left one on the right with a center menu, not the other way around.
This can be accomplished strictly in the HTML by using Bootstrap's "navbar-right" and "navbar-left" for the logos and then "nav-justified" instead of "navbar-nav" for your UL. No addtional CSS needed (unless you want to put the navbar-collapse toggle in the center in the xs viewport, then you need to override a bit, but will leave that up to you).
<nav class="navbar navbar-default" role="navigation">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<div class="navbar-brand navbar-left"><img src="http://placehold.it/150x30"></div>
</div>
<div class="navbar-brand navbar-right"><img src="http://placehold.it/150x30"></div>
<div class="navbar-collapse collapse">
<ul class="nav nav-justified">
<li>home</li>
<li>about</li>
</ul>
</div>
</nav>
Bootply: http://www.bootply.com/W6uB8YfKxm
For those who got here trying to center the "brand" here is my old answer:
I know this thread is a little old, but just to post my findings when working on this. I decided to base my solution on skelly's answer since tomaszbak's breaks on collaspe. First I created my "navbar-center" and turned off float for the normal navbar in my CSS:
.navbar-center
{
position: absolute;
width: 100%;
left: 0;
text-align: center;
margin: auto;
}
.navbar-brand{
float:none;
}
However the issue with skelly's answer is if you have a really long brand name (or you wanted to use an image for your brand) then once you get to the the sm viewport there could be overlapping due to the absolute position and as the commenters have said, once you get to the xs viewport the toggle switch breaks (unless you use Z positioning but I really didn't want to have to worry about it).
So what I did was utilize the bootstrap responsive utilities to create multiple version of the brand block:
<nav class="navbar navbar-default" role="navigation">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<div class="navbar-brand visible-xs">Brand That is Really Long</div>
</div>
<div class="navbar-brand visible-sm text-center">Brand That is Really Long</div>
<div class="navbar-brand navbar-center hidden-xs hidden-sm">Brand That is Really Long</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav navbar-left">
<li>Left</li>
<li>Left</li>
<li>Left</li>
<li>Left</li>
<li>Left</li>
<li>Left</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li>Right</li>
<li>Right</li>
<li>Right</li>
<li>Right</li>
<li>Right</li>
<li>Right</li>
</ul>
</div>
So now the lg and md viewports have the brand centered with links to the left and right, once you get to the sm viewport your links drop to the next line so that you don't overlap with your brand, and then finally at the xs viewport the collaspe kicks in and you are able to use the toggle. You could take this a step further and modify the media queries for the navbar-right and navbar-left when used with navbar-brand so that in the sm viewport the links are all centered but didn't have the time to vet it out.
You can check my old bootply here: www.bootply.com/n3PXXropP3
I guess having 3 brands might be just as much hassle as the "z" but I feel like in the world of responsive design this solution fits my style better.
this is an update for BS5 and a suggested edit for #Zim's answer. Unfortunately I can't submit the edit as the edit queue is full. It would be good if a user with high reputation could submit the edit once the queue allows new edits.
2021 Update
Bootstrap 5.0.0 beta-1
With the addition of RTL-support, ml-auto and mr-auto become ms-auto and me-auto (left/right to start/end).
Bootstrap 5 also requires the navbar to be contained inside a container div.
This code produces the equivalent of the first Bootstrap 4 example:
Left, center(brand) and right links:
<nav class="navbar navbar-expand-md navbar-dark bg-dark">
<div class="container-fluid">
<div class="navbar-collapse collapse w-100 order-1 order-md-0 dual-collapse2">
<ul class="navbar-nav me-auto">
<li class="nav-item active">
<a class="nav-link" href="#">Left</a>
</li>
<li class="nav-item">
<a class="nav-link" href="//codeply.com">Codeply</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>
<div class="mx-auto order-0">
<a class="navbar-brand mx-auto" href="#">Navbar 2</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".dual-collapse2">
<span class="navbar-toggler-icon"></span>
</button>
</div>
<div class="navbar-collapse collapse w-100 order-3 dual-collapse2">
<ul class="navbar-nav ms-auto">
<li class="nav-item">
<a class="nav-link" href="#">Right</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
</ul>
</div>
</div>
</nav>
In bootstrap 5 ms-auto worked for me to move the nav items right
the code of bootstrap was:
<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="#">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>
<li class="nav-item">
<a class="nav-link disabled">Disabled</a>
</li>
</ul>
</div>
</div>
</nav>
I changed at ul tag <ul class="navbar-nav"> to <ul class="navbar-nav ms-auto mb-2 mb-lg-0"> and it's worked fine for me
my code after editing is :
<nav class="navbar navbar-expand-lg navbar-light">
<div class="container-fluid">
<a class="navbar-brand nav-title" href="#">Cooking Master</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 ms-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link active" aria-current="page" 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>
<li class="nav-item">
<a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
</li>
</ul>
</div>
</div>
</nav>
You set the margin left to auto -> ml-auto on the section you want to move to the right.
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNavAltMarkup">
<div class="navbar-nav ml-auto"> !<--MARGIN LEFT AUTO HERE !-->
<a class="nav-item nav-link active" href="#">Home <span class="sr-only">(current)</span></a>
<a class="nav-item nav-link" href="#">Features</a>
<a class="nav-item nav-link" href="#">Pricing</a>
<a class="nav-item nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
</div>
</div>
I found the following was a better solution depending on the content of your left, center and right items. A width of 100% without a margin caused overlapping of divs and was preventing anchor tags to work correctly - that is without the messy use of z-indexes.
.navbar-brand
{
position: absolute;
width: 100%;
left: 0;
margin: auto;
margin-left: 48%;
}
This is a dated question but I found an alternate solution to share right from the bootstrap github page. The documentation has not been updated and there are other questions on SO asking for the same solution albeit on slightly different questions. This solution is not specific to your case but as you can see the solution is the <div class="container"> right after <nav class="navbar navbar-default navbar-fixed-top"> but can also be replaced with <div class="container-fluid" as needed.
<!DOCTYPE html>
<html>
<head>
<title>Navbar right padding broken </title>
<script src="//code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
</head>
<body>
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
Hello
</div>
<div class="collapse navbar-collapse navbar-ex1-collapse">
<ul class="nav navbar-nav navbar-right">
<li>
<div class="btn-group navbar-btn" role="group" aria-label="...">
<button type="button" class="btn btn-default" data-toggle="modal" data-target="#modalLogin">Se connecter</button>
<button type="button" class="btn btn-default" data-toggle="modal" data-target="#modalSignin">Créer un compte</button>
</div>
</li>
</ul>
</div>
</div>
</nav>
</body>
</html>
The solution was found on a fiddle on this page:
https://github.com/twbs/bootstrap/issues/18362
and is listed as a won't fix in V3.
<div class="d-flex justify-content-start">hello</div>
<div class="d-flex justify-content-end">hello</div>
<div class="d-flex justify-content-center">hello</div>
<div class="d-flex justify-content-between">hello</div>
<div class="d-flex justify-content-around">hello</div>
Put the fields that you want to center, right or left according above division.
In Zim's answer the example 5 left and center only with empty space right is not really centering the navlinks. We can fix it using a few changes:
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.0.2/css/bootstrap.min.css" rel="stylesheet" />
<p>Zim's original code</p>
<!-- example 5 left and center only with empty space right -->
<nav class="navbar navbar-expand-sm navbar-dark bg-primary flex-sm-nowrap flex-wrap">
<div class="container-fluid">
<button class="navbar-toggler flex-grow-sm-1 flex-grow-0 me-2" type="button" data-bs-toggle="collapse"
data-bs-target="#navbar5">
<span class="navbar-toggler-icon"></span>
</button>
<span class="navbar-brand flex-grow-1">Navbar 5</span>
<div class="navbar-collapse collapse flex-grow-1 justify-content-center" id="navbar5">
<ul class="navbar-nav mx-auto">
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Codeply</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
</ul>
</div>
<div class="flex-grow-1">
<!--spacer-->
</div>
</div>
</nav>
<div class="centerMarker">mid</div>
<br><br><br>
<p>Modified solution</p>
<!-- example 5 left and center only -->
<nav class="navbar navbar-expand-sm navbar-dark bg-primary flex-sm-nowrap flex-wrap">
<!-- 2.5rem is height of the toggler -->
<div class="container-fluid" style="height:2.5rem">
<span class="navbar-brand position-absolute">
<button class="navbar-toggler me-2" type="button" data-bs-toggle="collapse"
data-bs-target="#navbar5">
<span class="navbar-toggler-icon"></span>
</button>
Navbar 5
</span>
<div class="navbar-collapse collapse mx-auto w-100" id="navbar5">
<ul class="navbar-nav mx-auto">
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Codeply</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
</ul>
</div>
</div>
</nav>
<div class="centerMarker">mid</div>
<style>
body {
font-family: monospace;
padding: 10px
}
.centerMarker {
margin: 0 auto;
width: fit-content;
background-color: wheat;
}
</style>
Switch to full page and resize browser to see navlinks.
Bootstrap doesn't allow us to set flex-basis:0, so the property remains auto and the spacer doesn't expand equally. So the navlinks are not centered.
We can use class position-absolute on brand container and mx-auto w-100 on navlinks. The toggler sets the min height of the navbar to 2.5rem. That we have to set to the fluid div container.
The simplest solution:
Just divide the navbar into columns: for instance, if you have 24 columns over all, 12 are going to be empty and 12 are going to contrain the navbar:
<nav class="navbar navbar-default">
<div class="row">
<div class="col-sm-4"></div>
<div class="col-sm-4"></div>
<div class="col-sm-4"></div>
<div class="col-sm-12">
<ul class="nav navbar-nav" align="center">
<li>Home</li>
<li>First Link</li>
<li>Second Link</li>
<li>Third Link</li>
<li>Fourth Link</li>
</ul>
</div>
</div>
</nav>

Resources