How do I add text to navbar in Bootstrap 4? - html

I'm trying to display the username of the logged in user in my application in Bootstrap 4 navbar. However, it's displayed out of vertical alignment with other elements.
Here's the example:
<nav class="navbar navbar-light bg-faded">
<a class="navbar-brand" href="#">Brand Name</a>
<ul class="nav navbar-nav">
<li class="nav-item">
<a class="nav-link" href="#">Foo</a>
</li>
</ul>
<ul class="nav navbar-nav pull-xs-right">
<li class="nav-item">
<span>email#example.com</span>
</li>
</ul>
</nav>
The resulted E-Mail address is displayed top-right, however I want it to be displayed middle-right.

Add the class .nav-link to the span and then style the color of it accordingly like so:
Here is a fiddle Fiddle Demo
.navbar-light .navbar-nav span.nav-link,
.navbar-light .navbar-nav span.nav-link:hover,
.navbar-light .navbar-nav span.nav-link:focus{
color:rgba(0,0,0,0.8);
}
It should look like the following:
<nav class="navbar navbar-light bg-faded">
<a class="navbar-brand" href="#">Brand Name</a>
<ul class="nav navbar-nav">
<li class="nav-item">
<a class="nav-link" href="#">Foo</a>
</li>
</ul>
<ul class="nav navbar-nav pull-xs-right">
<li class="nav-item">
<span class="nav-link">email#example.com</span>
</li>
</ul>
</nav>
Or you can keep your markup and style the span youself whichever you like
.navbar-nav span {
display: block;
padding-top: .425rem;
padding-bottom: .425rem;
}

You have to remove the class pull-xs-right so :
<ul class="nav navbar-nav pull-xs-right">
<li class="nav-item">
<span>email#example.com</span>
</li>
</ul>
Should be :
<ul class="nav navbar-nav">
<li class="nav-item">
<span>email#example.com</span>
</li>
</ul>
Or use one ul tag like :
<nav class="navbar navbar-light bg-faded">
<a class="navbar-brand" href="#">Brand Name</a>
<ul class="nav navbar-nav">
<li class="nav-item">
<a class="nav-link" href="#">Foo</a>
</li>
<li class="nav-item">
<span>email#example.com</span>
</li>
</ul>
</nav>
Hope this helps.

Try navbar-text to li like this
<nav class="navbar navbar-light bg-faded">
<a class="navbar-brand" href="#">Brand Name</a>
<ul class="nav navbar-nav">
<li class="nav-item">
<a class="nav-link" href="#">Foo</a>
</li>
</ul>
<ul class="nav navbar-nav pull-xs-right">
<li class=" nav-item navbar-text">
<span>email#example.com</span>
</li>
</ul>
</nav>
see example here

Related

cannot make :hover in nav

I'm trying to male that when you hover a "home" it will be a different color. Could someone please help me? Thank you!
<header class = "header">
<nav class="navbar navbar-expand-lg navbar-fixed-top navbar-light bg-light;">
<div class="container-fluid">
<div class="collapse navbar-collapse justify-content-between" id="navbarSupportedContent">
<ul class="navbar-nav ml-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link" href="#" style="color:#b38b40;">Home</a>
</li>
</ul>
</div>
</div>
You need to move your inline style="" attributes to a separate stylesheet .css or <style> element because you cannot define :hover rules in inline style="" attributes and inline styles takes precedence over rules from a stylesheet.
header.header > nav ul > li > a {
color: #b38b40;
}
header.header > nav ul > li > a:hover {
color: red;
}
<header class = "header">
<nav class="navbar navbar-expand-lg navbar-fixed-top navbar-light bg-light;">
<div class="container-fluid">
<div class="collapse navbar-collapse justify-content-between" id="navbarSupportedContent">
<ul class="navbar-nav ml-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link" href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Away</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Life</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Is Short</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">And Love</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Is Always Over</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">In the Morning</a>
</li>
</ul>
</div>
</div>
You have 3 options,
create a new file - only for css, and put a link to the File from the HTML page.
write in the right row, style = "your style".
On the HTML page, write the label style

Navigation bar not stacking properly

In the extra-small size screen, I want to have the navbar-brand and navbar-nav vertically stacked.
<nav class="navbar navbar-dark navbar-expand-sm bg-primary fixed-top">
<div class="container">
<a class="navbar-brand" href="#">Ristorante Con Fusion</a>
<ul class="navbar-nav mr-auto">
<li class="nav-item active"><a class="nav-link" href="#">Home</a></li>
<li class="nav-item"><a class="nav-link" href="./aboutus.html">About</a></li>
<li class="nav-item"><a class="nav-link" href="#">Menu</a></li>
<li class="nav-item"><a class="nav-link" href="#">Contact</a></li>
</ul>
</div>
</nav>
But for some reason, it looks like this
How can I have the navbar-nav under the navbar-brand in an extra-small size screen?
Do you want the nav items to stack vertically too? I think you're looking for this...
<nav class="navbar navbar-dark navbar-expand bg-primary fixed-top">
<div class="container flex-column flex-sm-row">
<a class="navbar-brand" href="#">Ristorante Con Fusion</a>
<ul class="navbar-nav mr-sm-auto mr-0">
<li class="nav-item active"><a class="nav-link" href="#">Home</a></li>
<li class="nav-item"><a class="nav-link" href="./aboutus.html">About</a></li>
<li class="nav-item"><a class="nav-link" href="#">Menu</a></li>
<li class="nav-item"><a class="nav-link" href="#">Contact</a></li>
</ul>
</div>
</nav>
https://codeply.com/p/UNjoLoEnSn
Use .collapse class and button toggler.
UPDATE
Just add flex-column flex-lg-row classes in your nav tag.
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<nav class="navbar navbar-expand-lg navbar-light bg-light flex-column flex-lg-row">
<a class="navbar-brand" href="#">Navbar</a>
<ul class="navbar-nav mr-auto">
<li class="nav-item active"><a class="nav-link" href="#">Home</a></li>
<li class="nav-item"><a class="nav-link" href="./aboutus.html">About</a></li>
<li class="nav-item"><a class="nav-link" href="#">Menu</a></li>
<li class="nav-item"><a class="nav-link" href="#">Contact</a></li>
</ul>
</nav>
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js#1.16.0/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>

How to make horizontal navigation bar using Bootstrap?

I have this simple code in HTML using Bootstrap classes to create a navigation bar. I want my navigation bar to be horizontal so I added the navbar-inverse class. However, my navigation bar keeps showing vertical and I can't figure out why it does that even thought I tried different things to fix it.
<div class="blog-masthead">
<div class="container">
<nav class="navbar navbar-inverse">
<ul class="nav navbar-nav">
<li class="nav-item"><a class="blog-nav-item active" href="#">Home</a></li>
<li class="nav-item"><a class="blog-nav-item" href="#">New features</a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li class="nav-item"><a class="blog-nav-item" href="#">{{ Auth::user()->name }}</a></li>
</ul>
</nav>
</div>
</div>
I hope navbar-inverse is nothing to do with menu view style I hope it's used to change the color of the menu text and for the view style you should use nav-link in a tag and nav-toggleable in nav tag I hope it's the answer.
I'm sorry if I misunderstood your question
<ul class="nav navbar-nav ">
<li class="nav-item">
<a class="nav-link active" href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link " href="#">New features</a>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li class="nav-item"><a class="nav-item" href="#">{{ Auth::user()->name }}</a></li>
</ul>
</nav>
</div>

Dropdown misplaced in navbar bootstrap4

A newbie to bootstrap,I have a dropdown in a navbar in bootstrap 4.
Here is my code:
<nav class="navbar navbar-toggleable-md fixed-top navbar-inverse bg-inverse " >
<button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#nav-items">
<span class="navbar-toggler-icon"></span>
</button>
Flicker
<div class="collapse navbar-collapse" id="nav-items" >
<ul class="navbar-nav mr-auto">
<li class="nav-item active" href="#">
Home
</li>
<li class="nav-item">
Baidu
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#">Disbabled</a>
</li>
<li class="nav-item">
DropDown<span class="caret"></span>
<ul class="dropdown-menu bg-inverse">
<li class="dropdown-item">Hello </li>
<li class="dropdown-item">Yeah </li>
</ul>
</li>
<form class="form-inline ">
<input class="form-control mr-sm-2" type="text" placeholder="Input">
<button class="btn btn-outline-success">Submit</button>
</form>
</ul>
</div>
</nav>
It turns out like this:
The dropdown is misplaced.What's going wrong? And how to fix it? Any help will be appreciated.
you need to add the class dropdown to your nav-item div
<li class="nav-item dropdown">
DropDown<span class="caret"></span>
<ul class="dropdown-menu bg-inverse">
<li class="dropdown-item">Hello </li>
<li class="dropdown-item">Yeah </li>
</ul>
</li>

How do I add plain text to a Bootstrap 4 navbar?

This is probably very simple, but the answer seems to be eluding me. I have tried several combinations using navbar-text, but most of them end up putting the text either above or below another navbar item. I would like the text to be located just to the left of the "Sign In" link.
Here is what I have now:
<nav class="navbar navbar-fixed-top navbar-dark bg-inverse">
<a class="navbar-brand" href="#">Sample App</a>
<ul class="nav 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="#">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Contact</a>
</li>
</ul>
<div class="nav navbar-nav pull-md-right">
<div class="nav nav-item">
<p class="navbar-text">Signed in as User#1234</p>
</div>
<ul class="nav navbar-nav">
<li class="nav-item">
<a class="nav-link" href="#">Sign In</a>
</li>
</ul>
</div>
</nav>
That ends up looking like this:
Many thanks in advance.
I think it is because of the div block here:
<div class="nav nav-item">
<p class="navbar-text">Signed in as User#1234</p>
</div>
A div is always displayed as a block element and thats the reason why it will start a new line below the div block. If you put the item as a part of the list tag then it should work. For example:
<div class="nav navbar-nav pull-md-right">
<ul class="nav navbar-nav">
<li class="nav-item">
<p class="navbar-text">Signed in as User#1234</p>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Sign In</a>
</li>
</ul>
</div>