This question already has answers here:
Bootstrap NavBar with left, center or right aligned items
(14 answers)
Center an element in Bootstrap Navbar
(10 answers)
Closed 4 years ago.
I need to achieve the brand logo in left, menu items to be center and couple of buttons in the right. I have tried using the flexbox approach but the center items does not align well. and also the brand logo has a wide space with a clickable link.
JS Fiddle
<nav class="navbar navbar-light navbar-expand-md bg-faded justify-content-center">
<div class="container">
<img src="assets/logo/" alt="logo">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#collapsingNavbar3">
<span class="navbar-toggler-icon"></span>
</button>
<div class="navbar-collapse collapse w-100 ml-auto" id="collapsingNavbar3">
<ul class="navbar-nav w-100 justify-content-center">
<li class="nav-item active">
<a class="nav-link" href="#">How it works</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Become a tutor</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Pricing</a>
</li>
</ul>
<ul class="nav navbar-nav ml-auto w-100 justify-content-end">
<li class="nav-item">
<a class="btn btn-link" href="#">Sign In</a>
</li>
<li class="nav-item">
<a class="btn btn-primary" href="#">Sign Up</a>
</li>
</ul>
</div>
</div>
</nav>
Answering my own question few months later.
<nav class="navbar navbar-light navbar-expand-md bg-white justify-content-center main--navigation dark-header">
<a href="#" class="navbar-brand d-flex w-50 mr-auto">
<img src="assets/images/logo-header.svg" class="navigation__logo" alt="Logo" />
</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#collapsingNavbar">
<span class="navbar-toggler-icon"></span>
</button>
<div class="navbar-collapse collapse w-100" id="collapsingNavbar">
<ul class="navbar-nav w-100 justify-content-center">
<li class="nav-item active">
<a class="nav-link" href="#">Browse</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">How it works</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Become a tutor</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Prices</a>
</li>
</ul>
<ul class="nav navbar-nav ml-auto w-100 justify-content-end">
<li class="nav-item dropdown no-arrow mx-1 nav-link px-0">
<button id="bookmarksDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" type="button" class="btn btn-outline-secondary btn-outlined" data-original-title="Bookmarks"><img src="assets/icons/bookmark-header-dark.svg"></button>
<div class="dropdown-list dropdown-menu dropdown-menu-right" aria-labelledby="alertsDropdown">
<h6 class="dropdown-header">
Bookmarks
</h6>
</div>
</li>
<li class="nav-item dropdown no-arrow mx-1 nav-link px-0">
<button id="messagesDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" type="button" class="btn btn-outline-secondary btn-outlined" data-original-title="Notifications"><img src="assets/icons/bell-header-dark.svg"></button>
<div class="dropdown-list dropdown-menu dropdown-menu-right" aria-labelledby="notificationsDropdown">
<h6 class="dropdown-header">
You do not have any notifications yet
</h6>
</div>
</li>
<li class="nav-item dropdown no-arrow mx-1 ml-3 mr-0 nav-link px-0">
<a class="nav-link dropdown-toggle pr-0" href="#" id="userDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<div class="header__profile__title d-inline-block text-right align-middle">
<span class="mr-2 d-none d-block header__profile__name">Aaron Alexander</span>
<span class="mr-2 d-none d-block header__profile__role">Student</span>
</div>
<div class="header__profile d-inline-block">
<img class="img-profile rounded" src="assets/images/andrew-franklin.jpeg" alt="Aaron Alexander">
</div>
</a>
<div class="dropdown-menu dropdown-menu-right shadow animated--grow-in" aria-labelledby="userDropdown">
<a class="dropdown-item" href="#">
<i class="fas fa-user fa-sm fa-fw mr-2"></i>
Profile
</a>
<a class="dropdown-item" href="#">
<i class="fas fa-cogs fa-sm fa-fw mr-2"></i>
Settings
</a>
<a class="dropdown-item" href="#">
<i class="fas fa-list fa-sm fa-fw mr-2"></i>
Activity Log
</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#" data-toggle="modal" data-target="#logoutModal">
<i class="fas fa-sign-out-alt fa-sm fa-fw mr-2 text-gray-400"></i>
Logout
</a>
</div>
</li>
</ul>
</div>
</nav>
That container div you added after the nav makes you loose the flexbox attributes.
You also set the logo with the class "w-100" which takes all possible width.
I removed those and got a nicely aligned navbar.
<nav class="navbar navbar-light navbar-expand-md bg-faded justify-content-center align-items-center">
<img src="assets/logo/" alt="logo">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#collapsingNavbar3">
<span class="navbar-toggler-icon"></span>
</button>
<div class="navbar-collapse collapse w-100 ml-auto" id="collapsingNavbar3">
<ul class="navbar-nav w-100 justify-content-center">
<li class="nav-item active">
<a class="nav-link" href="#">How it works</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Become a tutor</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Pricing</a>
</li>
</ul>
<ul class="nav navbar-nav ml-auto w-100 justify-content-end">
<li class="nav-item">
<a class="btn btn-link" href="#">Sign In</a>
</li>
<li class="nav-item">
<a class="btn btn-primary" href="#">Sign Up</a>
</li>
</ul>
</div>
</nav>
Here's a fiddle with the results.
here is my working example.
You just need to put all content into single div in order to Justify content with bootstrap classes. Also, I am using navbar-brand and making them show hide depending on screen resolution. You can do that with js which might be smoother once the page loads completely.
#media only screen and (max-width: 991px) {
.logo {
display: flex;
}
.collapse .navbar-brand {
display: none
}
}
#media only screen and (min-width: 992px) {
.logo {
display: none;
}
.collapse .navbar-brand {
display: none;
}
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
<nav class="navbar navbar-expand-lg navbar-light bg-light d-flex ">
<a class="navbar-brand logo" href="#">Navbar</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 justify-content-between" id="navbarSupportedContent">
<a class="navbar-brand" href="#">Navbar</a>
<ul class="navbar-nav">
<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</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropdown
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#">Disabled</a>
</li>
</ul>
<form class="form-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
</div>
</nav>
Related
I'm building a page using Bootstrap 5 with a fixed top navbar and I would like the "active" item to have a solid background color similar to here.
When I open the developer tools I see that the items do not fill the entire height of the navbar, and I'm new to Bootstrap and quite rusty at HTML/CSS so I couldn't figure out what elements are relevant to style to get what I want.
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap#5.2.0/dist/css/bootstrap.min.css" integrity="sha384-gH2yIJqKdNHPEq0n4Mqa/HGKIhSkIHeL5AyhkYV8i59U5AR6csBvApHHNl/vI1Bx" crossorigin="anonymous">
<div class="container">
<a class="navbar-brand" href="#">
<img class src="https://via.placeholder.com/50" alt="Foobar" height="32">
</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-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 me-auto mb-2 mb-md-0">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">Dropdown</a>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">Item A #1</a></li>
<li><a class="dropdown-item" href="#">Item B #2</a></li>
</ul>
</li>
<li class="nav-item">
<a class="nav-link active" href="#" role="button" aria-expanded="false">
Active
</a>
</li>
</ul>
<ul class="navbar-nav flex-row ml-md-auto d-none d-md-flex">
<li class="nav-item">
<a class="nav-link p-2 bi bi-github" href="#" target="_blank" rel="noopener" aria-label="GitHub" style="font-size: 1.2em"></a>
</li>
</ul>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.2.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-A3rJD856KowSb7dwlZdYEkO39Gagi7vIsF0jrRAoQmDKKtQBHUuLZ9AsSv4jD4Xa" crossorigin="anonymous"></script>
A few things get you started in this direction:
Use the standard navbar layout so you have list items at the top level and not just raw buttons.
Remove the padding on the outer element with p-0 so the list items are full height. Those could be made taller again with other methods if you prefer, as I've done here via vertical padding.
Add hover styling with some custom CSS. Here I've used a media query to only apply the effect where the navbar is in desktop mode (992px and above).
View this demo in fullscreen mode to see the result.
#media (min-width: 992px) {
.hover-bg > .nav-item {
padding: .5rem;
}
.hover-bg > .nav-item:hover {
background: pink;
}
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap#5.2.0/dist/css/bootstrap.min.css" integrity="sha384-gH2yIJqKdNHPEq0n4Mqa/HGKIhSkIHeL5AyhkYV8i59U5AR6csBvApHHNl/vI1Bx" crossorigin="anonymous">
<nav class="navbar navbar-expand-lg bg-light p-0">
<div class="container-fluid">
<a class="navbar-brand" href="#">Navbar</a>
<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 hover-bg">
<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="#">Link</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>
<ul class="dropdown-menu">
<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 disabled">Disabled</a>
</li>
</ul>
<form class="d-flex" role="search">
<input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success" type="submit">Search</button>
</form>
</div>
</div>
</nav>
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.2.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-A3rJD856KowSb7dwlZdYEkO39Gagi7vIsF0jrRAoQmDKKtQBHUuLZ9AsSv4jD4Xa" crossorigin="anonymous"></script>
I want my navbar to appear as a list after collapsing when User press toggle button but it appears in one line and not as a list
This is how the navbar looks i dont want it
This is how i want it to look
<!-- Navbar -->
<nav class="navbar navbar-light navbar-expand-md bg-dark">
<i class="fas fa-child text-warning fa-2x"></i>
<button type="button" class="navbar-toggler bg-light" data-toggle = "collapse" data-target ="#nav">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse justify-content-between" id="nav">
<ul class="navbar nav">
<li class = "nav-item">
<a class="nav-link text-light font-weight-bold text-uppercase px-3" href="#">Home</a>
</li>
<li class = "nav-item">
<a class="nav-link text-light font-weight-bold text-uppercase px-3" href="#">Skills</a>
</li>
<li class = "nav-item dropdown" data-toggle = "dropdown">
<a class="nav-link text-light font-weight-bold text-uppercase px-3 dropdown-toggle" href="#">Projects</a>
<div class="dropdown-menu">
<a class="dropdown-item" href="#">Project 1</a>
<a class="dropdown-item" href="#">Project 2</a>
<a class="dropdown-item" href="#">Project 3</a>
<a class="dropdown-item" href="#">Project 4</a>
</div>
</li>
<li class = "nav-item">
<a class="nav-link text-light font-weight-bold text-uppercase px-3" href="#">Team</a>
</li>
<li class = "nav-item">
<a class="nav-link text-light font-weight-bold text-uppercase px-3" href="#">Contact</a>
</li>
</ul>
<form class="form-inline">
<div class="input-group">
<input type="text" name="" placeholder="Search" class="form-control">
</div>
<div class="input-group-append">
<button class="btn">
<i class="fas fa-search text-muted "></i>
</button>
</div>
</form>
</div>
</nav>
<!-- End of navbar -->
how about this?
<!-- Navbar -->
<nav class="navbar bg-dark navbar-dark">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-
toggle="collapse" data-target="#collapsibleNavbar" aria-
expanded="true">
<span class="navbar-toggler-icon"></span>
</button>
<div class="navbar-collapse collapse show"
id="collapsibleNavbar" style="">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="javascript:void(0)">Link</a>
</li>
<li class="nav-item">
<a class="nav-link" href="javascript:void(0)">Link</a>
</li>
<li class="nav-item">
<a class="nav-link" href="javascript:void(0)">Link</a>
</li>
<li>
<form class="form-inline">
<div class="input-group">
<input type="text" name="" placeholder="Search"
class="form-control">
</div>
</li>
</ul>
</div>
</nav>
<!-- End of navbar -->
I have a bootstrap nav bar, and I would like to have both collapse-able and non collapse-able menu items.
my code is as follows:
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<nav class="navbar navbar-toggleable-md navbar-light fixed-top">
<div class="container">
<a class="navbar-brand" href="https://ngohub.asia">
<%= image_tag 'ngohublogo.png', class: 'hidden-md-down img-responsive', style: 'width: 100%'%>
<%= image_tag 'ngohublogo.png', class: 'hidden-lg-up img-responsive w-100'%>
</a>
<div class="w-100" style="float:right;">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="#">Login <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item active">
<a class="nav-link">|</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Sign Up </a>
</li>
</ul>
</div>
<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 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</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropdown
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#">Disabled</a>
</li>
</ul>
<form class="form-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
</div>
</nav>
The login and sign up links appear as expected to the left of the brand,
however when I decrese the size of the browser window to simulate a mobile device, the links aren't sharing space with the brand as I would like:
Here it is on a full width screen
here it is in a simulated mobile view. I would like login and sign up to be next to the brand and next to the hamburger
First thing of note, visibility helpers have been renamed in the latest version of bootstrap. hidden-md has been replaced with .d-md-none, etc.
Second, I made sure to declare flex-direction: row; on .navbar-nav to arrange it's children horizontally instead of vertically.
.navbar .navbar-nav{
flex-direction: row;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<nav class="navbar navbar-toggleable-md navbar-light fixed-top">
<div class="container">
<a class="navbar-brand" href="https://ngohub.asia">
<img src="https://via.placeholder.com/200x50" class="hidden-md-down img-responsive" style="width: 100%" />
</a>
<div style="float:right;">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="#">Login <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item active">
<a class="nav-link">|</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Sign Up </a>
</li>
</ul>
</div>
<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 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</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropdown
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#">Disabled</a>
</li>
</ul>
<form class="form-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
</div>
</nav>
There are a few different ways you can approach this. Remember that in Bootstrap 4.1, the navbar-toggleable classes have been replaced with navbar-expand-.
Use flex-row on the first navbar-nav so that it doens't wrap/stack in a column on smaller screens. Use flex-grow-1 on the collapse so that it expands the remaining width.
Demo: https://www.codeply.com/go/LBY9LXio09
<nav class="navbar clg navbar-light fixed-top">
<div class="container-fluid">
<a class="navbar-brand" href="https://ngohub.asia">
<img src="//placehold.it/40?text=sm" class="d-lg-none d-inline">
<img src="//placehold.it/100x30?text=lg" class="d-lg-inline d-none">
</a>
<div class="mr-auto flex-shrink-1">
<ul class="navbar-nav flex-row">
<li class="nav-item active">
<a class="nav-link" href="#">Login <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item px-1">
<a class="nav-link">|</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Sign Up </a>
</li>
</ul>
</div>
<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 flex-grow-1" id="navbarSupportedContent">
<ul class="navbar-nav ml-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</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropdown
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#">Disabled</a>
</li>
</ul>
<form class="form-inline d-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
</div>
</div>
</nav>
Center Navbar Brand on mobile devices. I can't seem to get my navbar brand centered on mobile device...I am trying to use media queries and flexbox attributes to no avail....Is this achievable with bootstrap classes or custom css??? Thanks! :)
<nav class="navbar navbar-expand-lg navbar-default fixed-top nav-menu navbar-
light bg-white">
<div class="container">
<div class="header-border">
<a class="navbar-brand hidden-sm-down" href="index.html">
<img src="/images/logo.png" alt="masslogo">
</a>
<a class="navbar-brand-two hidden-md-up" href="index.html">
<img src="/images/mpsmall.png" alt="masssmalllogo">
</a>
</div>
<button class="navbar-toggler nav-button" type="button" data-toggle="collapse" data-target="#myNavbar">
<div class="bg-dark line1"></div>
<div class="bg-dark line2"></div>
<div class="bg-dark line3"></div>
</button>
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="navbar-nav ml-auto">
<li class="nav-item">
Our Solution
</li>
<li class="nav-item">
How We Help
</li>
<li class="nav-item">
Blog
</li>
<li class="nav-item">
Contact
</li>
<li class="nav-item"><div class="dropdown"> <a class="nav-link m-2 dropdown-toggle"
href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true"
aria-expanded="false"> Resources </a> <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
<a class="dropdown-item" href="#">Knowledge base</a> <a class="dropdown-item"
href="#">Video tutorials</a> <a class="dropdown-item"
href="#">Forms</a> </div></div></li>
</ul>
<ul class="navbar-nav navbar-btns">
<a class="search" href="/search"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1024 1024" role="img">
<title>Search</title>
<path d="M914.876 846.934l-226.894-226.92c93.082-124.27 83.508-301.176-29.516-414.14-61.952-61.97-143.146-92.954-224.366-92.954s-162.414 30.984-224.384 92.954c-123.922 123.922-123.922 324.812 0 448.768 61.97 61.934 143.146 92.936 224.366 92.936 66.918 0 133.624-21.368 189.772-63.402l226.952 226.936 64.07-64.178zm-480.794-190.038c-60.536 0-117.486-23.604-160.238-66.414-88.38-88.362-88.38-232.174-.018-320.538 42.828-42.812 99.72-66.398 160.298-66.398 60.536 0 117.486 23.56 160.256 66.398 88.32 88.38 88.32 232.174 0 320.538-42.87 42.81-99.762 66.414-160.298 66.414z">
</path></svg></a><li class="nav-item login"><a class="btn btn-success
btn-cust" href="https://masspay.myisolved.com/UserLogin.aspx?ReturnUrl=%2f"><span class="fa fa-user-circle mr-1"></span>Client Login</a></li></ul>
</div>
</nav>
Use the flex utility classes to align the Navbar components. You'll also need a hidden spacer to push the brand to center on mobile. Also note the display classes have changed from hidden-* to d-*...
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="d-flex flex-grow-1">
<span class="w-100 d-lg-none d-block"><!-- hidden spacer to center brand on mobile --></span>
<a class="navbar-brand d-none d-lg-inline-block" href="index.html">
<img src="//placehold.it/100x30" alt="masslogo">
</a>
<a class="navbar-brand-two mx-auto d-lg-none d-inline-block" href="index.html">
<img src="//placehold.it/40" alt="masssmalllogo">
</a>
<div class="w-100 text-right">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#myNavbar">
<span class="navbar-toggler-icon"></span>
</button>
</div>
</div>
<div class="collapse navbar-collapse flex-grow-1" id="myNavbar">
<ul class="navbar-nav ml-auto flex-nowrap">
<li class="nav-item">
Our Solution
</li>
<li class="nav-item">
How We Help
</li>
<li class="nav-item">
Blog
</li>
<li class="nav-item">
Contact
</li>
<li class="nav-item">
<div class="dropdown"> <a class="nav-link m-2 dropdown-toggle" href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> Resources </a>
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
<a class="dropdown-item" href="#">Knowledge base</a> <a class="dropdown-item" href="#">Video tutorials</a> <a class="dropdown-item" href="#">Forms</a> </div>
</div>
</li>
</ul>
<ul class="navbar-nav navbar-btns">
<a class="search" href="/search">
...
</a>
<li class="nav-item login">
...
</li>
</ul>
</div>
</nav>
https://www.codeply.com/go/w13DoSZyVc
Similar questions:
Center an element in Bootstrap 4 Navbar
Bootstrap 4 Navbar align logo center and toggle icon on the left
This question already has answers here:
Bootstrap NavBar with left, center or right aligned items
(14 answers)
Bootstrap align navbar items to the right
(24 answers)
Closed 4 years ago.
I'm very new to bootstrap so this is probably an easy question for most. I have a navbar, want two nav items and an input field to be on the left side of the navbar. With the two remaining nav items on the right. I have tried messing with the classes to make this happen and using the "float-left" and "float-right" classes but no joy :(
<nav class="navbar fixed-top navbar-expand-lg navbar-dark bg-dark fixed-top">
<div class="container">
<a class="navbar-brand" href="index.php">L'amour</a>
<button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarResponsive">
<ul class="navbar-nav ml-auto">
<li class="nav-item float-left">
<a class="nav-link float-left" href="order.php">Contact Us</a>
</li>
<li class="nav-item float-left">
<a class="nav-link float-left" href="order.php">Testimonial</a>
</li>
<li class="nav-item">
<input class='form-control' type='text' name='search' placeholder='Search for wedding...'>
</li>
<li class="nav-item float-right">
<a class="nav-link float-right" href="order.php">Order Wesbite</a>
</li>
<li class="nav-item dropdown float-right">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownBlog" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Account
</a>
<div class="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdownBlog">
<a class="dropdown-item" href="signIn.php">Login</a>
<a class="dropdown-item" href="sign-up">Sign up</a>
<a class="dropdown-item" href="homeUser.php">Account details</a>
<a class="dropdown-item" href="logout.php">Logout</a>
</div>
</li>
</ul>
</div>
</div>
</nav>
You can use the following solution:
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<nav class="navbar fixed-top navbar-expand-lg navbar-dark bg-dark fixed-top">
<div class="container">
<a class="navbar-brand" href="index.php">L'amour</a>
<button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarResponsive">
<!-- left menu -->
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="order.php">Contact Us</a>
</li>
<li class="nav-item">
<a class="nav-link" href="order.php">Testimonial</a>
</li>
<li class="nav-item">
<input class='form-control' type='text' name='search' placeholder='Search for wedding...'>
</li>
</ul>
<!-- right menu -->
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link" href="order.php">Order Wesbite</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownBlog" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Account
</a>
<div class="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdownBlog">
<a class="dropdown-item" href="signIn.php">Login</a>
<a class="dropdown-item" href="sign-up">Sign up</a>
<a class="dropdown-item" href="homeUser.php">Account details</a>
<a class="dropdown-item" href="logout.php">Logout</a>
</div>
</li>
</ul>
</div>
</div>
</nav>
Separate menu into two ul left and right use class mr-auto for left and my-0 for the right one
<nav class="navbar fixed-top navbar-expand-lg navbar-dark bg-dark fixed-top">
<div class="container">
<a class="navbar-brand" href="index.php">L'amour</a>
<button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarResponsive">
<ul class="navbar-nav mr-auto">
<li class="nav-item float-left">
<a class="nav-link float-left" href="order.php">Contact Us</a>
</li>
<li class="nav-item float-left">
<a class="nav-link float-left" href="order.php">Testimonial</a>
</li>
<li class="nav-item">
<input class='form-control' type='text' name='search' placeholder='Search for wedding...'>
</li>
</ul>
<ul class="navbar-nav my-lg-0">
<li class="nav-item ">
<a class="nav-link float-right" href="order.php">Order Wesbite</a>
</li>
<li class="nav-item dropdown float-right">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownBlog" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Account
</a>
<div class="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdownBlog">
<a class="dropdown-item" href="signIn.php">Login</a>
<a class="dropdown-item" href="sign-up">Sign up</a>
<a class="dropdown-item" href="homeUser.php">Account details</a>
<a class="dropdown-item" href="logout.php">Logout</a>
</div>
</li>
</ul>
</div>
</div>
</nav>
https://jsfiddle.net/raj_mutant/v00oy85h/
To achieve this you should wrap your nav items in different ul
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<nav class="navbar fixed-top navbar-expand-lg navbar-dark bg-dark fixed-top">
<div class="container">
<a class="navbar-brand" href="index.php">L'amour</a>
<button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarResponsive">
<ul class="navbar-nav">
<li class="nav-item float-left">
<a class="nav-link float-left" href="order.php">Contact Us</a>
</li>
<li class="nav-item float-left">
<a class="nav-link float-left" href="order.php">Testimonial</a>
</li>
<li class="nav-item">
<input class="form-control" type="text" name="search" placeholder="Search for wedding...">
</li>
</ul>
<ul class="navbar-nav ml-auto">
<li class="nav-item ">
<a class="nav-link" href="order.php">Order Wesbite</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownBlog" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Account
</a>
<div class="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdownBlog">
<a class="dropdown-item" href="signIn.php">Login</a>
<a class="dropdown-item" href="sign-up">Sign up</a>
<a class="dropdown-item" href="homeUser.php">Account details</a>
<a class="dropdown-item" href="logout.php">Logout</a>
</div>
</li>
</ul>
</div>
</div>
</nav>
Note: View the snippet in full screen
You are forgetting to use clearfix class to parent class that contain float-left or float-right class