Changing the structure of divs by using elements from different divs

I'm looking for some help with adjusting the CSS of a div when hovering over certain elements. Here is my current code:

<div class = "container-main">
<div class = "container-inner">
<ul class = "list">
<li>Option 1</li>
<li>Option 2</li>
</ul>
</div>
</div>

<div class = "container1">
<p>Container text</p>
</div>

<div class = "container2">
<p>Container text</p>
</div>

I am trying to change the classes container1 and container2 when hovering over the ul or li within the main container. I currently only know how to do this with sibling containers, but that's not the effect I want.

.container-main:hover + .container1 {background:red;}

Any assistance in achieving this would be greatly appreciated. Thank you!

Answer №1

Unfortunately, it seems that achieving what you desire solely with CSS is not feasible given your current markup hierarchy. However, a solution using JavaScript can easily accomplish this task. Below is a sample code snippet that demonstrates how to add a background-color to .container1 when hovering over .container-main ul:

const list = document.querySelector('.container-main ul')
const container1 = document.querySelector('.container1')

list.addEventListener('mouseenter', () => container1.classList.add('red'))
list.addEventListener('mouseleave', () => container1.classList.remove('red'))
.red {
  background-color: red;
}
<div class="container-main">
    <div class="container-inner">
        <ul class="list">
            <li>Option 1</li>
            <li>Option 2</li>
        </ul>
    </div>
</div>

<div class="container1">
    <p>Container text</p>
</div>

<div class="container2">
    <p>Container text</p>
</div>

Answer №2

Is this the desired outcome? Here is a jQuery code snippet for achieving it:

$( ".container-main li" ).hover(
    function() {
        //perform action while hovering 
    }, function() {
        //perform action after hover has ended 
        $(".container1").css("background", "red");
    }
);

Answer №3

Appreciate all the insights shared here. I was a bit uncertain about its feasibility, but I'm going to attempt restructuring my div hierarchy. While I'm aware that jQuery can accomplish this, I'm tasked with solely utilizing CSS without any additional programming languages.
Once more, thank you kindly. Your input has provided me with a clearer understanding of the adjustments needed.

Answer №4

Using just CSS may not be sufficient for this task. Do you have access to jQuery? Here is a helpful JSFiddle link

$(document).ready(function() {
  $(".container-main li").each(function(index) {
    $(this).hover(
      function() {
        div = ".container" + (index + 1);
        console.log("in_" + div);
        $(div).css("background-color", "red")
      },
      function() {
        div = ".container" + (index + 1);
        console.log("out_" + div);
        $(div).css("background-color", "white");
      });
  })

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

jQuery failing to trigger onClick event for a specific group of buttons

Javascript: <script> $(document).ready(function(){//Implementing AJAX functionality $(".acceptorbutton").on('click', function(){ var whichClicked = this.attr('name'); ...

Can someone explain how to implement an onclick event for a div element inside an iframe using jquery or javascript?

Understanding the HTML Structure <iframe src="#" scrolling="no" id="gallery-frame"> <div class="gv_filmstrip"> <div class="gv_frame"> <div class="gv_thumbnail current"> <img src="images/grandstand-padang-right/1.jpg"> </d ...

CSS overflowing issue causing inability to reach bottom of div with scroll bars not functioning as anticipated

My objective is to create a layout featuring a sticky header and left sidebar, with a non-sticky DashboardBody (the green-bordered box) that can be scrolled through. When scrolling, I want the content at the top to disappear "under" the sticky header. The ...

angular-ui-tab-scroll: Odd spacing between blocks and tabs, each separated individually

Greetings! I would like to express my gratitude for this wonderful library! However, I am encountering an unusual issue when trying to wrap a tabset with tabs that are included separately. This can be done either by adding individual tab elements manually ...

Changing font styles using the jMenu jQuery plugin: A step-by-step guide

I'm having trouble changing the font-family using CSS with jQuery's "jMenu" plugin. Here is the CSS I currently have: .jMenu{ position : absolute; top : 80px; left : 0px; width : 100%; display:table; margin:0; padding ...

Mentioning VARs found on additional pages

I'm currently designing a website. At the moment, I have set up the site to prompt users for their name on the landing page and store it in a string variable. var username = ""; Once users enter their name and click "Enter Site," they are directed ...

Element with adhesive properties alters its color at a particular location

I need the sticky element to change colors at specific points and then revert back to its original color. The color should be orange initially, green on block 2, and orange again on block 3. For the complete code and to address any issues with jQuery, pl ...

How can I implement a jQuery slide down effect for a specific individual instance?

Whenever I click on the "Share your thoughts" button, the comments block slides down for every section instead of just the particular section I clicked on. Is there a way to make it so that only a single block slides down when the button is clicked, regard ...

Is there a way to remove backdrop-filter: blur effect from elements within a div?

Having an issue with a CSS property. I have designed a modal that includes inputs and labels, and I want to blur the background content. However, the problem I am facing is that the blur effect is also being applied to all elements inside the container, in ...

Inserting HTML content into a DIV with the help of jQuery

Looking for a solution with my index.html file. I want to load other HTML files into a DIV by clicking on buttons. Here's an example of what I'm trying to achieve: I've searched through various examples online, but none seem to work for ...

Height of inline-block list items in ul is not properly adjusted

I am working on a horizontal navigation bar using inline-block for the li tags. Here is the code snippet: <ul> <li><a href="#">HOME</a></li> <li><a href="#">FEATURES</a></li> <li><a href ...

Center Align Images in HTML List Items

Hey there! I'm currently diving into the world of web development with responsive design, but I'm still a bit of a newbie. So please bear with me as I try to explain my issue in detail. If you need more information, just let me know! My current ...

JavaScript array images are not showing when using the img tag

For this module, I am working on creating a flipbook (magazine) using images stored in a JavaScript array. However, I am facing an issue where the images are not loading up properly. Instead of displaying the image, it shows as [object" htmlimageelement]=" ...

Creating an accessible order for the next and back buttons

Currently, I am working on designing the user interface for a checkout flow. Towards the end of the page, there are 3 buttons available - Next, Back, and Cancel. When viewed on a desktop, the button layout appears as follows: (Back) (Next) Cancel http ...

Can you navigate to HTML files using Vue router?

Hey there! I'm currently utilizing VueJS along with the webpack template. I've got a variety of components that I can easily showcase with Vue Router. However, the testing framework utilized by my team is Robot Framework and we typically create a ...

Issue with enabling overflow-y: scroll within a Bootstrap Modal

I've implemented a modal using the Bootstrap library that contains multiple lists populated through Ajax search requests. These lists have a fixed size and utilize the CSS property overflow-y: scroll to accommodate a large number of elements without a ...

Mobile responsiveness malfunctioning

I recently launched my first responsive design website, which you can view here After testing the responsiveness on Chrome and FF by resizing the window, everything seemed to be working fine. However, when I checked it on my phone, the layout appeared ide ...

Positioning CSS icons beside text

I'm having trouble aligning a close button I created in CSS with regular text. Although the containing span element seems to align with the adjacent text, it doesn't align properly with the child divs. <a class="event-filter button" href="#"& ...

A problem arises in Next.js when CSS is not rendering properly during Server Side Rendering

After creating my next.js application using the command npx create-next-app, I realized that the styles from the imported .css files are rendering correctly on Client Side Render but not on Server Side Render. The Next.js documentation states that importe ...

Is it possible to dynamically set the maximumSelectionLength in a select2 dropdown?

In my HTML, I have two select elements. One is for multiple selection of items in a dropdown and the other is for adding tags. If the number of selected items is 3, then users should only be allowed to add 3 tags - no more and no less. $(".js-example-b ...