Active class not changing to items for navbar in bootstrap - html

I have this navbar from bootstrap which is supposed to add active class on nav-item or on anchor element on click, but while inspecting it in browser its not changing states, I don't understand why.
.nav-link {
color: #FFF;
width: 100%;
height: 100%;
text-transform: uppercase;
}
.nav-link::after {
content: '';
display: block;
width: 0;
height: 2px;
background: green;
transition: width .3s;
}
.nav-link:hover::after {
width: 100%;
}
.navbar-nav>.active>a {
color: #00cc00;
}
<nav class="navbar navbar-expand-lg navbar-dark " style="background-color: black;">
<div class="container-fluid">
<a class="navbar-brand d-lg-none" href="#"><img src="../assets/images/howdy-white.png" style="width:45px; height:45px;" /></a>
<button class="navbar-toggler shadow-none" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<a class="navbar-brand d-none d-lg-block" href="#"><img src="../assets/images/howdy-white.png" style="width:45px; height:45px;" /></a>
<div class="collapse navbar-collapse justify-content-end" id="navbarNavAltMarkup">
<ul class="navbar-nav">
<li class="nav-item active">
<a class="nav-link" href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">about</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">works</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">contacts</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">services</a>
</li>
</ul>
</div>
</div>
</nav>

You need add some javascript event handler while you click that link will do some action.
example if you using jQuery with that navbar structure like you shown
$("ul.navbar-nav li").on("click", function() {
$("ul.navbar-nav li").removeClass("active");
$(this).addClass("active")
})
or if you like using vanilla javascript with DOM query SelectorAll
let navbarLink = document.querySelectorAll(".navbar-nav li")
document.addEventListener("click", function(e) {
for (let x = 0;x < navbarLink.length;x++) {
navbarLink[x].classList.remove("active")
}
e.target.closest("li").classList.add("active")
})
hope this work on your case.

You suppose to add active class on anchor tag

Related

Section Area gets hidden behind sticky navbar

I am using sticky navbar in my page . However I see that when i try to go to that section via nav link a certain part of section gets hidden behind the navbar. I want my entire section area to be visible below navbar. I tried writing jquery code to set offset but its not helping.
here is my navbar code
<header id="navigation-items" style="width:100%; z-index: 99;">
<nav class="navbar navbar-expand-lg navbar-light " style="background-color: #7ea7ca;">
<div class="container-fluid">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#section1">Section 1</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#section2">Section 2</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#section3">Section 3</a>
</li>
<li class="nav-item">
<a class="nav-link " href="#section4">Section 4</a>
</li>
</ul>
</div>
</div>
</nav>
</header>
Here is the section code:
<div class="main">
<section id="section1"><div class="container">..Section 1..</div> </section>
<section id="section2"><div class="container">..Section 2..</div> </section>
<section id="section3"><div class="container">..Section 3..</div> </section>
<section id="section4"><div class="container">..Section 1..</div> </section>
<div>
Here is jquery i wrote for the offset section
$(document).ready(function(){
/* For setting the offset header */
var headerHeight = $("#navigation-items").height();
$('a[href*="id"]').bind("click", function(e) {
e.preventDefault();
var target = $(this).attr("href"); //Get the target
var scrollToPosition = $(target).offset().top - (headerHeight);
$('html').animate({
'scrollTop': scrollToPosition
}, 300, function(target) {
window.location.hash = target;
})
});
});
Here is the css for my code
.main>section{
border:1px solid red;
padding-top:80px
}
.main>section>.container{
background-color: rgba(231, 158, 158, 0.884);
}
.sticky{
position: fixed;
top: 0;
width: 100%
}
```
I am using bootstrap 5 and jquery version 3.5.1. [Link to HTML page and issue][1]
[1]: https://codepen.io/maddy-176/pen/abyWZEQ
Use scroll-margin-top on sections.
section {
scroll-margin-top: 60px;
}

Toggle Bar responsiveness issue

I have written code using bootstrap. But my issue is in the Toggle bar , like when I shrink the size to see my Web page responsive or not then the toggle bar is not open.
It shows the three button but when i click then it is not expanding.
Please give me suggestions to get rid of this issue.
Thanks.
Html code
<section>
<div id="container1">
<nav class="navbar navbar-expand-lg navbar-light bg-dark">
<div class="container-fluid">
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-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">
<li class="nav-item">
<a class="scroll nav-link" href="#home" style = "color: #ffff;">HOME</a>
</li>
<li class="nav-item">
<a class="scroll nav-link" href="#about" style = "color: #ffff;">ABOUT</a>
</li>
<li class="nav-item">
<a class="scroll nav-link" href="#services"style = "color: #ffff;">SERVICES</a>
</li>
<li class="nav-item">
<a class="scroll nav-link" href="#contacts" style = "color: #ffff;">CONTACTS</a>
</li>
</ul>
</div>
</div>
</nav>
</div>
</section>
**Css code**
#container1{
margin-top:5rem ;
display: inline-block;
width: 100%;
}
#container1 ul li {
margin-left: 8rem;
margin-right: -5rem;
border-right: 0.0625rem solid grey;
}
#container1 ul li a:hover{
background-color: #5e9ee7;
}
I just get rid of my issue that I have arose a few hours ago. I finally got a fix of my problem. It only happens by removing "bs" which is present in between (data-bs-toggle="collapse") and in (data-bs-target="#navbarNav") in the html code.

Hamburger menu not showing up on smaller screens for bootstrap 5 navabar

'I've put in the stylesheets for both bootstrap css and js in the file. It shows up when I retain default colour options but not when I customise the colour.'
'''
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">item</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">item</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">item</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">item</a>
</li>
</div>
</div>
</nav>
'''
CSS: I've added a background colour to the body and also made the nabber the same colour. I've made the text pink and added a green hover to navbar text.#
body{
background-color: #FFE57C;
}
.navbar{
padding-left: 350px;
}
.navbar a:hover{
color: #FF7CD8;
}
.navbar a {
color: #FF7CD8;
}
.nav navbar-nav .navbar-center {
color: ##FF7CD8;
}
Try retaining that navbar-light class in inside top nav tag, or you can use navbar-dark if you want. check the documentation here for more details. this class is associated with showing hamburger icon in bootstrap 5. see the code from bootstrap.css :
navbar-light .navbar-toggler-icon {background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 30 30'%3e%3cpath stroke='rgba%280, 0, 0, 0.55%29' stroke-linecap='round' stroke-miterlimit='10' stroke-width='2' d='M4 7h22M4 15h22M4 23h22'/%3e%3c/svg%3e");}
but you can change the colors in navbar by adding an id for ul tag and add the properties for id in style, this way we can tweak the default properties given by the bootstrap. You might be able to achieve what you need by this code, check the code below:
html:
<nav class="navbar navbar-expand-lg navbar-light">
<div class="container-fluid">
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-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 me-auto mb-2 mb-lg-0" id="nav-list">
<li class="nav-item">
<a class="nav-link active" aria-current="page"href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Item</a>
</li>
</ul>
</div>
</div>
</nav>
css:
body{
background-color: #FFE57C;
}
.navbar{
padding-left: 350px;
}
#nav-list a {
color: #FF7CD8;
}
#nav-list a:hover{
color: #FF7CD8;
}
for sample i just put 2 list items in navbar.

Bootstrap custom file input bugs on navbar overflow

I coded a navbar which is fixed-top.
When I scroll, if there is body content passing through it, it must be hidden.
It works, but when I use the bootstrap custom class for files fields, there is a bug. The file field is not hidden when it overlaps the navbar.
I reproduced the problem here:
html
<!-- NAVBAR -->
<nav class="navbar navbar-expand-lg navbar-dark">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarColor01" aria-controls="navbarColor01" aria-expanded="false" aria-label="Toggle navigation">
<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">
<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">
<a class="nav-link" href="#">About</a>
</li>
</ul>
</div>
</nav>
<!-- Bootstrap custom file input -->
<div class="container">
<div class="custom-file">
<input type="file" class="custom-file-input" id="inputGroupFile01"
aria-describedby="inputGroupFileAddon01">
<label class="custom-file-label" for="inputGroupFile01">Choose file</label>
</div>
css
html, body{
padding-top: 30px;
}
.navbar {
overflow: hidden;
background-color: black;
position: fixed;
top: 0;
width: 100%;
z-index: 1;
}
.navbar .bg-primary{
border: 1px solid black;
}
And the demo : https://jsfiddle.net/kiuega/u3z8be4c/3/
Do you know what the problem is?
EDIT: I just noticed that the same thing happened when we use FullCalendar (https://fullcalendar.io/docs), it's the same thing!
Change the Navbar "z-index" to 2 or more.. its better to make the z-index to 9999 (Extreme Value) that means it always top the top of the content..
otherwise if there is any other div or section that have greater z-index than the navbar then that div or section overlape the navbar..
.navbar {
overflow: hidden;
background-color: black;
position: fixed;
top: 0;
width: 100%;
z-index: 9999;
}

Bootstrap4 navigation link active top border Style

Im trying to get Navigation Link styles like this
In the mobile View active link should be like this =>
For now Using border and border color i got this =>
Respective mobile view
Any solution for get the expected results?
The current code
<nav class="navbar navbar-expand-lg navbar-light ">
<a class="navbar-brand" href="#">
LOGO
</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 justify-content-end" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item active">
<a class="nav-link " href="#">Home</a>
</li>
<li class="nav-item" >
<a class="nav-link" >Services</a>
</li>
<li class="nav-item" >
<a class="nav-link" >About</a>
</li>
<li class="nav-item" >
<a class="nav-link" >Contact</a>
</li>
</ul>
</div>
</nav>
sass for active link
.navbar-nav > .active a
color: $nav-active-color !important
.navbar-nav > .active
border-top: 3px solid $nav-active-color !important
This would be a good use for the :before or :after psuedo classes, but without having any of your code to go off of I can only offer this vague suggestion.
As well, worth seeing the accepted answer here on a thread about nesting pseudo classes just in case you run into one of the outlined issues.
From there, you could add some keyframes and smooth out the transition to make a pretty seamless hover animation.
div {
width: 350px;
height: 100px;
background: lightgray;
position: relative;
margin: 20px;
}
div:before {
content: '';
width: 60px;
height: 4px;
background: gray;
position: absolute;
top: -4px;
}
<div></div>