Modifying the background color of the selected menu item

Currently, I am working on developing a basic side menu that changes its background color when the user hovers over an item. Additionally, I want the hover effect to remain even after the user clicks on it until they click on another item.

Below is a snippet of my code:

 ul#nav {
    padding: 0;
    margin: 0 0 10px 0;
    -moz-appearance: menubar;
    -webkit-appearance: menubar;
    background-position: left;
    font-size: larger;
}

.mmenu{ 
    width: 190px;
    position: fixed;
}
.mmenu a{
    font-size: 14px;
    font-family: Arial, sans-serif;
    font-style: normal;
    font-weight: bold;
    display: block;
}

.mmenu a:hover{ 
    color: white;
    background-color: #003366;
    font-style: oblique;
    border-top: 0px outset #003366;
    border-bottom: 0px inset #003366;
    border-right: 0px outset #003366;
    border-left: 0px inset ;
}

The hover effect works as expected, but the style does not persist when clicking on any item. I have tried using `active` and `visited`, however, without success. Does anyone know what could be causing this issue?

Here's a part of the HTML code:

<div id="nav" class="mmenu" style="border: 2px double #f2f2f2; left: 25px; width: 200px; border-radius: 6px;">
    <ul type="none">        
        <li><a href="another example.php">Messages</a></li>
        <li><a href="example.php">My Conferences</a></li>
    </ul>
</div>

Answer №1

Simply insert the following line of code:

Onclick="this.style.background='#003366'"

into the menu item selector (".mmenu a")

Answer №2

Are you looking to create a unique style for navigation items that link to different pages? If so, you can add a class to these navigation items and customize their appearance using CSS.

<style>
body.page-name-1 #nav li.page-name-1 a, body.page-name-2 #nav li.page-name-2 a
{
    background-color: #003366;
}
</style>

<body class='<?php echo $page_name ?>'> 
<!-- Use this PHP code if each page has a specific name stored in $page_name variable -->
<div id='nav' class='mmenu'>
<ul>
<li class='page-name-1' ><a href="another example.php">Messages</a></li>
<li class='page-name-2' ><a href= "example.php">My Conferences</a></li>
</ul>
</div>
...
</body>

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

Adjusting the height of an Ionic list item when the text overflows is not functioning properly

Snippet of Code: <ion-header-bar align-title="center" class="bar-positive"> <div class="button button-clear" ng-click="closeGlobalTaskModal()">Cancel</div> <h1 class="title">Add Global Task</h1> <div class="bu ...

Checkbox loses its check mark when window is resized

My solution for toggling the sidebar visibility on mobile and tablet devices using CSS3 checkboxes is working perfectly. However, I encountered an issue where resizing the browser window causes the sidebar to become unchecked, resulting in the navigation b ...

What to do when JavaScript fireEvent isn't working as expected?

I've hit a roadblock while working on my JavaScript rendition of the classic game Battleship for a school project. I'm currently stuck on devising an AI for the opponent player. I have set up an event listener for when a cell is clicked on the pl ...

Issues with Safari browser's HTML overflow-x not functioning as expected

I'm having trouble with this code, it's not behaving as expected on Safari. Any ideas on how to fix it? html { overflow-x: hidden; } The overflow-x: hidden property isn't functioning correctly for me. ...

Bootstrap navigation bar collapsing horizontally instead of vertically

My Navbar is collapsing correctly, but when I click the toggle icon after it's collapsed, the items appear in a horizontal row instead of the intended vertical block. I have checked the code on the Bootstrap 5 example page and tried to replicate it, b ...

What is the best way to effectively incorporate Ruby into the CSS attribute of a HAML %li element?

Greetings everyone, I am new to the world of development and seeking guidance from experienced individuals. I have been trying to solve a coding issue for quite some time now. I am currently enrolled in a programming course at Code Academy based in Chicago ...

Animate hovering over a specific element within a group of similar elements using jQuery

Hi there! I recently started exploring Js and jQuery, and I was able to create a cool width change animation on a div when hovering over another. However, I ran into an issue when trying to implement this with multiple sets of similar divs. Whenever I hove ...

Outlook's limited width is causing images to exceed the boundaries of the HTML email

I have a collection of images that display perfectly in Gmail, but when viewed in Outlook, all five images appear within a single <tr> and cause the table width to expand. My desired layout is to have the first four images centered on the first line ...

Step-by-step guide to displaying the dropdown menu button content at the top of the page in main.html by using the frameset tag

The layout of my main.html involves displaying menu.htm and welcome.htm using frameset. One issue that arises is with the drop-down menu buttons "Admin..." and "Scheduler...". When hovering over these buttons, the dropdown content does not display properly ...

Desynchronization of jQuery

Incorporating a slideshow and a function to modify the appearance of four boxes, the following code is utilized: jQuery(document).ready(function() { jQuery('#fp-slides') .cycle({ fx: 'fade', speed: '2500&a ...

Adjusting SVG Size for Mobile Devices

How do I make my SVG Logo responsive on mobile devices? Check out my Fiddle and find the code below: body { background:black; } @media screen and (max-width: 500px) { svg { width:50%; } } <?xml version="1.0" encoding="UTF-8" sta ...

Ways to insert a div element into the HTML display utilizing JavaScript

There's a method I'd like to use to dynamically add a div to the view using JavaScript. I attempted hiding the div on page load and then showing it through JavaScript. However, what ends up happening is that the div content appears briefly on l ...

Exploring the capabilities of Angular 9 with mat-table's multiTemplateDataRows feature to enhance the user experience by enabling simultaneous hover effects on multiple rows

Seeking advice as a newcomer to Angular, I am encountering an issue with mat-table and multiTemplateDataRows. Essentially, each element from my data source is displayed on two rows in the table. When I hover over row 1, only row 1 is highlighted, and the s ...

Using Angular with CSS Grid: Avoid distributing grid items evenly in terms of width

I am working on an app that utilizes a grid with rows and columns. One of the features allows me to dynamically remove rows, causing the remaining items to evenly distribute themselves across the width of the grid. However, I now require a layout similar ...

Stylish grey container with crisp white lettering

Just getting started with HTML and I've been working on a project for my class. Here's what I've come up with: <font style="background-color: grey; color: white; display: block">Black and white copies $.10 per page letter/legal size&l ...

Upgrade jQuery visibility and opacity animations to pure JavaScript for a more lightweight solution

Currently I am in the process of removing jQuery from an older website. However, I have encountered a problem with an animation that is currently being used. I have managed to replicate the opacity fade effect, but I am struggling to smoothly transition t ...

What's causing the HTML body to be fixed at the top of the viewport?

Currently, I am in the process of developing a commandline tool to quickly generate an HTML Bootstrap starter template for my personal use. The main issue I am facing is with the index page, where a basic navigation bar should be displayed at the top, and ...

How can I adjust the brightness, contrast, saturation, and hue in HTML5?

Is there a way to adjust the brightness without turning the image grey? I've been attempting to do so, but only managed to change it to greyscale. My objective is to increase and decrease the brightness of the image. Here is the current code: HTML ...

JavaScript expression not being processed

What could be the reason behind not receiving the value for dish.price, dish,description, etc.? Where am I making a mistake in this code snippet? <!DOCTYPE html> <html lang="en" ng-app> <head> <meta charset="utf-8"> < ...