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>
Related
EDIT:
I resized the drop-down menu bar, removed the borders & added a box-shadow. The drop-down button is on the right side when clicked the menu opens underneath the site title on the left, here's the updated code & picture of update:
enter image description here
HTML:
<body id="page-top">
<!-- Navigation -->
<nav class="navbar navbar-expand-lg navbar-light fixed-top py-3" id="mainNav">
<div class="container">
<a class="navbar-brand js-scroll-trigger" href="#page-top">My Site</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 my-2 my-lg-0">
<li class="nav-item">
<a class="nav-link js-scroll-trigger" href="#about">About</a>
</li>
<li class="nav-item">
<a class="nav-link js-scroll-trigger" href="#services">Services</a>
</li>
<li class="nav-item">
<a class="nav-link js-scroll-trigger" href="#portfolio">Portfolio</a>
</li>
<li class="nav-item">
<a class="nav-link js-scroll-trigger" href="#contact">Contact</a>
</li>
</ul>
</div>
</div>
</nav>
CSS:
#media screen and (max-width:991.98px) {
.navbar-nav{
background-color: #f0ede8;
height: 200px;
width: 100px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
float:left;
}
.navbar-nav .nav-item{
text-align: center
}
}
Thanks again for your help!
I'm customizing a Bootstrap theme and would like to keep my navbar transparent but have the drop-down menu have a background color when it's resized to a smaller device. I tried altering the media queries but can't seem to figure this out. I'm still trying to get the hang of Bootstrap. I've attached two screenshots of my site as reference.The first one is how the drop-down menu currently looks. The second one is how I'd like for the drop-down menu to look.
Thank you!
HTML
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet" />
<body id="page-top">
<!-- Navigation -->
<nav class="navbar navbar-expand-lg navbar-light fixed-top py-3" id="mainNav">
<div class="container">
<a class="navbar-brand js-scroll-trigger" href="#page-top">My Site</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 my-2 my-lg-0">
<li class="nav-item">
<a class="nav-link js-scroll-trigger" href="#about">About</a>
</li>
<li class="nav-item">
<a class="nav-link js-scroll-trigger" href="#services">Services</a>
</li>
<li class="nav-item">
<a class="nav-link js-scroll-trigger" href="#portfolio">Portfolio</a>
</li>
<li class="nav-item">
<a class="nav-link js-scroll-trigger" href="#contact">Contact</a>
</li>
</ul>
</div>
</div>
</nav>
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js#1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
</body>
if u want the menu size in half and positioned it under the title,
i just added a little code from #Shanice Morgan
#media screen and (max-width:768px){
.navbar-nav{
background-color:gainsboro;
border:blue 2px solid;
float:left; /* left side */
}
.navbar-nav .nav-item{
border:blue 2px solid;
padding: 0px 10px;
}
}
Try this:
#media screen and (max-width:768px){
.navbar-nav{
background-color:gainsboro;
border:blue 2px solid;
}
.navbar-nav .nav-item{
border:blue 2px solid;
}
}
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.
The hover is not covering all the height of the link, How I do to fix the hover?
I know that the combination of colors is not correct but is only for testing*
I'm using Bootstrap 4.3.1
.navbar .collapse ul li{
}
.navbar .collapse ul li a{
font-family: 'Comfortaa', cursive;
font-size: 20px;
color: skyblue !important;
}
.navbar .collapse ul li a:hover{
background-color: deepskyblue;
height: 100%;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<nav class="navbar navbar-expand-lg navbar-light bg-light fixed-top">
<a class="navbar-brand mx-5" href="#">
<img src="Resources/Images/bootstrap-solid.svg" width="30" height="30" alt="">
</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">
<ul class="navbar-nav ml-auto mx-3">
<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="#">News</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Services</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Contact</a>
</li>
</ul>
</div>
</nav>
Your first div's .navbar class is adding padding. That's why the hover is not displaying like you want.
FIX: Remove padding (top & bottom) by adding this CSS
.navbar {
padding: 0 1rem;
}
Navbar items remain visible for some time after closing the collapsed navbar as you can see in the second picture.
Html code:
<nav class="navbar navbar-dark fixed-top" style="background-color: transparent;>
<a class="navbar-brand" href="#">Muhammad Yasir</a>
<button class="navbar-toggler navbar-toggler-right collapsed" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="my-1 mx-2 close">X</span>
<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="#">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Resume</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Contact</a>
</li>
</ul>
</div>
</nav>
CSS code:
.navbar-toggler>.close {
display:inline;
color: white;
}
.navbar-toggler.collapsed>.close, .navbar-toggler:not(.collapsed)>.navbar-toggler-icon {
display:none;
}
Here is when I collapse the navbar
And here is how collapsed item shows for some time even after closing the navbar
For displaying collapsed navbar on the whole page and to change color on collapsing I have used some javascript along with css.
Javascript code:
$(".navbar-toggler").click(function(){
$("nav").toggleClass("navbar-blue");
})
Css code:
.navbar-collapse {
height: 100vh;
}
.navbar{
color: white;
}
.navbar-blue{
background-color: #23415C !important;
}
I don't know where is the problem in my code. I guess maybe its due to increased size of collapsing navbar.
Change the JS to
$(".navbar-toggler").on('click', function(e){
$("nav").toggleClass("navbar-blue");
setTimeout(() => {
$($(e.target).closest('.navbar-toggler').data('target')).toggleClass('opened')
}, 350);
})
... and add this to your CSS:
.collapsing.opened {
transition-duration: 0s;
}
See it working:
$(".navbar-toggler").on('click', function(e){
$("nav").toggleClass("navbar-blue");
setTimeout(() => {
$($(e.target).closest('.navbar-toggler').data('target')).toggleClass('opened')
}, 350);
})
.navbar-toggler>.close {
color: white;
}
.navbar-toggler.collapsed>.close, .navbar-toggler:not(.collapsed)>.navbar-toggler-icon {
display:none;
}
.navbar{
color: white;
}
.navbar-blue{
background-color: #23415C !important;
}
.collapsing.opened {
transition-duration: 0s;
}
.navbar-collapse > .navbar-nav {
height: calc(100vh - 58px);
}
.navbar-toggler>.close {
width: 30px;
height: 30px;
}
body {
background-color: #212121 !important;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
<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.6/umd/popper.min.js" integrity="sha384-wHAiFfRlMFy6i5SRaxvfOCifBUQy1xHdJ/yoi7FRNXMRBu5WHdZYu1hA6ZOblgut" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js" integrity="sha384-B0UglyR+jN6CkvvICOB2joaf5I4l3gm9GU6Hc1og6Ls7i6U/mkkaduKaBhlAXv9k" crossorigin="anonymous"></script>
<nav class="navbar navbar-dark fixed-top" style="background-color: transparent;">
<a class="navbar-brand" href="#">Muhammad Yasir</a>
<button class="navbar-toggler navbar-toggler-right collapsed" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="close">X</span>
<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="#">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Resume</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Contact</a>
</li>
</ul>
</div>
</nav>
Additionally,
.navbar-collapse > .navbar-nav {
height: calc(100vh - 58px);
}
... fixes the opening animation of your collapse (removes the jump), and
.navbar-toggler>.close {
width: 30px;
height: 30px;
}
... combined with removing mx-1 my-2 classes from it will stop your title from slightly changing position when closing/opening the .navbar-collapse.
Here's how the above works:
.navbar-collapses animation is performed by swiftly adding .collapsing class to it (which gives it a transition-duration of .35s). Setting the duration to 0s fixes the problem, but also disables the opening animation, as Bootstrap uses the same class for both opening and closing animations.
Had they used a different class for opening (say: .expanding),
.collapsing {
transition-duration: 0s
}
... would have been sufficient.
That's why the solution is to toggle a custom class on the target element (.opened) 350ms after the toggler is pressed and only override the transition-duration value when both classes are present.
In theory, this method has the potential to break when one clicks very rapidly (more than once every 350ms) on the toggler button but, since it's an edge case and it involves complicating the solution considerably, I chose not to deal with it.
I am working on a website and I want the navigation (only home the homepage) to be transparent and the image to cover the full screen. This is what I have so far:
The nav-bar is styled in this way because that's how it is on the other pages.
This is my HTML:
<div class="container-fluid">
<nav class="navbar navbar-expand-lg navbar-light bg-light fixed-top navbar-trans" id="mainNav">
<a class="navbar-brand js-scroll-trigger" href="#page-top">
<img class="logo" src="app/assets/images/bunklogo.png" alt="bunk">
Bunk.
</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">
Menu
<i class="fa fa-bars"></i>
</button>
<div class="collapse navbar-collapse" id="navbarResponsive">
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link js-scroll-trigger" href="#tenant">TENANT</a>
</li>
<li class="nav-item">
<a class="nav-link js-scroll-trigger" href="landlord.html">LANDLORD</a>
</li>
<li class="nav-item">
<a class="nav-link js-scroll-trigger" href="#bunkfaqs">BUNK FAQs</a>
</li>
<li class="nav-item">
<a class="nav-link js-scroll-trigger" href="#blog">BLOG</a>
</li>
<li class="nav-item">
<a class="nav-link js-scroll-trigger" href="#team">TEAM</a>
</li>
<li class="nav-item">
<a class="nav-link js-scroll-trigger" href="#login">LOG IN</a>
</li>
<li class="nav-iten">
<button class="btn signup"> SIGN UP</button>
</li>
</ul>
</div>
</nav>
</div>
and my css:
#mainNav {
background-image: linear-gradient(90deg, #439EE0 0%, #26ADE6 100%);
/*margin-top: -0.5%;*/
padding-top: 1rem;
padding-bottom: 3rem;
margin-bottom: 0;
}
.navbar-toggler.navbar-toggler-right {
color:white;
border: 1px solid white;
font-size: 1.2rem;
padding: 1rem;
}
.navbar-light .navbar-nav .nav-link {
font-size: 1.2rem;
color: #ffffff;
letter-spacing: 0.8px;
text-align: center;
padding-right: 5rem;
}
.navbar-brand.js-scroll-trigger {
color: #ffffff;
font-size: 2rem;
}
Any suggestions?
There's no need for any custom css hacks here.
To make your Bootstrap navbar transparent, all you need is to remove the class bg-light.
That's it!
The bg in bg-light stands for "background". So, if you leave out the class for background color, you automatically have a transparent navbar.
Ok, first of all you need to separate the home page from the other pages. To do this just insert an ID on your body section like #home.
So, in this scenario you will add the following:
body#home #mainNav {
background:transparent;
background-image:none;
position:absolute;
width:100%;
left:0px;
right:0px;
top:0px;
}
Don't forget that your Slider (or w/e you have after #mainNav), maybe will need some padding-top equals to the #mainNav height, for example (in case that your #mainNav height is 50px):
.slider {
padding-top:50px;
}
You can give an extra class to your navbar on your index.html only. (or your homepage html file name.)
<nav class="navbar navbar-expand-lg navbar-light bg-light fixed-top navbar-trans navbar--home" id="mainNav">
Then you can select this class like :
#mainNav.navbar--home{
background:transparent;
}
Use home page body class, for ex.: ".home / .home-index / .index-page" as a parent of ".nav.fixed-top" container.
CSS::
.home .nav.fixed-top{
background:rgba(0, 0, 0, 0) none repeat scroll 0 0;
}