Styling your navigation with a CSS background

Having trouble creating a custom CSS navigation for my website. Below is the code snippet of my attempt at a vertical navigation:

<style type="text/css">

/* Custom stylesheet */
#cssmenu > ul {
list-style: none;
margin: 0;
padding: 0;
vertical-align: baseline;
line-height: 1;
}

/* Container styling */
#cssmenu > ul {
display: block;
position: relative;
width: 150px;
}

/* List elements containing links */
#cssmenu > ul li {
    display: block;
    position: relative;
    margin: 0;
    padding: 0;
    width: 250px;   
}

/* Link styles */
#cssmenu > ul li a {
        /* Layout */
        display: block;
        position: relative;
        margin: 0;
        border-top: 1px dotted #3a3a3a;
        border-bottom: 1px dotted #1b1b1b;
        padding: 11px 20px;
        width: 210px;

        /* Typography */
        font-family: Helvetica, Arial, sans-serif;
        color: #d8d8d8;
        text-decoration: none;
        text-transform: uppercase;
        text-shadow: 0 1px 1px #000;
        font-size: 13px;
        font-weight: 300;

        /* Background & effects */
        background: #282828;
    }

    /* Hover state styling */
    #cssmenu > ul li>a:hover, #cssmenu > ul li:hover>a {
        color: #000;
        text-shadow: 0 1px 0 rgba(0, 0, 0, .3);
        background: #A9CA6F;
        background: -webkit-linear-gradient(bottom, #A9CA6F, #B4D876);
        background: -ms-linear-gradient(bottom, #A9CA6F, #B4D876); 
        background: -moz-linear-gradient(bottom, #A9CA6F, #B4D876);
        background: -o-linear-gradient(bottom, #A9CA6F, #B4D876);
        border-color: transparent;
    }
    
</style>

Struggling to make the clicked link change its background to #A9CA6F. I want the <li> with an active id/class to maintain this color. Any insights?

Thanks in advance!

Feel free to check out the demo on JSFiddle here.

Answer №1

Include the following code...

#cssmenu li.active a {background-color:#A9CA6F;}

It seems that adding the background color to the a element and targeting it specifically should do the trick. I've provided a link to a fiddle where I made this adjustment in your CSS. Ensure you are specific enough in targeting the element so that it overrides the default style.

UPDATE

To clarify, based on your existing CSS:

#cssmenu > ul li a {...}

In order to target the active class, your selector needs to be even more specific than what you currently have. Otherwise, the rule above will take precedence over all others. Therefore:

#cssmenu > ul li.active a {...}

Instead of...

li.active a  {...}

For instance, in a hypothetical CSS battle...

  1. #menu a would defeat a.active
  2. a.active would trump just a
  3. #wrapper #menu a would surpass #menu a

:-) Hope this helps!

Answer №2

To implement this style, use the following code:

#navigation > ul li.active a{
    background-color: #A9CA6F;
}

See it in action

Answer №3

If you need to navigate within the same page using JavaScript, you can try this jQuery solution:

$("#menu li").click(function(){
    $("#menu li").removeClass('active');
    $(this).addClass('active');
});

Check out this jsfiddle example

This code will move the CSS class ".active" to the clicked li element. You can then style the active link in your CSS with a selector like this:

#menu > ul li.active a

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

Tips for playing a video uploaded directly into Vue-plyr

My attempt to integrate vue-plyr with a YouTube video was successful using the following code: <template> <section id="vision" class="vision"> <vue-plyr> <div data-plyr-provider="youtube" data- ...

Changing the color of a checkbox in React JS when it is checked: a step-by-step guide

Is there a way to change the color of the checkbox when it is checked? Right now, my checkbox appears with this color: https://i.sstatic.net/mY9Jg.png However, I am looking to change it to red. Any suggestions on how I can achieve that? <input ...

Utilizing jQuery Validate to individually verify different sections of a form

I am currently exploring how to integrate jQuery validate into my questionnaire form. The form is divided into sections (divs) that are hidden and revealed using jQuery for a cleaner layout. My goal is to validate one section or specific fields at a time b ...

Set the datepicker to automatically show today's date as the default selection

The date picker field is functioning correctly. I just need to adjust it to automatically display today's date by default instead of 1/1/0001. @Html.TextBoxFor(model => model.SelectedDate, new { @class = "jquery_datepicker", @Value = Model.Selecte ...

I would like to split the window into four columns and populate each one with images

I've been struggling to create 4 columns in my design, but I just can't seem to make it work. I've tried setting the height to 100% in each div, but it didn't produce the desired result. I was able to achieve it with colors, but not wit ...

The relationship between parent and child elements in CSS

Recently on Stack, I came across a question that was answered correctly. The query was about how to target the first two li elements after each occurrence of .class1. <ul> <li>Something</li> <li class="class1">Important</li ...

Clicking on a jQuery element will reveal a list of corresponding elements

I've retrieved a list of elements from the database and displayed them in a table with a button: <a href="#" class="hiden"></a> To show and hide advanced information contained within: <div class="object></div> Here is my jQ ...

What is the best method to check BNB token balance in a React application using the Metamask wallet

I just wanted to share a snippet of code that I've been working on: const getBalance = (userAddress: String) => { const provider = new Web3((window as any).web3.currentProvider); const bnbContract = new provider.eth.Contract(bnbTokenAbi ...

Use the css file specifically for iPad devices

My goal is to load a specific CSS file only for iPads. I attempted the following approach: <link href="/css/ipad.css" rel="stylesheet" media="all and (max-device-width: 1024px)" type="text/css"> While this method successfully loads the iPad styleshe ...

Is it feasible to utilize the .fadeTo() function in jQuery multiple times?

I need some help with writing a script that will display a warning message in a div tag every time a specific button is pressed. I chose to use the fadeTo method because my div tag is within a table and I want to avoid it collapsing. However, I'm noti ...

utilizing the power of Ajax in Laravel version 5.7

I am seeking assistance in utilizing AJAX to delete and update table rows with a modal in Laravel 5.7. I am new to AJAX and would appreciate any help, especially in explaining how AJAX sends/gets data in the controller and if there are any differences betw ...

Having trouble with compiling SCSS code

While diving into the world of SCSS, I decided to compile my SCSS into CSS using npm. So I entered npm run compile: sass as defined in my package.json as follows: "scripts": { "compile:sass": "node-sass sass/main.scss css/style ...

establishing a CSS class for the containing element of an active route link in Angular version 2 or later

I have a bootstrap 4 navbar that includes a dropdown menu with routes. I am able to show the active route based on 'routerLinkActive', but I also want to style the 'dropdown-toggle' or 'nav-item' as active when one of its chil ...

Getting rid of the 'label' decorator in Zend 2 forms

Here is a basic Radio element example: $form->add([ 'name' => 'account_type', 'type' => 'Zend\Form\Element\Radio', 'options' => [ ...

Ways to ensure <li> elements remain anchored to the top and bottom of <ul> element while scrolling

I currently have a ul with a fixed height that contains a dynamic number of li elements. When there are too many elements, a scrollbar appears. By clicking on a li element, you can select it. What I am aiming for is to have the active li element stay fixe ...

What do you notice about interactions involving 'input type=text' in HTML and JavaScript?

When a new binding is created for the value property on an input, any manual modifications by the user no longer update the value. What happens when the binding is altered? Does regular user interaction involve key press listeners? I've modified the ...

Illustrative Python Django email submission form demo

I am currently working on creating a contact form for my website, but I am struggling to find examples using Django. While PHP email forms are readily available, I don't have the knowledge to create one from scratch using Django. Is there anyone who c ...

Encountering a 422 Unprocessable Entity error when the search bar in the frontend is empty - What is the best way to handle

My frontend setup includes a search bar with an input field By combining HTMX and FastAPI, I am able to dynamically fetch user information from a static list of dictionaries. I aim for dynamic results that update as I type, and the fetching of user infor ...

How to Show Extensive Content in an Android WebView with a Fixed Height

Is anyone familiar with how to make a large HTML page fit completely within a web-view on an Android device without any vertical scroll bars? I want the entire webpage to be displayed within the varying height of the web-view when the user clicks "fill hei ...

Combining HTML code with PHP for seamless integration into a webpage

Can someone assist me with converting HTML to PHP? I'm having trouble with my code and need to use an if statement and echo inside it to determine conditions. The error seems to be coming from the value of the input field that is pulled from the datab ...