How to hide collapsible Bootstrap navbar on click - css

I've created this collapsable navbar using Bootstrap 4 that works nicely, but I would like it to close when the user clicks on a link. Any way to do this? Thanks
html navbar:
<nav class="navbar navbar-toggleable-md fixed-top">
<button id="nav-btn"class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarDiv" aria-expanded="false" aria-label="Toggle navigation">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<div class="container">
<a class="navbar-brand" href="#"><img src="Images/logo.png" width="60px"></a>
<div class="collapse navbar-collapse" id="navbarDiv">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="#home">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#about-us">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#pricing">Pricing</a>
</li>
</ul>
</div>
</div>
</nav>
css for .icon-bar, since Bootstrap 4 doesn't use the icon-bar class.
.navbar-toggler .icon-bar {
margin: 7px;
display: block;
width: 22px;
height: 1px;
background-color: #cccccc;
border-radius: 1px;
}

Update 2021 - Bootstrap 5 (beta)
Use javascript to add a click event listener on the menu items to close the Collapse navbar..
const navLinks = document.querySelectorAll('.nav-item')
const menuToggle = document.getElementById('navbarSupportedContent')
const bsCollapse = new bootstrap.Collapse(menuToggle)
navLinks.forEach((l) => {
l.addEventListener('click', () => { bsCollapse.toggle() })
})
BS5 demo javascript method
Or, Use the data-bs-toggle and data-bs-target data attributes in the markup on each link to toggle the Collapse navbar...
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container">
<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">
<li class="nav-item active">
<a class="nav-link" href="#" data-bs-toggle="collapse" data-bs-target=".navbar-collapse.show">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#" data-bs-toggle="collapse" data-bs-target=".navbar-collapse.show">Link</a>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#" data-bs-toggle="collapse" data-bs-target=".navbar-collapse.show">Disabled</a>
</li>
</ul>
<form class="d-flex my-2 my-lg-0">
<input class="form-control me-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>
BS5 demo data-attributes method
Update 2019 - Bootstrap 4
The navbar has changed, but the "close after click" method is still the same:
BS4 demo jQuery method
BS4 demo data-toggle method
Bootstrap 3 (original answer)
You can add the collapse component to the links like this..
<ul class="navbar-nav mr-auto">
<li class="nav-item active" data-toggle="collapse" data-target=".navbar-collapse.show">
<a class="nav-link" href="#home">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item" data-toggle="collapse" data-target=".navbar-collapse.show">
<a class="nav-link" href="#about-us">About</a>
</li>
<li class="nav-item" data-toggle="collapse" data-target=".navbar-collapse.show">
<a class="nav-link" href="#pricing">Pricing</a>
</li>
</ul>
BS3 demo using 'data-toggle' method
Or, (perhaps a better way) use jQuery like this..
$('.navbar-nav>li>a').on('click', function(){
$('.navbar-collapse').collapse('hide');
});
BS3 demo using jQuery method

I am using ANGULAR and since it gave me problems the routerLink just add the data-toggle and target in the li tag.... or use jquery like "ZimSystem"
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item" data-toggle="collapse" data-target=".navbar-collapse.show">
<a class="nav-link" routerLink="/inicio" routerLinkActive="active" >Inicio</a>
</li>
</ul>
</div>

You can call $.collapse('hide'); with an event handler on the links.
$('.nav-link').on('click',function() {
$('.navbar-collapse').collapse('hide');
});
.navbar-toggler .icon-bar {
margin: 7px;
display: block;
width: 22px;
height: 1px;
background-color: #cccccc;
border-radius: 1px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.6/js/bootstrap.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet"/>
<nav class="navbar navbar-toggleable-md fixed-top">
<button id="nav-btn" class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarDiv" aria-expanded="false" aria-label="Toggle navigation">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<div class="container">
<a class="navbar-brand" href="#"><img src="Images/logo.png" width="60px"></a>
<div class="collapse navbar-collapse" id="navbarDiv">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="#home">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#about-us">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#pricing">Pricing</a>
</li>
</ul>
</div>
</div>
</nav>

With trying out above solutions, I was missing a solution for Dropdown toggles, so here you go! Also opens submenu items.
Maybe you have to tweak it a bit if your toggle class is different.
$('.navbar-nav li a').on('click', function(){
if(!$( this ).hasClass('dropdown-toggle')){
$('.navbar-collapse').collapse('hide');
}
});

this is the solution to close menu when click on anchor then apply this line in list item
data-target="#sidenav-collapse-main" data-toggle="collapse"
the real example that work for me is below
<li class="nav-item" data-target="#sidenav-collapse-main" data-
toggle="collapse" >
<a class="nav-link" routerLinkActive="active" routerLink="/admin/users">
<i class="ni ni-single-02 text-orange"></i> Users
</a>
</li>

The easiest way to do it using only Angular 2/4 template with no coding:
<nav class="navbar navbar-default" aria-expanded="false">
<div class="container-wrapper">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" (click)="isCollapsed = !isCollapsed">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="navbar-collapse collapse no-transition" [attr.aria-expanded]="!isCollapsed" [ngClass]="{collapse: isCollapsed}">
<ul class="nav navbar-nav" (click)="isCollapsed = !isCollapsed">
<li [routerLinkActive]="['active']" [routerLinkActiveOptions]="{exact: true}"><a routerLink="/">Home</a></li>
<li [routerLinkActive]="['active']"><a routerLink="/about">About</a></li>
<li [routerLinkActive]="['active']"><a routerLink="/portfolio">Portfolio</a></li>
<li [routerLinkActive]="['active']"><a routerLink="/contacts">Contacts</a></li>
</ul>
</div>
</div>
</nav>

This code simulates a click on the burguer button to close the navbar by clicking on a link in the menu, keeping the fade out effect. Solution with typescript for angular 7. Avoid routerLink problems.
ToggleNavBar () {
let element: HTMLElement = document.getElementsByClassName( 'navbar-toggler' )[ 0 ] as HTMLElement;
if ( element.getAttribute( 'aria-expanded' ) == 'true' ) {
element.click();
}
}
<li class="nav-item" [routerLinkActive]="['active']">
<a class="nav-link" [routerLink]="['link1']" title="link1" (click)="ToggleNavBar()">link1</a>
</li>

I am using Angular 5 with Boostrap 4. It works for me in this way.
$(document).on('click', '.navbar-nav>li>a, .navbar-brand, .dropdown-menu>a', function (e) {
if ( $(e.target).is('a') && $(e.target).attr('class') != 'nav-link dropdown-toggle' ) {
$('.navbar-collapse').collapse('hide');
}
});
}
<nav class="navbar navbar-expand-lg navbar-dark bg-primary">
<a class="navbar-brand" [routerLink]="['/home']">FbShareTool</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarColor01" aria-controls="navbarColor01" aria-expanded="false" aria-label="Toggle navigation" style="">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarColor01">
<ul class="navbar-nav mr-auto">
<li class="nav-item active" *ngIf="_myAuthService.isAuthenticated()">
<a class="nav-link" [routerLink]="['/dashboard']">Dashboard <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item dropdown" *ngIf="_myAuthService.isAuthenticated()">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Manage
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" [routerLink]="['/fbgroup']">Facebook Group</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#">Fetch Data</a>
</div>
</li>
</ul>
<ul class="navbar-nav navbar-right navbar-right-link">
<li class="nav-item" *ngIf="!_myAuthService.isAuthenticated()" >
<a class="nav-link" (click)="logIn()">Login</a>
</li>
<li class="nav-item" *ngIf="_myAuthService.isAuthenticated()">
<a class="nav-link">{{ _myAuthService.userDetails.displayName }}</a>
</li>
<li class="nav-item" *ngIf="_myAuthService.isAuthenticated() && _myAuthService.userDetails.photoURL">
<a>
<img [src]="_myAuthService.userDetails.photoURL" alt="profile-photo" class="img-fluid rounded" width="40px;">
</a>
</li>
<li class="nav-item" *ngIf="_myAuthService.isAuthenticated()">
<a class="nav-link" (click)="logOut()">Logout</a>
</li>
</ul>
</div>
</nav>

In my case the menu was always in the shown state after using the bootstrap 5 js solution of Zim.
The problem was resolved after adding toggle: false to the Collapse object constructor as also shown in one example of the specification.
Modified code of Zen:
const navLinks = document.querySelectorAll('.nav-item')
const menuToggle = document.getElementById('navbarSupportedContent')
const bsCollapse = new bootstrap.Collapse(menuToggle, {
toggle: false
})
navLinks.forEach((l) => {
l.addEventListener('click', () => { bsCollapse.toggle() })
})
(I could not comment, so I created a separate answer)

I'm working with Bootstrap 5.1.3 in React and Redux-Toolkit. By adding a simple span tag like this to my Link (Route) component, like this: <span data-bs-target="#navbarNav" data-bs-toggle="collapse">Home</span>, allows the bootstrap collapse and link route to work seamlessly.
*If you see a blinking effect, you can fix by adding bootstrap screen size display tags e.g.:
<Link to={'/home'} className="nav-link d-sm-none"><span data-bs-target="#navbarNav" data-bs-toggle="collapse">Home</span></Link>
<Link to={'/home'} className="nav-link d-none d-sm-block">Home</Link>
*No extra JS or Jquery required
*It should work with most applications using Bootstrap 5.0
<nav className="navbar navbar-expand-lg navbar-dark bg-dark navbar-expand-sm fixed-top">
<div className="container">
<button className="navbar-toggler" type="button"
data-bs-toggle="collapse" data-bs-target="#navbarNav"
aria-controls="navbarNav" aria-expanded="false"
aria-label="Toggle navigation">
<span className="navbar-toggler-icon"></span>
</button>
<div className="collapse navbar-collapse" id="navbarNav">
<ul className="navbar-nav mt-2 mt-sm-0">
<li className="nav-item">
// 2 tags below for different screen sizes
<Link to={'/home'} className="nav-link d-sm-none"><span data-bs-target="#navbarNav" data-bs-toggle="collapse">Home</span></Link>
<Link to={'/home'} className="nav-link d-none d-sm-block">Home</Link>
</li>
<li className="nav-item">
// 2 tags below for different screen sizes
<Link to={'/counter'} className="nav-link d-sm-none"><span data-bs-target="#navbarNav" data-bs-toggle="collapse">Counter</span></Link>
<Link to={'/counter'} className="nav-link d-none d-sm-block">Counter</Link>
</li>
</ul>
</div>
</div>
</nav>

None of the answers above was helpful since I use React Bootstrap. In this case, I had to create a state which stored whether the navbar was collapsed or expanded.
The state gets updated whenever I toggle/click a link.
The toggle happens if I click the toggle menu itself.
The closure happens whenever I click a link.
Here is a code snippet that does the same:
function Navigation() {
const [expanded, setExpanded] = useState(false);
const navToggle = () => {
setExpanded(expanded ? false : true);
};
const closeNav = () => {
setExpanded(false);
};
return (
<React.Fragment>
<Navbar expand="lg" expanded={expanded}>
<Container>
<Navbar.Brand as={Link} to="/" onClick={closeNav}>
Site Header
</Navbar.Brand>
<Navbar.Toggle aria-controls="basic-navbar-nav" onClick={navToggle} />
<Navbar.Collapse id="basic-navbar-nav">
<Nav className="me-auto">
<Nav.Link to="/link1" onClick={closeNav}>
Link 1
</Nav.Link>
<Nav.Link to="/link2" onClick={closeNav}>
Link 2
</Nav.Link>
<Nav.Link to="/link3" onClick={closeNav}>
Link 3
</Nav.Link>
</Nav>
</Navbar.Collapse>
</Container>
</Navbar>
</React.Fragment>
);
}

I was searching for a solution with Bootstrap 5 to hide a popup menu when a new partial view was diplayed, I did it like this but duno if I am right.
// clear menu dropdowns
const navLinks = document.querySelectorAll('.nav-item')
const menuToggle = document.getElementById('megaMenuDropdown')
const bsCollapse = new bootstrap.Collapse(menuToggle)
navLinks.forEach(() => bsCollapse.hide());

The other answers didn't work for me. I use typescript with Bootstrap 5 and React.
What I do is first getting the hamburger button and then calling 'click' on it:
var btn = document.getElementsByClassName("navbar-toggler") as HTMLCollectionOf<HTMLElement>;
btn[0].click();
Full example:
<Navbar expand="lg" className="nav iq-navbar">
<Container fluid className="navbar-inner">
<Navbar.Toggle aria-controls="navbarSupportedContent">
<span className="navbar-toggler-icon">
<span className="navbar-toggler-bar bar1 mt-2"></span>
<span className="navbar-toggler-bar bar2"></span>
<span className="navbar-toggler-bar bar3"></span>
</span>
</Navbar.Toggle>
<Navbar.Brand as="div" className="mb-2 ms-auto navbar-list mb-lg-0">
<img src={logo} alt="logo" className="img-fluid p-0 flex-column" />
</Navbar.Brand>
<Navbar.Collapse className="col-md-2 ms-auto align-items-start" id="navbarSupportedContent">
<ul className="navbar-nav ms-auto">
<li className="item-name">
<Link className={`${location.pathname === '/dashboard' ? 'active' : ''} nav-link d-sm-none h1`} aria-current="page" to="/dashboard" onClick={() => { var btn = document.getElementsByClassName("navbar-toggler") as HTMLCollectionOf<HTMLElement>; btn[0].click(); }}>D </Link>
<Link className={`${location.pathname === '/dashboard' ? 'active' : ''} nav-link d-none d-sm-block`} aria-current="page" to="/dashboard" onClick={() => { var btn = document.getElementsByClassName("navbar-toggler") as HTMLCollectionOf<HTMLElement>; btn[0].click(); }}>Dashboard</Link>
</li>
</ul>
</Navbar.Collapse>
</Container>
</Navbar>

You can use a simply bind on click and close, like this: (click)="drawer.close()
<a class="nav-link" [routerLink]="navItem.link" routerLinkActive="selected" (click)="drawer.close()">

add below code in your < a > TAG
data-toggle="collapse" data-target=".navbar-collapse.show"
as shows below in every TAG
<li class="nav-item">
<a class="nav-link" href="#about-us" data-toggle="collapse" data-target=".navbar-collapse.show">About</a>
</li>

I have modified the answer of Zim.
Now the code closes the navbar only in case it is open.
const navLinks = document.querySelectorAll('.nav-item')
const menuToggle = document.getElementById('navbarSupportedContent')
const bsCollapse = new bootstrap.Collapse(menuToggle, {
toggle: false
})
navLinks.forEach((l) => {
l.addEventListener('click', () => {
if(bsCollapse._isShown()){
bsCollapse.hide()
}
})
})

Related

Bootstrap 5 navbar and fixed-top position

I am trying to use navbar with fixed-top class so that the menu will not pull down the element of the page when it is collapsed. But when I use the fixed-top class, the navbar will go over the container (in width) as if it was inside a container-fluid while i use a container class to have the menu and the site in the middle of the page (not a full width). Is there a way to have a navbar with fixed-top but not at full width?
This is the code:
<div class="container" style="border: 1px solid;">
<nav class="navbar fixed-top navbar-expand-lg">
<div class="container-fluid">
<a class="navbar-brand" href="#"><img src="n-img/logo.png" class="img-responsive"></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 ms-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link" aria-current="page" href="n-index.php">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Chi Siamo</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Dove Siamo</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Contatti</a>
</li>
</ul>
</div>
</div>
</nav>
</div> <!-- end container -->
When using position:fixed the element is removed from the normal DOM flow so it no longer relates to other page elements including its parent container.
If you want limit the navbar to the container width, use the container inside the navbar to wrap the navbar content...
<nav class="navbar fixed-top navbar-expand-lg">
<div class="container">
<a class="navbar-brand" href="#"><img src="n-img/logo.png" class="img-responsive"></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 ms-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link" aria-current="page" href="n-index.php">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Chi Siamo</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Dove Siamo</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Contatti</a>
</li>
</ul>
</div>
</div>
</nav>
https://codeply.com/p/AFQj4wf6DD

How to align a bootstrap navbar toggle button to the left or right of the brand name?

I am using a bootstrap navbar in my react app and the code is as follows -
<nav className="navbar navbar-default navbar-expand-sm navbar-light bg-light">
<div className="container-fluid">
<div className="navbar-header">
<button
className="navbar-toggler"
type="button"
data-toggle="collapse"
data-target="#navbarTogglerDemo03"
aria-controls="navbarTogglerDemo03"
aria-expanded="false"
aria-label="Toggle navigation"
>
<span className="navbar-toggler-icon"></span>
</button>
<NavLink className="navbar-brand mx-auto" to="/">
<h4>Crafting Social Minds</h4>
</NavLink>
</div>
<div
className="collapse navbar-collapse me-auto"
id="navbarTogglerDemo03"
>
<ul className="navbar-nav ms-auto navbar-custom">
<li className="nav-item">
<NavLink
className="nav-link"
exact
to="/"
>
Organised Events
</NavLink>
</li>
<li className="nav-item">
<NavLink
className="nav-link"
to="/upcoming-events"
>
Upcoming Events
</NavLink>
</li>
<li className="nav-item">
<NavLink
className="nav-link"
to="/new-event"
>
New Event
</NavLink>
</li>
<li className="nav-item">
<NavLink
className="nav-link"
to="/about"
>
About Us
</NavLink>
</li>
</ul>
{/* <div className="social-part">
<i className="fa fa-facebook" aria-hidden="true"></i>
<i className="fa fa-twitter" aria-hidden="true"></i>
<i className="fa fa-instagram" aria-hidden="true"></i>
</div> */}
</div>
</div>
</nav>
For large and medium devices the navbar looks like this -
and for small devices, the same navbar looks as follows-
So, for large(and medium) devices the navbar looks as I was intended, but in the case of small devices, the toggle-navbar button appears above the brand name whereas I want it to appear on the left or on the right of the brand name. Is there any way to fix this without changing the view for large devices using only bootstrap classes?
You’re using me-auto and ms-auto (margin end and margin start), but those are for Bootstrap 5. Bootstrap 4 uses mr-auto and ml-auto (the tag on your post says bootstrap-4).
If you want the button on the left and your brand name on the right, you should override the normal margin with mr-0 mr-md-3, although with the h4 styling on your name, it just fits on a 320px wide display.
<link href="https://cdn.jsdelivr.net/npm/bootstrap#4.6.0/dist/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap#4.6.0/dist/js/bootstrap.bundle.min.js"></script>
<nav class="navbar navbar-expand-md navbar-light bg-light">
<div class="container-fluid">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo03" aria-controls="navbarTogglerDemo03" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<a class="navbar-brand mr-0 mr-md-3" href="#">
<h4>Crafting Social Minds</h4>
</a>
<div class="collapse navbar-collapse mr-auto" id="navbarTogglerDemo03">
<ul class="navbar-nav ml-auto">
<li class="nav-item active">
<a class="nav-link" href="#">Organised Events <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Upcoming Events</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">New Event</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">About Us</a>
</li>
</ul>
</div>
</div>
</nav>

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>

Navbar to get horizontal layout

I have the following code for a navbar in a React app. The navbar content looks fine when the browser window is wide, but once I drop past the navbar-expand-lg cutoff, the icons align vertically one above the other. How do I prevent that? (see screenshot below code)
import React, {Component} from 'react';
import {
Badge,
Nav,
Navbar,
NavbarBrand,
NavbarToggler,
NavItem,
NavLink,
} from 'reactstrap';
import FontAwesome from '#fortawesome/react-fontawesome';
class Header extends Component {
constructor(props) {
super(props);
}
render() {
return (
<Navbar dark color="dark" expand="lg" className="w-100 p-1">
<NavbarToggler className="d-flex"/>
<NavbarBrand className="ml-3" href="/">Logo</NavbarBrand>
<Nav className="d-flex ml-auto" horizontal="end" navbar>
<NavItem>
<NavLink href="#">
<FontAwesome icon={["fas","bell"]} className="text-light" size="lg" />
<Badge pill color="danger">5</Badge>
</NavLink>
</NavItem>
<NavItem>
<NavLink href="#">
<FontAwesome icon={["fas","clipboard"]} className="text-light" size="lg" />
<Badge pill color="warning">5</Badge>
</NavLink>
</NavItem>
<NavItem>
<NavLink href="#">
<i className="icon-location-pin"></i>
<Badge pill color="info">5</Badge>
</NavLink>
</NavItem>
</Nav>
<NavbarToggler className="d-flex ml-3"/>
</Navbar>
);
}
}
export default Header;
To achieve what you want, you need to use the flex-row class to turn what normally would be a flex column into a flex row.
Here is the complete working code for your navbar including both hamburger menus (I have hidden the right menu items on lg screens with the d-lg-none class):
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<button class="navbar-toggler mr-3" 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>
<a class="navbar-brand" href="#">Navbar</a>
<div class="ml-auto order-lg-last">
<ul class="navbar-nav flex-row">
<li class="nav-item pr-3 pr-lg-0">
<a class="nav-link" href="#">
<i class="fa fa-bell" aria-hidden="true"></i>
<span class="badge badge-pill badge-danger">5</span>
</a>
</li>
<li class="nav-item pr-3 pr-lg-0">
<a class="nav-link" href="#">
<i class="fa fa-envelope" aria-hidden="true"></i>
<span class="badge badge-pill badge-warning">6</span>
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">
<i class="fa fa-telegram" aria-hidden="true"></i>
<span class="badge badge-pill badge-primary">8</span>
</a>
</li>
</ul>
</div>
<button class="navbar-toggler ml-3" type="button" data-toggle="collapse" data-target="#navbarSupportedContent2" aria-controls="navbarSupportedContent2" aria-expanded="false" aria-label="Toggle navigation2">
<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="#">Left Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Left Link 1</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Left Link 2</a>
</li>
</ul>
</div>
<div class="collapse navbar-collapse" id="navbarSupportedContent2">
<ul class="navbar-nav mr-auto d-lg-none">
<li class="nav-item active">
<a class="nav-link" href="#">Right Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Right Link 1</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Right Link 2</a>
</li>
</ul>
</div>
</nav>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
The padding on the right for the icons is adjusted the padding-right classes pr-3 pr-lg-0 i.e. zero or default padding on lg screens and up and 3 units right padding on smaller screens.
Also note how the order-lg-last class switches things around on lg screens and above.
Just replace navbar-expand-lg with navbar-expand

Bootstrap align navbar items to the right

How do I align a navbar item to right?
I want to have the login and register to the right.
But everything I try does not seem to work.
This is what I have tried so far:
<div> around the <ul> with the atribute: style="float: right"
<div> around the <ul> with the atribute: style="text-align: right"
tried those two things on the <li> tags
tried all those things again with !important added to the end
changed nav-item to nav-right in the <li>
added a pull-sm-right class to the <li>
added a align-content-end class to the <li>
This is my code:
<div id="app" class="container">
<nav class="navbar navbar-toggleable-md navbar-light bg-faded">
<button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<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="#">Features</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Pricingg</a>
</li>
</ul>
<ul class="navbar-nav " >
<li class="nav-item">
<a class="nav-link" href="{{ url('/login') }}">Login</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{{ url('/register') }}">Register</a>
</li>
</ul>
</nav>
#yield('content')
</div>
Bootstrap 5 (update 2021)
In Bootstrap 5 (see this question), ml-auto has been replaced with ms-auto to represent start instead of left. Since the Navbar is still based on flexbox, auto margins OR flexbox utility classes are still used to align Navbar content.
For example, use me-auto...
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="#">Brand</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link active" href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
</ul>
<ul class="navbar-nav">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false"> Menu </a>
<ul class="dropdown-menu dropdown-menu-end" aria-labelledby="navbarDropdown">
<li><a class="dropdown-item" href="#">Action</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
</ul>
</li>
</ul>
</div>
</div>
</nav>
Bootstrap 5 right align Navbar content
Bootstrap 4 (original answer)
Bootstrap has many different ways to align navbar items. float-right won't work because the navbar is now flexbox.
You can use mr-auto for auto right margin on the 1st (left) navbar-nav.
Alternatively, ml-auto could be used on the 2nd (right) navbar-nav , or if you just have a single navbar-nav.
<nav class="navbar navbar-expand-md navbar-light bg-light">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<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="#">Features</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Pricing</a>
</li>
</ul>
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="#">Login</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Register</a>
</li>
</ul>
</div>
</nav>
https://codeply.com/go/P0G393rzfm
There are also flexbox utils. For example use justify-content-end on the collapse menu:
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<div class="container-fluid">
<a class="navbar-brand" href="#">Brand</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse justify-content-end" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">Contact</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Pricing</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Download</a>
</li>
</ul>
</div>
</div>
</nav>
Or when you have 2 navbar-navs, use justify-content-between in navbar-collapse would work to even the space between the navbar-navs:
<div class="navbar-collapse collapse justify-content-between">
<ul class="navbar-nav mr-auto">
..
</ul>
<ul class="navbar-nav">
..
</ul>
</div>
Update for Bootstrap 4.0 and newer
As of Bootstrap 4 beta, ml-auto will still work to push items to the right. Just be aware the the navbar-toggleable- classes have changed to navbar-expand-*
Updated navbar right for Bootstrap 4
Another frequent Bootstrap 4 Navbar right alignment scenario includes a button on the right that remains outside the mobile collapse nav so that it is always shown at all widths.
Right align button that is always visible
Related: Bootstrap NavBar with left, center or right aligned items
In my case, I wanted just one set of navigation buttons / options and found that this will work:
<div class="collapse navbar-collapse justify-content-end" id="navbarCollapse">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="#">Sign Out</a>
</li>
</ul>
</div>
So, you will add justify-content-end to the div and omit mr-auto on the list.
Here is a working example.
For those who is still struggling with this issue in BS4 simply try below code -
<ul class="navbar-nav ml-auto">
On Bootstrap 4
If you want to align brand to your left and all the navbar-items to right, change the default mr-auto to ml-auto
<ul class="navbar-nav ml-auto">
On Bootsrap 4.0.0-beta.2, none of the answers listed here worked for me. Finally, the Bootstrap site gave me the solution, not via its doc but via its page source code...
Getbootstrap.com align their right navbar-nav to the right with the help of the following class: ml-md-auto.
Use this code to move items to the right.
<div class="collapse navbar-collapse justify-content-end">
Use ml-auto instead of mr-auto after applying nav justify-content-end to the ul
Just add mr-auto class at ul:
<ul class="nav navbar-nav mr-auto">
If you have menu list in both side you can do something like this:
<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">
<a class="nav-link disabled" href="#">Disabled</a>
</li>
</ul>
<ul class="navbar-nav ml-auto">
<li class="nav-item active">
<a class="nav-link" href="#">left 1</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">left 2</a>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#">left disable</a>
</li>
</ul>
If you want Home, Features and Pricing on left immediately after your nav-brand, and then login and register on right then wrap the two lists in <div> and use .justify-content-between:
<div class="collapse navbar-collapse justify-content-between">
<ul>....</ul>
<ul>...</ul>
</div>
In bootstrap v4.3 just add ml-auto in <ul class="navbar-nav">
Ex:<ul class="navbar-nav ml-auto">
use the flex-row-reverse class
<nav class="navbar navbar-toggleable-md navbar-light">
<div class="container">
<button class="navbar-toggler navbar-toggler-right" 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>
<a class="navbar-brand" href="#">
<i class="fa fa-hospital-o fa-2x" aria-hidden="true"></i>
</a>
<div class="collapse navbar-collapse flex-row-reverse" id="navbarNavAltMarkup">
<ul class="navbar-nav">
<li><a class="nav-item nav-link active" href="#" style="background-color:#666">Home <span class="sr-only">(current)</span></a</li>
<li><a class="nav-item nav-link" href="#">Doctors</a></li>
<li><a class="nav-item nav-link" href="#">Specialists</a></li>
<li><a class="nav-item nav-link" href="#">About</a></li>
</ul>
</div>
</div>
</nav>
It's little change in boostrap 4.
To align navbar to right side, you've to make only two changes.
they are:
in navbar-nav class add w-100 as navbar-nav w-100 to make width as 100
in nav-item dropdown class add ml-auto as nav-item dropdown ml-auto to make margin left as auto.
If you didn't understand, please refer the image that i've attached to this.
CodePen Link
Full source code
<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="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNavDropdown">
<ul class="navbar-nav w-100">
<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="#">Features</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Pricing</a>
</li>
<li class="nav-item dropdown ml-auto">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropdown link
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</li>
</ul>
</div>
</nav>
For bootstrap 4.3.1, I was using nav-pills and nothing worked for me except this:
<ul class="nav nav-pills justify-content-end ml-auto">
<li ....</li>
</ul>
In my case Bootstrap v5, I wanted just one set of navigation options to the right side:
just add "ms-auto" in ul.
And it helped me.
<div class="collapse navbar-collapse" id="navbarCollapse">
<ul class="navbar-nav ms-auto mb-2 mb-md-0">
<li class="nav-item">
<a class="nav-link " aria-current="page" href="index.php">Home</a>
</li>
<li class="nav-item">
<a class="nav-link " href="services.php" tabindex="-1" aria-disabled="true">Services</a>
</li>
<li class="nav-item">
<a class="nav-link" href="aboutus.php">About Us</a>
</li>
<li class="nav-item">
<a class="nav-link" href="Portfolio.php">Portfolio</a>
</li>
</ul>
</div>
The above answers surprisingly didn't work for me so here is my approach. To align the login and register navigation bar items to the right you can make use of either of these two ways:
pull-right:
Modify the unordered list tag for login and register as
<ul class="navbar-nav pull-right">
navbar-right:
Modify the unordered list tag for login and register as
<ul class="navbar-nav navbar-right">
I am running Angular 4 (v.4.0.0) and ng-bootstrap (Bootstrap 4). This code won't all be relevant but hoping people can pick and choose what works. It took me sometime to find a solution to get my items to justify right, collapse properly and to implement a dropdown off my google (using OAuth) profile picture.
<div id="header" class="header">
<nav class="navbar navbar-toggleable-sm navbar-inverse bg-faded fixed-top">
<button class="navbar-toggler navbar-toggler-right" 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>
<a class="navbar-brand" href="#">
<img alt='Brand' src='../assets/images/logo-white.png' class='navbar-logo-img d-inline-block align-top '>
<span class="navbar-logo-text">Oncoscape</span>
</a>
<div class="collapse navbar-collapse justify-content-end" id="navbarSupportedContent">
<ul class="navbar-nav float-left">
<a class="navbar-items nav-item nav-link active " *ngIf='authenticated' (click)='goDashboard()'>
<span class="fa fa-dashboard"></span>Dashboard
</a>
<a class="nav-item nav-link navbar-items active" href="http://resources.sttrcancer.org/oncoscape-contact">
<span class="fa fa-comments"></span>Feedback
</a>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<img *ngIf='user && authenticated' class="navbar-pic" src={{user.thumbnail}} alt="Smiley face">
</a>
<div *ngIf='user && authenticated' class="dropdown-menu " aria-labelledby="navbarDropdownMenuLink">
<a class="dropdown-item" (click)="toProfile()">Account</a>
<div class="dropdown-item">
<app-login></app-login>
</div>
</div>
</li>
</ul>
</div>
</nav>
</div>
<router-outlet></router-outlet>
For Bootstrap 4 beta, sample navbar with elements aligned to the right side is:
<div id="app" class="container">
<nav class="navbar navbar-expand-md navbar-light bg-faded">
<button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<a class="navbar-brand" href="#">Navbar</a>
<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="#">Features</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Pricingg</a>
</li>
</ul>
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="{{ url('/login') }}">Login</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{{ url('/register') }}">Register</a>
</li>
</ul>
</nav>
</div>
Using the bootstrap flex box helps
us to control the placement and alignment of your navigation element.
for the problem above adding mr-auto is a better solution to it .
<div id="app" class="container">
<nav class="navbar navbar-toggleable-md navbar-light bg-faded">
<button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<a class="navbar-brand" href="#">Navbar</a>
<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="#">Features</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Pricingg</a>
</li>
</ul>
<ul class="navbar-nav " >
<li class="nav-item">
<a class="nav-link" href="{{ url('/login') }}">Login</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{{ url('/register') }}">Register</a>
</li>
</ul>
</nav>
#yield('content')
</div>
other placement may include
fixed- top
fixed bottom
sticky-top
The working example for BS v4.0.0-beta.2:
<body>
<nav class="navbar navbar-expand-md navbar-dark bg-dark">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNavDropdown">
<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="#">Features</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Pricingg</a>
</li>
</ul>
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="#">Login</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Register</a>
</li>
</ul>
</div>
</nav>
<div class="container-fluid">
container content
</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="node_modules/jquery/dist/jquery.slim.min.js"></script>
<script src="node_modules/popper.js/dist/umd/popper.min.js"></script>
<script src="node_modules/bootstrap/dist/js/bootstrap.min.js"></script>
</body>
If all above fails, I added 100% width to the navbar class in CSS. Until then mr auto wasn't working for me on this project using 4.1.
but the nav item you want to align to right in alone
put it outside the of main nav but in the same , like that:
<ul class="navbar-nav justify-content-end ms-auto">
<li class="nav-item">
<a class="nav-link" href="#" aria-expanded="false">Contact support</a>
</li>
<li class="nav-item ">
<a class="nav-link" href="#" aria-expanded="false">Log in</a>
</li>
</ul>
use justify-content-end ms-auto
Find the 69 line in the verndor-prefixes.less and write it following:
.panel {
margin-bottom: 20px;
height: 100px;
background-color: #fff;
border: 1px solid transparent;
border-radius: 4px;
-webkit-box-shadow: 0 1px 1px rgba(0,0,0,.05);
box-shadow: 0 1px 1px rgba(0,0,0,.05);
}
Just copied this from one of the getbootstrap pages for the released version 4 which worked much better than the above
<div class="d-none d-xl-block col-xl-2 bd-toc float-md-right">
<ul class="section-nav">
<li class="toc-entry toc-h2">Overview</li>
<li class="toc-entry toc-h2">Classes</li>
<li class="toc-entry toc-h2">Mixins</li>
<li class="toc-entry toc-h2">Responsive</li>
</ul>
</div>
I'm new to stack overflow and new to front end development. This is what worked for me. So I did not want list items to be displayed.
.hidden {
display:none;
}
#loginButton{
margin-right:2px;
}
<nav class="navbar navbar-toggleable-md navbar-light bg-faded fixed-top">
<button class="navbar-toggler navbar-toggler-right" 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>
<a class="navbar-brand" href="#">NavBar</a>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item active hidden">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item hidden">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item hidden">
<a class="nav-link disabled" href="#">Disabled</a>
</li>
</ul>
<form class="form-inline my-2 my-lg-0">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit" id="loginButton">Log In</button>
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Register</button>
</form>
</div>
</nav>

Resources