Change the parent's color when the child is hovered over

head has no background and the color is black. On mouseover, its color changes to white with a black background, so far so good but

When I hover over content, I want to change the color of head. How can I achieve this? Is it possible using only CSS?

 <ul id="meg">
   <li><a href="#">head</a>
    <div>
    <h2>sub head</h2>
    <p><a href="#">content</a></p>
    <p><a href="#">content</a></p>
     </div>
    </li>
 <li><a href="#">head</a>
    <div>
    <h2>sub head</h2>
    <p><a href="#">content</a></p>
    <p><a href="#">content</a></p>
     </div>
    </li>
</ul>

Answer №1

If you're looking to achieve this using JavaScript, I can help guide you in the right direction. Here's a sample code snippet that attempts to address what you may be trying to accomplish:

<!DOCTYPE html> 
<html>
<head>
<script type="text/javascript">  
function customizeListItems(){
   var listItems = document.getElementsByTagName('li');
   for (var i = 0; i < listItems.length; i++) {
        var item = listItems[i];     
        item.firstChild.style.color = 'white';
        item.firstChild.style.backgroundColor= 'black';
    }
}
</script>
</head>
<body>
  <ul id="myList">
    <li>
       <a href="#">Header</a>
       <div>
          <h2>Sub Header</h2>
          <p><a href="#" onmouseover="customizeListItems()">Content</a></p>
       </div>
    </li>
  </ul>
</body>
</html>

Answer №2

It is possible that this method will work. Typically, CSS affects elements after the closing tag, not before it. Therefore, this approach seems to reverse the usual "+" method often used for such purposes.

<style type="text/css">
   #content:hover - #head { background-color: black; color: white; }
  </style>

 <ul id="meg">
   <div id="head">
   <li><a href="#">head</a>
   </div>
   <div id="content">
    <h2>su head</h2>
    <p><a href="#">content</a></p>
    <p><a href="#">content</a></p>
     </div>
    </li>
    </ul>

Answer №3

Did you test out the styling for #meg li a:hover { background-color:#000;color:#FFF; }

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

Printing dynamic data

When the print button is clicked, I need to print dynamically generated data from a table that has an ID. The table data (td and tr) is dynamically generated. I have successfully retrieved the table data and attempted to print everything using window.prin ...

Tips for validating user input in AngularJS without using a form tag

Within a popup, I am displaying HTML content that has been copied from another div and shown in the popup. I need to validate this input field to ensure it is required, and display an error message below the input box. <!-- HTML code for changing zip c ...

I'm having trouble with my code not fitting all screen resolutions. I usually code on a Macbook, but recently switched to a Windows computer and now everything is out of whack

$(function(){ $("#Welcome").typed({ strings: ["PROGRAMMERS / NETWORKERS"], typeSpeed: 60 }); }); html { background-color: mintcream; background-size: 100%; } /* Home */ .homeheader button { background-color: #4CAF450; top: ...

Is it possible for me to utilize a type of CSS variables?

Looking to organize all the CSS code on my website in a specific manner. I want to define the type with details such as size, weight, and color. For example: <h1 bold blue>Hello world</h1 blue bold> So essentially, the CSS file will include: ...

"Encountering a Javascript issue while trying to apply a CSS class to a

Encountering issues in Safari desktop / mobile and Internet Explorer. The error message states: In Safari: TypeError: Attempted to assign to readonly property. IE Edge: Assignment to read-only properties is not allowed in strict mode The problem arises ...

How can I troubleshoot the unresponsive remove div function on my website?

My code is running fine on CodePen (link provided below), but for some reason, it's not working properly in the web browser. I am executing the code from localhost and the button isn't responding as expected. CODE Here is my Visual Studio code ...

What is the best way to load content into Bootstrap tabs?

I would like to incorporate a loading spinner within the bootstrap tabs. When a user clicks on a tab pane link, a loading spinner will be displayed for a few seconds before showing the actual tab content. HTML : <ul class="nav nav-tabs" id=&q ...

Unable to create a clickable button within a CSS3DObject using Three.js

How can I create an interactive button on a CSS3DObject within a cube made up of 6 sides? The button is located on the yellow side, but I'm facing issues with clicking on it. Whenever I attempt to click on the button, the event.target always points to ...

Using an onclick function to increment and decrement values

Can anyone help me figure out how to reverse a function onclick? Here is the function: var theTotal = 0; $('button').click(function(){ theTotal = Number(theTotal) + Number($(this).val()); $('.total').text(theTotal); }); ...

What is the best way to ensure a table is responsive while maintaining a fixed header? This would involve the table scrolling when it reaches the maximum viewpoint, while also keeping

Can anyone help me create a responsive table with a fixed header that scrolls when it reaches the maximum viewpoint without scrolling the entire page? I've tried using box-sizing: border-box; and overflow-x:scroll; but it didn't work. Any suggest ...

Plugin for creating a navigation bar with a jQuery and Outlook-inspired style

Our team is considering transitioning our asp.net forms application to MVC. Currently, we utilize outlook-style navigation with ASP controls such as listview and accordion, using code referenced here. Are there any jQuery/CSS controls available for MVC t ...

difficulties with struts2 user interface elements

Can anyone explain why the s:textfield is being positioned at the bottom when inserted among other HTML elements within a form? And what steps can be taken to resolve this issue? <label><span class="required">*</span>First name</label ...

Four unique classes will each be styled differently using pseudo selectors

Consider using this specific markup - it cannot be altered (Even though it would be simpler to add more classes or IDs). There will always be a maximum of six class "test" elements on the page, so the code can be written without certainty of their surroun ...

Is it possible to implement a custom icon for pagination in Material UI?

Can I use a custom icon for pagination in material ui? Find more information on Material UI Pagination in the official documentation here, and check out the CodeSandbox Demo here I am interested in changing the default left and right chevron icons for na ...

How can I incorporate margins or adjust scaling within dompdf?

After creating a PDF using dompdf with the paper size set to A6, I am experiencing issues when trying to print it on A4 and letter paper sizes. The left and top borders are being cut off. Is there a way to generate a PDF that is compatible with A4, A6, l ...

Arranging Data in an HTML Table Using TableSorter

I have been trying to incorporate sortable columns into my HTML table and decided to experiment with the jquery tablesorter. I have followed the syntax below, but for some reason, my table is not allowing me to sort. Can you help me understand why? &l ...

Ways to update div content following a successful AJAX call

I have a form that utilizes AJAX requests, and when the input fields are not filled correctly and the submit button is clicked, error messages are displayed without refreshing the page. While this functionality works, a problem arises when the form is subm ...

A method to apply a class to the third <li> element using javascript

Can someone help me figure out how to add a class to the third element using Javascript? Here is the structure I am working with: <ul class="products-grid row four-columns first"> <li class="item"></li> <li class="item"></li&g ...

Turn off HTML5 Audio

There are 4 <audio> elements on a webpage. The client has asked for them to be available sequentially. Meaning, the first audio should play, then the rest should remain disabled until the first one finishes, and so on. In addition, they want the au ...

What is the best approach for promoting reusability in Angular: creating a new CSS class or a new component?

I have a div with a set of CSS properties that are essential to my application. These properties will be reused across multiple pages and components. <div style="display: flex; flex-direction: column; height: 100%"> //inner html will vary based on c ...