I have a question about CSS selectors: How can I change the font color of a parent <li> element when the child

After struggling for hours, I'm finally throwing in the towel on this issue.

I have an unordered list with no background at the top level and text color of #fff. When rolled over, the background turns #fff and the text color turns #000. The child ul elements have a background of #fff and text color of #000.

My challenge is styling the text color change at the top level when the child ul elements are open, but the top-level links are not being moused over. I can't seem to keep the top level li's text color at #000 (although I got the background color to stay) as soon as I move my mouse off the top-level links to a child ul, the top level text reverts to #fff.

<div class="menu">
<ul>
<li><a href="#">Home</a></li>
<li>About
<ul class="children">
<li><a href="#">About Us</a></li>
</ul>
</li>
</ul>
</div>

.menu ul li{
color: #ffffff;
text-decoration:none;
}
.menu ul li:hover{
color:#000000
background-color:#ffffff;
}

.children{
background-color:#ffffff;
color:#000000
}

This is the markup that I have come up with as a starting point. The menu ul li has no background with text color #ffffff. When the "About" link is rolled over, the background turns #ffffff, and text turns #000000. The dropdown shows up and reveals the .children ul/li's, which have the same background and text color. However, as soon as the user moves their mouse off of the parent link, the parent text goes back to white on a white background while the children retain the white background.

I want the font to turn black with a white background as long as the user hovers or clicks on a child element.

Answer №1

To effectively handle child clicks in jQuery, utilize the Parents function.

Upon detecting a click event, navigate to the parent element that contains a specific class.

Apply the desired style to the identified parent element.

Explore this code snippet for a working example.

Answer №2

To efficiently style multiple groups of elements, consider assigning a common class to each set and utilize jQuery to apply CSS dynamically. For example, for menu1, menu2, etc., you can implement the following script:

$(".menu_1").hover(
   // on mouseover
  function () {
    $(".menu_1).css("background-color", "red");
  }, 
   // on mouseout
  function () {
    $(".menu_1).css("background-color", "white");
  }
);

Answer №3

Your CSS code needs to include a semi-colon for proper syntax:

.menu ul li:hover{
   color:#000000; <!-- don't forget to add this semi-colon -->
   background-color:#ff0;
 }

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

Is it possible to assign the margin-bottom property of one element to be equal to the dynamic height of a sibling element?

I am in the process of creating a website that features a fixed (non-sticky) footer placed behind the main content using the z-index property. The footer only becomes visible when the user scrolls to the bottom of the page, achieved by assigning a margin-b ...

Ways to verify if JSON.parse fails or yields a falsy outcome

Objective: const jsonData = JSON.parse(this.description) ? JSON.parse(this.description) : null When executing the above statement, my aim is to have the ability to parse a value successfully and return null if parsing fails. However, instead of achieving ...

Having trouble with AngularJs 1 functionality?

Having trouble fetching data from a JSON file. I've tried all available options, but nothing seems to be working. Not sure if I need to import something else. The JSON file is located at the same level as the index file. Any help would be much appreci ...

What is the best way to ensure an input field and a button are aligned perfectly within the same div tag and of equal height?

During a recent HTML-CSS project, I encountered an issue where I struggled to ensure that two elements within a div tag were the same height. The elements in question were an input field and a button with an icon. Here is a snippet of the relevant HTML cod ...

What are the steps to incorporate PointerLockControl in Three.js?

Having trouble correctly integrating the PointerLockControl in Three.js? Despite trying various examples, errors seem to persist. I've been importing libraries through the head part like this: <script src="lib/controls/PointerLockControls.js"> ...

How can I extract text within a specific HTML tag using Selenium?

On my second day of learning Selenium, I am looking to extract text from within specific HTML tags. Here is a sample of the HTML code: <div id="media-buttons" class="hide-if-no-js"/> <textarea id="DescpRaw" class="ckeditor" name=" ...

JavaScript: Targeting elements by their tag name within a designated container

When working with PHP, it is possible to use the getElementsByTagName method on any DOM object. However, this concept does not seem to exist in JavaScript. For example, if we have a specific node stored in the variable detailsNode, attempting to use detai ...

Tips for including multiple spaces within a cell of a table

I've come across an issue while working on a table and attempting to insert text with multiple spaces. For example, I want to add the text HELLO<1stSPACE><2ndSPACE>WORLD. However, when I enter it as HELLO WORLD, it only displays with a si ...

How can I filter an array to retain only the initial 5 characters of every element?

I need help using the .map function to transform this array so that only the first 5 characters are displayed, like "01:00", "02:00"? This is the array: ["01:00:00 AM", "02:00:00 AM", "03:00:00 AM", "04:00:00 AM", "05:00:00 AM", "06:00:00 AM", "07:00:00 ...

The minified version of Bootstrap's CSS will only be loaded when I explicitly import it in my index

I used to rely on BootstrapCDN for my styles, but now I want to download the files and use them locally. However, when I try to load the styles without an internet connection, they don't seem to work properly, especially the grid layout. My current s ...

A guide on incorporating dynamic formControlName functionality into AngularJs2

Currently, I am building forms using the form builder in AngularJS2. My goal is to incorporate the formControlName property/attribute into the form element as shown below: <input type="text" formControlName={{'client_name' + i}} placeholder=" ...

Implementing a callback in the app.get function within Node.js: A step-by-step guide

I have a fully functioning website and now I am looking to add some logic and data analysis to it. Below is the code snippet for rendering my /data page: app.get("/data", (req, res) => { const sql = "SELECT * FROM MyMoods"; cons ...

Setting nodeIntegration to false led to an Uncaught ReferenceError: require is not defined when trying to access Object.url (external "url":1) in the electron-react-typescript environment

After setting nodeIntegration to false, I encountered the following error message: "Uncaught ReferenceError: require is not defined at Object.url (external 'url': 1)". https://i.sstatic.net/galzh.png Upon clicking on the link referring to "exte ...

Is it possible to search LinkedIn using just one keyword?

Is it possible to search for a LinkedIn profile using a single string instead of separate first and last names? The issue is that I only have a single name field in my database... Typically, one would construct a dynamic URL like this: http://www.linkedin ...

Organizing classes under namespaces in Node.js

Currently working on a project in node.js. I have several related classes that I want to organize in a subdirectory, with one class per file structured like this: lib/ main.js melons/ casaba.js cantaloupe.js carnegie.js I plan to us ...

"Can someone guide me on the process of transmitting data to a client using Node in combination with

I am new to web development and struggling to understand how to transfer data from the Node server to the client while also displaying an HTML page. I am aware that res.send() is used to send data, but I'm having difficulty maintaining the client disp ...

Error: Material-UI prop type validation failed. Please specify either children, image, src, or component prop for CardMedia component. This error occurred at

I encountered an issue while trying to utilize the CardMedia component from Material-ui. The error message received was 'Failed prop type: Material-UI: Either children, image, src, or component prop must be specified. at CardMedia'. I attempted v ...

Pressing the button will activate the Ctrl+z and Ctrl+y key commands

I have created two separate buttons for triggering actions equivalent to pressing Ctrl+z and Ctrl+y. I am attempting to make these actions occur with the click of a single button. However, when trying to set up the functionality to trigger Ctrl+z and Ctr ...

Troubleshooting issue: Unable to edit images using PHP and jQuery ajax

I have been attempting to insert text onto my image using jQuery ajax, but I am encountering strange text in the ajax response and not getting the desired image output. https://i.sstatic.net/AGx5d.jpg Snippet of jQuery Ajax Code: $(document.body).on(&ap ...

Troubleshooting a live chat box for CSS alignment issues

Need help with aligning a live chat box on every webpage? Check out this example for reference: You can find my code here: https://jsfiddle.net/fn77d0de/2/ ...