Skip to main content
Participating Frequently
October 3, 2019
Answered

Nav Bar active class

  • October 3, 2019
  • 3 replies
  • 1291 views

I really hope someone can help ive been trying to solve this problem for 3 days now.

i am creating a website for college i have my template done out with a footer and header bootstrap but my navbar active class is  not working.

it says active on the home page but if you click another link the home page stays active.

This topic has been closed for replies.
Correct answer osgood_

If you are refering to your main navigation:

 

<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="nav-link meal" href="../Pages/home.html">HOME<span class="sr-only"></span></a></li>

<li class="nav-item "> <a class="nav-link meal" href="../Pages/starters.html">STARTER</a></li>
<li class="nav-item "> <a class="nav-link meal" href="../Pages/mains.html">MAIN</a></li>
<li class="nav-item "> <a class="nav-link meal" href="../Pages/Dessert.html">DESSERT</a></li>
<li class="nav-item "> <a class="nav-link meal" href="../Pages/Events.html">EVENTS</a></li>
</ul>

 

Then you need to use some javascript to 'mark' the naviagtion link as you go from page to page:

Include the below at the bottom of ALL your pages, just before the closing </body> tag.

 

<script>
var url = document.URL
var url = url.substr(url.lastIndexOf('/') + 1);
var links = document.querySelectorAll('.navbar-nav li a');
for (var i = 0; i < document.links.length; i++) {
var link = document.links[i].href;
var link = link.substr(link.lastIndexOf('/') + 1);
if (url === link) {
document.links[i].className = 'active';
}
}
</script>

 

 

You can also do this using css.

3 replies

osgood_Correct answer
Brainiac
October 3, 2019

If you are refering to your main navigation:

 

<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="nav-link meal" href="../Pages/home.html">HOME<span class="sr-only"></span></a></li>

<li class="nav-item "> <a class="nav-link meal" href="../Pages/starters.html">STARTER</a></li>
<li class="nav-item "> <a class="nav-link meal" href="../Pages/mains.html">MAIN</a></li>
<li class="nav-item "> <a class="nav-link meal" href="../Pages/Dessert.html">DESSERT</a></li>
<li class="nav-item "> <a class="nav-link meal" href="../Pages/Events.html">EVENTS</a></li>
</ul>

 

Then you need to use some javascript to 'mark' the naviagtion link as you go from page to page:

Include the below at the bottom of ALL your pages, just before the closing </body> tag.

 

<script>
var url = document.URL
var url = url.substr(url.lastIndexOf('/') + 1);
var links = document.querySelectorAll('.navbar-nav li a');
for (var i = 0; i < document.links.length; i++) {
var link = document.links[i].href;
var link = link.substr(link.lastIndexOf('/') + 1);
if (url === link) {
document.links[i].className = 'active';
}
}
</script>

 

 

You can also do this using css.

Participating Frequently
October 3, 2019
Thank you so much osgood that done the trick my head was melted trying to figure that out
BenPleysier
Adobe Expert
October 3, 2019

On each page, inlcude the active class for the relative menu item.

 

As an example, for the HOME page, the link will look like:

<a class="nav-link meal active" href="../Pages/home.html">HOME<span class="sr-only"></span></a></li>

Wappler is the DMXzone-made Dreamweaver replacement and includes the best of their powerful extensions, as well as much more!
Participating Frequently
October 3, 2019
i done that but every item is active not just the page i am on
Brainiac
October 3, 2019

You DONT apply the active class to each link ONLY the link you want active for the specific page which is being visited. This is not a great solution should you wish to move your navigation into an include file at any time down the road. Its more of a starter workflow which may be suitable for you skill level at this particular time. A more robust solution is the one I posted after this post, it looks like you have that working.

Participating Frequently
October 3, 2019

<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- TemplateBeginEditable name="doctitle" -->
<title>Untitled Document</title>
<!-- TemplateEndEditable -->
<!-- Bootstrap -->
<link href="../css/bootstrap-4.3.1.css" rel="stylesheet">
<link href="../css/main.css" rel="stylesheet" type="text/css">
<!--The following script tag downloads a font from the Adobe Edge Web Fonts server for use within the web page. We recommend that you do not modify it.-->
<script>var __adobewebfontsappname__="dreamweaver"</script>
<script src="http://use.edgefonts.net/arizonia:n4:default.js" type="text/javascript"></script>
<!-- TemplateBeginEditable name="head" -->
<!-- TemplateEndEditable -->
</head>
<body class="site">
<div id="fb-root"></div>
<script async defer crossorigin="anonymous" src="https://connect.facebook.net/en_US/sdk.js#xfbml=1&version=v4.0&appId=621398881723778&autoLogAppEvents=1"></script>
<div class="container-fluid top">
<div class="row logos">
<div class="col-lg-12 logo h1">Traditional Scottish Food</div>
</div>

<nav class="navbar navbar-expand-lg navbar-light navbar-back nav-fill">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent1" aria-controls="navbarSupportedContent1" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span></button>
<div class="collapse navbar-collapse" id="navbarSupportedContent1">
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="nav-link meal" href="../Pages/home.html">HOME<span class="sr-only"></span></a></li>

<li class="nav-item "> <a class="nav-link meal" href="../Pages/starters.html">STARTER</a></li>
<li class="nav-item "> <a class="nav-link meal" href="../Pages/mains.html">MAIN</a></li>
<li class="nav-item "> <a class="nav-link meal" href="../Pages/Dessert.html">DESSERT</a></li>
<li class="nav-item "> <a class="nav-link meal" href="../Pages/Events.html">EVENTS</a></li>
</ul>
<!-- searchbar -->
<form class="form-inline my-2 my-lg-0" >
<input class="form-control mr-sm-2 recipe" type="search" placeholder="Recipe Search" arialabel="Search">
<button class="btn btn-rounded btn-sm my-0 recbtn" type="submit">Search</button>
</form>
<!-- end of search bar -->
</div>
</nav>
</div>
<!-- body code goes here -->
<!-- TemplateBeginEditable name="EditRegion1" -->
EditRegion1
<!-- TemplateEndEditable -->


<!-- Footer -->

 




<footer class="page-footer font-small blue pt-4 socialmed">

<!-- Footer Links -->
<div class="container-fluid text-center text-md-left">

<!-- Grid row -->
<div class="row">

<!-- Grid column -->
<div class="col-md-6 mt-md-0 mt-3 offset-lg-0 socialicons col-lg-3">

<!-- Content -->



<ul class="social">
<li>
<a href="https://www.facebook.com/Traditional-Scottish-Food-104360860972456" target="_blank"><i class="fa fa-facebook fa-3x facebook" aria-hidden="true"></i> </a>
<div class="text">FACEBOOK</div>
</li>
<li><a href="https://www.instagram.com/scottishriver24/?hl=en" target="_blank"><i class="fa fa-instagram fa-2x instagram" aria-hidden="true"></i> </a>
<div class="text">INSTAGRAM</div>
</li>
<li><a href="https://twitter.com/Derek15449919" target="_blank"><i class="fa fa-twitter fa-2x twitter" aria-hidden="true"></i> </a>
<div class="text">TWITTER</div>
</li>


</ul>

</div>
<!-- Grid column -->

<hr class="clearfix w-100 d-md-none pb-3">

<!-- Grid column -->
<div class="col-md-3 mb-md-0 mb-3">

<!-- Links -->
<h5 class="text-uppercase">Links</h5>

<ul class="list-unstyled">
<li>
<a href="../Pages/home.html">HOME</a>
</li>
<li>
<a href="../Pages/starters.html">STARTER</a>
</li>

<li>
<a href="../Pages/mains.html">MAIN</a>
</li>
<li>
<a href="../Pages/Dessert.html">DESSERT</a>
</li>
<li>
<a href="../Pages/Events.html">EVENTS</a>
</li>
</ul>

</div>
<!-- Grid column -->

<!-- Grid column -->
<div class="col-md-3 mb-md-0 mb-3 col-lg-4 offset-lg-1">

<!-- Links -->
<h5 class="text-uppercase">Links</h5>

<ul class="list-unstyled">
<li>
<a href="../Pages/About Us.html">ABOUT US</a>
</li>
<li>
<a href="../Pages/Contact us.html">CONTACT US</a>
</li>
<li>
<a href="../Pages/Privacy Policey.html">PRIVACY POLICY</a>
</li>
<li>
<a href="../Pages/Terms Of Use.html">TERMS OF USE</a>
</li>
</ul>

</div>
<!-- Grid column -->

</div>
<!-- Grid row -->


<!-- Footer Links -->

<!-- Copyright -->
<div class="footer-copyright text-center py-3">© 2019 Copyright:
<a href=""> Scottishriver.com</a>
</div>
<!-- Copyright -->

</div></footer>
<!-- Footer -->





<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="../js/jquery-3.3.1.min.js"></script>



<script src="../js/popper.min.js"></script>
<script src="../js/bootstrap-4.3.1.js"></script>
</body>
</html>

Participating Frequently
October 3, 2019

This is my HTML and CSS another thing is that is a template and ive other pager coming off it

@charset "utf-8";
body
.email {
padding-top: 15px;
padding-right: 10px;
padding-bottom: 10px;
padding-left: 10px;
text-align: center;
}
.navbar-back {
background: #75DFFA;
}
.recipe {
font: bold;


}
.meal {
margin-top: 0px;
margin-right: 29px;
margin-left: 30px;
margin-bottom: 4px;
font-weight: bold;
}
.recbtn {
background:#C4ECFC;
font-weight: bold
}
.site {
background-image: url("../images/background image.jpg");
width:100%;
height: auto;
position: relative;
left: 0;
top: 0;
min-height: 100%;

}
.logos {
padding-right: 2.7%;
font-size: 3rem;



}
.logo {
left: 1.3%;
text-align: center;
font-weight: bolder;
font-size: 35px;
font-family: Cambria, "Hoefler Text", "Liberation Serif", Times, "Times New Roman", serif;
font-style: oblique;
color: #0206D3;
}
.ul{

margin: 0;
padding: 0;
position: absolute;
top: 50%;
left:50%;
transform:translate(-50%,-50%);
display: flex;

}
.social{

margin: 0;
padding: 0;
position: absolute;
top: 50%;
left:50%;
transform:translate(-50%,-50%);
display: flex;

}
.social li{
position: relative;
list-style: none;
text-align: center;
width: 80px;
cursor: pointer;

}
.social li .text{
position: absolute;
width: 0;
left:50%;
margin-top: 10px;
margin-left: 0;
transition: 1s;
overflow: hidden;
white-space: nowrap;
font-size: 18px;
color: #FFFFFF;

}
.social li .fa{

font-size: 2em;
}
.social li:hover .text{
width: 120px;
margin-left: -60px;
}
.socialmed {

background: #75DFFA;
}
body {
min-height: 100vh;
position: relative;
margin: 0;
}
.soc {
text-align: center;
}
.top {
background: #75DFFA;
}
.description {
background: #140BE8;
text-align: center;
color: #FFFFFF;
}
.nav-link.active>b{
color:blue;
}