Make the active navigation bar item bold and change its font color when clicked

I'm currently working on creating a navigation bar that changes font weight to bold and color to teal when a link is clicked. The issue I'm facing is that when the links are clicked, they turn blue instead of teal and lose their bold formatting, except for the "About Me" link which remains bold.

Here's an example of the desired outcome for the navigation bar:

When an item is clicked, it should switch to bold and teal, while the other items should remain black and normal font weight.

$(document).ready(function() {
  $("ul.navbar-nav > li").click(function(e) {
    $("ul.navbar-nav > li").removeClass("active");
    $(this).addClass("active");
  });
});
.navbar {
  background: #FEFFFF !important;
}

.nav-link {
  color: black;
  padding-right: 0.5 rem;
  padding-left: 0.5 rem;
}

.nav-item.active {
  color: #4ba67f;
  font-weight: bold;
  padding-right: 0.5rem;
  padding-left: 0.5rem;
}

.navbar-nav .nav-item:hover .nav-link {
  color: #4ba67f;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="24464b4b50575056455464110a140a16">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="781a17170c0b0c0a1908384d5648564a">[email protected]</a>/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
<link href="styles.css" rel="stylesheet">
<title>My Webpage</title>

<nav class="navbar navbar-expand-lg bg-light">
  <div class="container-md">
    <ul class="navbar-nav">
      <li class="nav-item active">
        <a class="nav-link" href="#"><span>ABOUT ME</span></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#"><span>CV</span></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#"><span>PROJECTS</span></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#"><span>GITHUB</span></a>
      </li>
    </ul>
  </div>
</nav>

Answer №1

Setting the active class to the li tag only colors the li tag, not the a tag inside it. To fix this issue, include the following CSS:

.nav-item.active , .nav-item.active a {
  color: #4ba67f;
  font-weight: bold;
  padding-right: 0.5rem;
  padding-left: 0.5rem;
}

Answer №2

$(document).ready(function() {
  $("ul.navbar-nav > li > a").click(function(e) {
    $("ul.navbar-nav > li > a").removeClass("active");
    $(this).addClass("active");
  });
});
.navbar {
  background: #FEFFFF !important;
}

.nav-item .nav-link.active {
  color: #4ba67f;
  font-weight: bold;
  padding-right: 0.5rem;
  padding-left: 0.5rem;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1c7e7373686f686e7d6c5c29322c322e">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="04666b6b70777076657444312a342a36">[email protected]</a>/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
<link href="styles.css" rel="stylesheet">
<title>My Webpage</title>

<nav class="navbar navbar-expand-lg bg-light">
  <div class="container-md">
    <ul class="navbar-nav">
      <li class="nav-item">
        <a class="nav-link active" href="#"><span>ABOUT ME</span></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#"><span>CV</span></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#"><span>PROJECTS</span></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#"><span>GITHUB</span></a>
      </li>
    </ul>
  </div>
</nav>

Answer №3

Here is an updated version of your CSS...

.navbar{
    background:#FEFFFF !important; 
}
.nav-link{
    padding-right: 0.5rem;
    padding-left: 0.5rem;
}
.nav-item > a {
    color:black;
}
.nav-item.active > a{
    color:#4ba67f;
    font-weight: bold;
    padding-right: 0.5rem;
    padding-left: 0.5rem;
}

.navbar-nav .nav-item:hover .nav-link {
    color: #4ba67f ;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

dual slider controls on a single webpage

I am attempting to place two different sliders on the same page. When I implement the following code for one slider, it functions correctly: <h3>Strength of Belief</h3> <div class="slidecontainer"> <div class="slider_left"> < ...

Merging backend code (Java and Python) into HTML for seamless integration

While I have a solid background in back-end coding languages like Java and Python, the task at hand requires integrating this code into a website. The backend code comprises various methods and classes to encrypt text messages using encryption techniques ...

Using PHP regular expressions to find the final occurrence trailing after the last match in HTML

Looking to analyze a webpage (where the same product price is compared on different stores - each store has a block with its logo, price...) I want to separate each store into a different array element using preg_match_all I am trying to skip the advert ...

Guide to showcasing two spring models within a single HTML table row utilizing Thymeleaf

I need help formatting a row to display in an HTML table: "$num out of $totalCount" For example, if num=5 and totalCount=8, I want it to show as 5 out of 8. This is the code I have so far: ... <tr> <td th:text=&quo ...

When using iOS, inserting an iFrame with a source URL (SRC) on a webpage will automatically open the URL

Currently facing a peculiar issue within a Cordova app, specifically on iOS. The scenario is: I have an HTML page with existing content. At a later stage, I fetch additional HTML from a remote source and inject it into the original HTML page. However, whe ...

Bootstrap 4 Navigation Bar: Aligning dropdown menu to the right with overflow opening on the right side

Utilizing Bootstrap 4 for a simple navbar, I recently noticed that my right-aligned dropdown opens to the right, causing overflow and displaying the horizontal browser scrollbar. Here is the code for the navbar: <nav class="navbar navbar-expand-lg nav ...

What is the secret behind Facebook's ability to keep the chat bar fixed at the bottom of the browser window?

I am curious to learn about the method Facebook uses to keep the chat bar at the bottom of the browser window even when we scroll through the page. ...

What is the best way to link buttons to specific drop down sections?

How can I create multiple drop down divs with different content for each button click? JSFiddle: http://jsfiddle.net/maddiwu/xe6xtfqh/ .slide { overflow-y: hidden; max-width: 100%; overflow-x: hidden; max-height: 100vw; /* approximate ...

Encountering a Django Channels issue while loading the JavaScript wrapper

After successfully running the server, the following appeared in my cmd prompt: System check identified no issues (0 silenced). January 21, 2020 - 17:43:42 Django version 3.0.2, using settings 'crm.settings' Starting ASGI/Channels version 2.4.0 ...

A sleek fixed navbar in Bootstrap showcasing a shrunk logo positioned above the navbar

Is there a way to implement a fixed navbar with a parent logo using bootstrap? I would like the logo to shrink as you scroll, while still remaining visible within the fixed navbar. For a demo of the shrinking logo and navbar, you can visit: ...

Complex UL structure with nested LI items and both column and inline styling

I'm facing a challenge that seems insurmountable - I can't even begin to tackle it, so I don't have a polished solution to present. All I have is the source code and my goal. My task is to create a three-level UL structure. The top level sh ...

The website is missing a scrollbar

I am having trouble getting the scroll bar to show up on my website, even after adding the following CSS: { overflow-y:scroll; } I have tried putting it in both the body and HTML sections of the CSS. If you'd like to take a look at the website, her ...

Issues encountered with the delete function using distinct row identification format

I am encountering an issue with variable value in input field. To provide some context, I have client information stored in a MySQL database. Using PHP, I retrieve this data and display it in a table. Currently, I am working on implementing a delete optio ...

Strategies for optimizing progressive enhancement

Designing a website that is accessible to everyone is truly an art form, and Progressive Enhancement is something I hold dear... I am curious to hear about the best techniques you have used to ensure websites work for all users, regardless of their browse ...

Is it possible to apply CSS opacity only to the background color without affecting the text on top of it

Is it possible to set the opacity property specifically for the background of a div, without affecting the text inside it? I attempted to achieve this with the following code: background: #CCC; opacity: 0.6; However, it seems like the opacity change is ...

Using Javascript to dynamically add form fields based on the selection made in a combo box

I am in the process of creating an information submission page for a website, where various fields will need to be dynamically generated based on the selection made in a combo box. For example, if the user selects "2" from the combo box, then two addition ...

Utilizing jQuery to emphasize a selected table row upon radio button selection

When I have a table (standard markup) with a radio select in each row, I want to highlight the selected radio button. It seems simple, but for some reason it's not triggering as expected. Here is the example of the markup: The Table Row: <tr> ...

Always ensure that the horizontal scrollbar is visible without the need for vertical scrolling

I'm attempting to create a data grid-style layout where the left side is fixed and only the right side is scrollable. However, the entire div needs to be vertically scrollable. The concept is functioning correctly, but the horizontal scrollbar appears ...

Repurpose the identical popup window

I am searching for a solution to use the same pop-up window whenever it is called. Currently, I need to submit a form into a pop-up window and it works well, except in Chrome iOS where each form submission opens a new tab/window instead of reusing the prev ...

Ensure that both textarea and pre elements have equal dimensions and line wrapping behavior in Internet Explorer

I am in the process of implementing a dynamic textarea that resizes automatically, based on a technique discussed in this article: Alistapart: Expanding Textareas (2011). The idea is quite straightforward: by using a pre element to mirror the input in the ...