I implemented the fixed navigation dropdown menu with hover functionality as described in the following steps

When clicking on a navigation item in the dropdown menu, I want to change the background. The issue arises when viewing the menu on screens with a resolution less than 992px, as it becomes difficult to differentiate between the submenus displaying. I need a solution for this problem.

Below is the HTML code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Index</title>
    <!-- bootstrap link -->
    <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
    <!-- Custom css -->
    <link rel="stylesheet" href="css/custom.css">
    <!-- JQuery3.2.1 -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>    
    <!-- Bootstrap  -->
    <script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js" integrity="sha384-b/U6ypiBEHpOf/4+1nzFpr53nxSS+GLCkfwBdFNTxtclqqenISfwAzpKaMNFNmj4" crossorigin="anonymous"></script>
</head>
<body>
<nav class="navbar navbar-expand-lg">
<!-- Navbar content -->
</nav>
<!-- Custom js -->
<script src="js/custom.js"></script>

</body>
</html>

This is the custom CSS written:

.navbar-collapse .navbar-nav .dropdown .dropdown-menu{
    /* CSS styles */
}
/* More styles */

@media only screen and (max-width: 991px) {
   /* Styles for screens less than 992px */
}

/* Additional custom styles */

This is the JavaScript file custom.js:

$(document).ready(function(){
  $(".dropdown-item").click(function(){
    $(this).addClass("customNav");
  });
});

Answer №1

You have mentioned the following:

I am interested in changing the background color of a navigation item when it is clicked

To achieve this, you will need to adjust the active css selector for the a tags as shown below:

.dropdown-submenu a:active { 
    background-color: yellow;
}

For more details, please visit:

https://www.w3schools.com/cssref/sel_active.asp

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 issue with Angular 4 imports not refreshing in a .less file persists

Currently, I am in the process of developing a small Angular project that utilizes less for styling purposes. My intention is to separate the styling into distinct folders apart from the components and instead have a main import file - main.less. This fil ...

Having trouble with saving the image after integrating Ajax into the code

My PHP file upload script simplifies the process, as shown below: <?php $path = 'uploads/'; $file_ext = array('jpg','png','gif','bmp','JPG'); $post_ext = end(explode('.& ...

Developing a personalized loop in handlebars templates

Just starting out with NodeJS and ExpressJS. I'm looking to customize a for loop in order to iterate through data from NodeJS using an index, akin to a non-traditional for loop. Take a look at the code snippet below, extracted from NodeJS, where I re ...

What is the most efficient way to apply a class to the post div every 4 seconds using jQuery?

What is the most effective method to dynamically add a class to each div element with a class of "post" every 4 seconds using jQuery? <div class="34 post"> <img width="311" height="417" src="#" class="#" alt="newspapers" /> <h2><a hre ...

Retrieve a targeted data value from a JSON object based on an array value

Looking at the JSON array and another array provided below. JSON OBJECT { id: 1, name: 'abc', email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dfbebdbc9fb8b2beb6b3f1bcb0b2">[emai ...

I have successfully integrated my custom external JavaScript code with React Router, and everything is working

Assistance Needed! I am working on a project that consists of 4 pages, one of which is the About page. I am using react-router to manage the paths and contents between these pages through their respective links. import React from 'react'; impo ...

Choosing between global and local Node packages can be a crucial decision that affects

Recently, I discovered that Angular 2 is installed globally on my system, but I can't remember when I did that or if it's the recommended setup. It seems redundant since Angular can be defined in each project individually. This situation has me ...

The execution of setRequestHeader in ng-file-upload has encountered an error

Having encountered an issue with ng-file-upload, I have implemented a code similar to the GitHub example. However, upon attempting to upload a file, I am confronted with the following error: Error: Failed to execute 'setRequestHeader' on 'X ...

Display tooltips for Autocomplete dropdown options using Vuetify

Just starting out with Vue.js and Vuetify Issue - Need to display tooltips on each dropdown item in v-autocomplete Resolution - included the v-tooltip component in the item template Code: var app = new Vue({ el: "#app", data: { items: [{ ...

Is there a way to update a JSON key using the "onchange" function in React?

I'm facing an issue. I have a form with two inputs. The first input is for the key and the second input is for the value. I need to update the values in the states whenever there is a change in the input fields, but I'm unsure of how to accomplis ...

Is it normal for Node's console to not display all object properties?

After running some code, I noticed something peculiar. console.log(myURL); It seems that the extension property is missing: console.log(myURL.extension); However, when logging it on its own, the value appears correctly. The URL object was created ...

Having trouble aligning page in the center with flexbox and styled components

Hey there, I'm having some trouble figuring out how to center my page using flexbox within styled components in a Next.js app. Any suggestions or tips? Let me share with you my Blog Component import ReactMarkdown from 'react-markdown' impor ...

The CSS styling feature is not functional within the JavaMail API

After receiving an email sent using the JavaMail API, I noticed that the CSS styles are not being applied and only the HTML content is rendered. HTML CODE <!doctype html> <html lang="en"> <head> <meta charset="u ...

Are there any strategies to bypass the color limitations in Bootstrap 5?

Recently, I began studying bootstrap through a crash course video tutorial. The developer demonstrated using various predefined color classes like danger, warning, and primary. Curious, I wondered if it would be possible to substitute dark with tomato re ...

Explanation of JavaScript code snippet

fnTest = /abc/.test(function () { abc; }) ? /\bchild\b/ : /.*/; I am struggling to comprehend the functionality of this particular javascript snippet. Would someone be able to elaborate on the logic behind this code fragment? ...

Is it possible to adjust the element's position using the code "element.style.top = window.scrollY"?

Recently, I attempted to create a floating input element that would follow the scroll. After much effort, I managed to achieve it and the code is provided below. However, I encountered an issue when trying to change "setText(window.scrollY)" to ...

Utilizing API data to set the state in a React component

In my quest to modify the state of this React component, I have a variable called rankAndTeam that holds the "prints=>" data listed below. My goal is to assign "Washington Capitals" to this.state.teamRank["0"], "New York Islanders" to this.state.teamRan ...

Events for radio buttons in Angular 2

What are the events called in Angular 2 when a radio button is selected or unselected? Is it something like this: <input type="radio" (select)="selected()" (unselect)="unselected()" /> When I click on a radio button in a group, will it trigger sel ...

The perplexing discussion of DOM enclosures within the v8 engine

While studying the design of V8 API structure, I came across a useful document explaining the relationship between isolates, contexts, and more: Link to V8 Binding However, one paragraph in particular has left me confused: It mentions that for compatibili ...

Align an Svg layer within another Svg to the center

I'm attempting a seemingly easy task: placing one svg layer inside another and centering it. In this particular situation, I have a simple arrow (represented by an svg path) that needs to be nested within a rectangle (a square shape in this case). T ...