HTML code to make navbar change color when clicked

Having some trouble with my very first website. I managed to put together a navigation bar at the top that is close to what I want (aside from the colors). The problem is, when I click on different links, I want the box to change color but it's stuck highlighted on the homepage. It seems like a simple fix but I can't figure it out. Thanks for any assistance.

body
{
    font-family:sans-serif;
    width: 100%;
    margin: 0px;
}

/* upper strip holding the tabs*/
ul
{
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    position:fixed;
    top: 0px;
    width: 100%;
    background-color: #328CC1
}

li
{
    float:left;
    border-right:3px solid #30FFE3;
}


li a
{
    display: block;
    color: whitesmoke;
    text-decoration: none;
    padding: 14px 16px;
    text-align: center;

}

li a:hover:not(.active) 
{
    background-color: #111;
}

a.active
{
    background-color: #EAB126

}
<ul>
    <li><a class="active" href="#home">Home</a></li>
    <li><a href="#academics">Academics</a></li>
    <li><a href="#athletics">Athletics</a></li>
    <li><a href="#contact">Contact</a></li>
</ul>

Answer №1

Modify the following:

a.active
  background-color: #EAB126
}

to look like this:

li a:focus {
  background-color: #EAB126
}

Check out the JSFiddle demonstration

While this solution meets your requirements, keep in mind that if this is part of a navigation bar, the focus will be lost when navigating to a different page. It might be more efficient to utilize Javascript/JQuery for easier control.

Answer №2

One common approach is to assign each page a class that correlates with the page name: for instance, <div class="academics">.

To implement this, make the following adjustments to your header (within the respective page div):

        <ul>
            <li><a class="for_home" href="#home">Home</a></li>
            <li><a class="for_academics" href="#academics">Academics</a></li>
            <li><a class="for_athletics" href="#athletics">Athletics</a></li>
            <li><a class="forcontact" href="#contact">Contact</a></li>
        </ul>

Subsequently, add the following CSS:

.home .for_home, .academics .for_academics, .athletics .for_athletics, .contact .for_contact {
  background-color: #EAB126;
}

This will ensure that the current page's menu item is highlighted.

Congratulations on creating your very first website!

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

"The CSS attributes for right and left alignment are malfunctioning when used in conjunction with the input-group component in

Recently, I encountered a dilemma with my search input using the input-group feature in Bootstrap 4. My intention was to position this element absolutely by utilizing the left and right attributes. However, I noticed that only one of these attributes would ...

How can JavaScript be used to display the related value in another drop down list when a user selects an option in one drop down list?

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Registration.aspx.cs" Inherits="Practical_5.View.Registration" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta charset ...

The dropdown submenu in Bootstrap 3 remains persistently open

I'm currently working on creating a submenu dropdown and following this reference link: https://www.w3schools.com/bootstrap/tryit.asp?filename=trybs_ref_js_dropdown_events2&stacked=h However, I am facing an issue where all my submenus are always ...

I am having trouble retrieving images from the /media/ directory. Strangely, everything works perfectly when I try to access them through the index. Additionally, the for loop within the templates is

My settings.py file looks like this: Static STATIC_URL = '/static/' STATIC_DIR = [os.path.join(BASE_DIR, "static")] Media MEDIA_ROOT = os.path.join(BASE_DIR, 'media') MEDIA_URL = '/media/' URLs Code - urls.py from d ...

Is it possible to generate a table without any grid lines present?

Currently, I am in the process of designing a table that will function as a popup when a link is clicked within my imported SQL table. An example of this table is included with this message for reference. After conducting thorough research, I attempted to ...

Option tag for a sub-menu

I'm currently working on an application that requires a submenu within the option tag. The idea is that when a user clicks on one of the submenu options, it should appear in the search bar for searching purposes. Unfortunately, I'm facing some ch ...

Internet Explorer is unable to recognize the .less file format

I have created a less file that works perfectly in Chrome and Firefox, but it is not being recognized in IE 9. Does anyone have suggestions on how to make it run in IE 9.0? Below is a sample of the CSS code: @import "../core/variables.less"; @import "Mi ...

Trouble with closing Bootstrap 4 modal

My Bootstrap 4 modal is causing issues as it pops up on button click but won't close unless I refresh the page. I've been attempting to get it to close by clicking the 'close' button or simply by clicking anywhere on the main page body. ...

Manipulating Vue.js data within HREF attributes using PHP

Does anyone know how to successfully pass the data from Vue into PHP? I've attempted a few methods, but when I try to use the resulting link in the href attribute (href = $ where $ name), it shows a strange string instead of the expected URL from the ...

Switching classes in real time with JavaScript

I'm struggling to understand how to toggle a class based on the anchor text that is clicked. <div> <a id="Menu1" href="#">Menu</a> <div id="subMenu1" class="subLevel"> <p>stuff</p> </div> <div> <a i ...

The JavaScript function is not being activated by clicking on Selenium in Chrome

Take a look at the HTML snippet below: <table class="table-main "> <thead> <tbody> <!----> <tr class="" ng-if="mapCtrl.isAdded" style=""> <td/> <td> <td> <t ...

What's the reason for switching from a custom tab icon to a React icon?

I've encountered a strange issue while working on my app - the tab icon keeps changing from the one I've set to the ReactJS icon whenever I navigate between different routes. I have linked the icon correctly in my HTML file, so I'm not sure ...

Sending JSON-encoded data using HTML5 Server-Sent Events (SSE) is a

I have a script that triggers an SSE event to fetch json encoded data from online.php. After some research, I discovered methods for sending JSON data via SSE by adding line breaks. My question is how to send JSON through SSE when the JSON array is genera ...

Identifying Touch Interaction exclusively on WebGL Canvas within threejs

Currently, I am developing a threejs application that requires input from HTML buttons and utilizes Raycast Detection on touch within threeJS. The issue I am encountering is that when the user clicks an HTML button, the Raycast function is triggered as we ...

Tips for inserting a DIV and SCRIPT from a single HTML template into a webpage

I'm working with an HTML template that contains a DIV with a button and a script with a function to call when the button is clicked: <div id="CLC_Form"> various text and checkbox inputs go here... <br> <input type="button" ...

Is there a way to position the text within an email body at the center using SendGrid?

I've been working on creating an email newsletter template using SendGrid, and I've run into a formatting issue where the content doesn't align properly in the email body. The image below shows how it's appearing incorrectly. Does anyon ...

Minimize the empty area separating two adjacent <sup> components

Here is the code snippet I am working with: <p>Test <sup>1</sup> <sup>, 2</sup> </p> The output of this code is "1 , 2". I am having trouble applying styles to the sup elements, as there are no margins or paddings ...

javascript the unseen element becomes visible upon page loading

my website has the following HTML snippet: function getURLParameters() { var parameters = {}; var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) { parameters[key] = value; }); return param ...

Loop through all the textarea fields within a designated class to remove emojis from a field that contains "Gift Message" in its name

I need a pure JavaScript solution for extracting the value of a textarea field with "Gift Message" in its name. I'm currently stuck and unable to achieve this. Can someone assist me in resolving this issue? Below is the HTML code snippet: <span cl ...

Hovering over a link causes malfunction in CSS drop-down menu

I have been struggling for hours to make this drop-down menu work, but it just won't cooperate. I want the list menu .dropdown to appear when you hover over .droptoggle. I'm including the entire page because I can't figure out what's wr ...