How do I use image for nav background in Bootstrap 4 - css

How do I use Bootstrap 4 with a pattern background
Just converting website to use Boostrap and currently I have this header using the dark background and it looks fine but ideally instead of just the dark grey background I want to use the patterned background I was previously using as the nav bar, this would go over the whole width of the page instead of the dark grey.
<div class="container-fluid fixed-top bg-dark">
<div class="navbar navbar-expand-lg navbar-dark bg-dark container text-center">
<a href="http://jthink.net/index.jsp" class="navbar-brand">
<img src="http://www.jthink.net/images/icon32.png" width="30" height="30" class="d-inline-block align-top" alt="">
JThink
</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<div class="navbar-nav">
<a class="nav-item nav-link" href="http://blog.jthink.net">
Blog
</a>
<a class="nav-item nav-link" href="http://jthink.net/jaikozforum">
User Forum
</a>
<a class="nav-item nav-link" href="http://jthink.net/songkong">
SongKong
</a>
<a class="nav-item nav-link" href="http://jthink.net/jaikoz">
Jaikoz
</a>
<a class="nav-item nav-link" href="http://albunack.net" target="_blank">
Albunack
</a>
<a class="nav-item nav-link" href="http://www.jthink.net/jaudiotagger/index.jsp" >
Jaudiotagger
</a>
</div>
</div>
</div>
</div>
e.g instead of looking like this
I want the blue background in this screenshot to be used for the navbar

You can apply a background image to your bootstrap navbar by adding a background-image style to the div surrounding your navbar.
Here are the attributes you can apply to your image:
background-image: url("[file-location].png");
background-repeat: ;/* Determine what direction the image will repeat */
background-position: ; /* Where the image will be positioned relative to "top, right, bottom, and/or left */
background-attachment: ; /* use with "fixed" to make it scroll with page */
Read more about this property at W3Schools.
If the background image is making your hyperlink hard to read, change its normal color using color: [hex value / color name]; on nav-item or nav-link like this:
.nav-item > a {
color: #000000; /* [hex value / color name] */
}
Then you can change the color of the link when you hover over it by using the following:
.nav-item > a:hover {
color: red; /* [hex value / color name] */
}

Related

Navbar: when I fixed the navbar to the top, my components are also fixed to the top

I work on a personnal project with Angular. I want to have a top fixed navbar to write "fixed-top" in <nav>. The navbar is fixed but the probleme is that my components are also fixed to the top which is a big issue that I don't know how to resolve.
Has you can see in my screen, my component movie list is also fixed to the top so the top of the component is hide by the navbar. It is that probleme with all components/pages.
[This is a screen of my app.]1
This is my navbar template
<nav class="navbar fixed-top navbar-expand-lg navbar-dark bg-dark">
<a class="navbar-brand" href="#">
<img src="../../assets/logo1.png" alt="">
</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav ml-auto">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Search
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<a class="dropdown-item" routerLinkActive="active" routerLink="search/movie">Movie</a>
<a class="dropdown-item" routerLinkActive="active" routerLink="search/tv-show">Tv Show</a>
</div>
</li>
<li class="nav-item" routerLinkActive="active">
<a class="nav-link" routerLink="movies">Movies</a>
</li>
<li class="nav-item" routerLinkActive="active">
<a class="nav-link"routerLink="tv-shows">Tv Shows</a>
</li>
</ul>
</div>
</nav>
This is the css
nav {
-webkit-box-shadow: 0px 5px 4px -1px rgba(173,161,173,1);
-moz-box-shadow: 0px 5px 4px -1px rgba(173,161,173,1);
box-shadow: 0px 5px 4px -1px rgba(173,161,173,1);
height: 60px;
width: 100%;
}
This is my app component calling the navbar
<app-header></app-header>
<router-outlet></router-outlet>
If you have some idea please help me.
Thanks
Here is the problem:
<nav class=". . . fixed-top . . .">
You have specified the fixed-top class. That implies that navbar will overlap beneath contents.
You could try removing that class. Or, if you want that specific class, try adding a padding: body { padding-top: 80px; } (80px or what you find it more confortable for web content shown).

Group items into dropdown menu when collapsed

I have just found this type of collapse navbar from a site, all menu items grouped into a dropdown menu and positioned right, its different from usual bootstrap collapse navbar
Sorry for having not enough reputations to post image
https://i.imgur.com/EFzI7hl.png
https://i.imgur.com/DhS4BlZ.png
I have tried many suggestions but it didnt work exactly like what theirs did, the result is always full vertical dropdown menu
https://i.imgur.com/s5O7cSD.png
My code
<nav class="navbar fixed-top navbar-expand-lg py-0" style="background-color: #0d47a1;">
<a class="navbar-brand" href="/dashboard/">
<img src="/images/logo.webp" width="40" height="40" alt="">
</a>
<button class="navbar-toggler navbar-dark" type="button" data-toggle="collapse" data-target="#collapsibleNavbar">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="collapsibleNavbar">
<ul class="navbar-nav">
<li class="nav-item px-2">
<a class="nav-link" href="#">Shop Manager</a>
</li>
<li class="nav-item px-2">
<a class="nav-link" href="#">Customer Care</a>
</li>
<li class="nav-item px-2">
<a class="nav-link" href="#">Messenger Order</a>
</li>
</ul>
</div>
</nav>
You'll need to add a little custom css to get the menu smaller that full width:
.navbar-collapse {
position: absolute;
right: 0;
top: 40px;
background-color: #333;
}
#media (min-width: 768px) {
.navbar-collapse {
background-color: transparent;
position: initial; /* Using 'initial' here rather than specificing 'relative'.*/
}
}
Here is a fiddle showing it working. You can customize it form there to get it looking like the examples you provided, but that's the rough idea.

Adding a fixed sticky nav bar which doesn't cover the image

I am trying to create a responsive bootstrap sticky nav to go above the carousel. However the navbar covers the top proportion of the image. How can i change so that it sticks above for desktop/mobile, but doesn't overflow onto the image itself.
<nav class="navbar navbar-expand-lg bg-primary fixed-top navbar " color-on-scroll="400">
<div class="container">
<div class="navbar-translate">
<button class="navbar-toggler navbar-toggler" type="button" data-toggle="collapse" data-target="#navigation" aria-controls="navigation-index" aria-expanded="false" aria-label="Toggle navigation">
<span><i class="fas fa-bars" style="
color: #ffffff;
"></i></span>
</button>
<img src="https://www.w3schools.com/bootstrap/chicago.jpg" class="img-fluid" alt="Responsive image">
</div>
<div class="collapse navbar-collapse justify-content-end" id="navigation" >
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="javascript:void(0)" onclick="scrollToDownload()">
<p>Location</p>
Give margin-top:100px or what is the height of you navigation menu, applying that height to the slider parent div class and it works fine.
Solution:
I added body { padding-top: 90px; }

Not able to get rid of Navbar item top/bottom margin?

I'm new with Bootstrap 4 and SASS. I've been struggling to learn how to make changes but have been able get a couple done. However, one of them I can't seem to figure out is the margin or padding at the top and bottom of the navbar item.
Here is what I've done. On a new MVC project I installed Bootstrap 4 and Bootstrap.sass. I then created a new _custom-variables and copied the _variables content into it so that I can make some changes. I then created a _my-theme.scss file and imported all of them into my site.scss like this.
#import "scss/_custom-variables.scss";
#import "scss/_bootstrap.scss";
#import "scss/_my-theme.scss";
I was finally able to figure out how to change the background color of the navbar by creating a variable for my color and applying it to the _custom-variables and changing the link text to white:
$main-color: #0078D2;
// Navbar links
$navbar-default-link-color: #fff !default;
$navbar-default-link-hover-color: #fff !default;
$navbar-default-link-hover-bg: darken($main-color, 6.5%) !default;
I also had to set the following in the _my-theme.css to actually change the text color to white. I've watched some videos and I know it has to do with specifcicity but I still haven't really got my head wrapped around it. Based on a video I watched, what I did was inspect the link and found the properties that I just copied straight in to the _my-theme.scss.
This is what I had to add to the _my-theme.scss to get the text white.
.navbar-dark
.navbar-nav
.nav-link {
color: #fff;
margin-top: 0px;
margin-bottom: 0px;
}
.navbar-default .navbar-nav > .active > a,
.navbar-default .navbar-nav > .active > a:hover,
.navbar-default .navbar-nav > .active > a:focus{
color: #fff;
}
However, there is still some padding or margin at the top each link that I would like to get rid of. You can see that I tried setting the margin-top: 0px and margin-bottom: 0px but that didn't work, neither did setting padding to 0px.
I also tried finding the setting in dev tools in Chrome but couldn't find what it is that is setting this.
This is my navbar layout:
<header>
<nav class="navbar navbar-default navbar-expand-md navbar-dark fixed-top rounded-0">
<a class="navbar-brand" href="#">Fixed navbar</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-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 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>
</div>
</nav>
</header>
How can I remove this spacing and make the dark blue take the whole height of the navbar?
By default the navbar class contains internal padding. If you want to remove the top and bottom padding, just add this CSS:
.navbar {
padding-top: 0;
padding-bottom: 0;
}
The space is padding, and not margins. Use the py-0 utility class to remove the padding from the navbar. And then add padding back to the nav-link to maintain the original height:
https://www.codeply.com/go/roU5MIf724
.navbar-dark
.navbar-nav
.nav-link {
color: #fff;
padding: 10px 0;
}
<nav class="navbar navbar-default navbar-expand-md navbar-dark fixed-top rounded-0 py-0">
<a class="navbar-brand" href="#">Fixed navbar</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-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 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>
</div>
</nav>

Adding logo to bootstrap 4 beta navbar

I want to add my logo in the navbar in bootstrap 4 beta framework.
The height of the navbar should be the same or nearly the same like the normal height without a logo, like you can see in the first example.
Example without and with logo
<nav class="navbar navbar-expand-md navbar-light" style="background-color: #CECFFF;">
<a class="navbar-brand" href="/"><img src="logo.png"></a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbar" aria-controls="navbar" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbar">
<ul class="navbar-nav mr-auto">
<li class="nav-item dropdown">
....
</li>
</ul>
</div>
</nav>
https://codepen.io/anon/pen/gGpJLq
The solutions from another question here are only for bootstrap 4 alpha working.
You could specify the height of the image to ensure it doesn't increase the size of the navbar by adding a class to the img. Like this:
.logo {
max-height: 40px;
padding: 0;
}
<a class="navbar-brand" href="/">
<img src="https://www.lern-online.net/bilder/logo.png" style="height: 35px; padding: 0 auto;">
</a>
here as you can clearly see i gave an inline styling which you can manually modify further on what you need.
The problem here was that the image which you used was appearing in its defaulting size hence the navigation bar was modifying itself to accomodate it we can avoid this by ensuring the size of our image before-hand.
hope I helped.

Resources