When I hover over one div, I aim to alter the background color of another div, but unfortunately, the change

Is there a way to change the color of #bg when hovering over any div element with an id of symbol in a container?

<div id="container">
    <div id="symbol1"></div>
    <div id="symbol2"></div>
    <div id="symbol3"></div>
    <div id="symbol4"></div>
</div>
<div id="bg"></div>

I attempted to use the CSS selector below, but it did not work. I also tried using +, >, and just whitespace, with no success.

#symbol3:hover ~ #bg{
    background-color:red;
}

Unfortunately, this approach did not achieve the desired result.

Answer №1

One efficient method to achieve this is by utilizing the :has() selector in combination with the target attribute selector [attribute*="value"]. This specific selector targets elements that contain a substring value within their specified attribute.

#bg {
  /* styling for #bg div */
  width: 100px;
  height: 100px;
  background-color: black;
}

#container:has([id*="symbol"]:hover) + #bg {
  background-color: red;
}
<div id="container">
  <div id="symbol1">1</div>
  <div id="symbol2">2</div>
  <div id="symbol3">3</div>
  <div id="symbol4">4</div>
</div>
<div id="bg"></div>

#container:has([id*="symbol"]:hover) + #bg
essentially looks for the presence of an #container element with an id containing the term symbol, while it is being hovered over, and subsequently selects the following #bg element.

For further information on CSS selectors, please refer to: w3schools CSS Selectors Reference

Answer №2

The problem with the code lies in what disinfor has mentioned.
By relocating the #bg div outside of, but before the #container div, you can implement the following solution.
The :has() selector targets a #bg element that is immediately followed by a #container element containing a hovered div#symbol3.
If altering the HTML structure isn't an option, it seems like JavaScript may be needed.

#bg:has(+ #container div#symbol3:hover) {
  background-color: red;
}
<div id="bg">0</div>
<div id="container">
  <div id="symbol1">1</div>
  <div id="symbol2">2</div>
  <div id="symbol3">3</div>
  <div id="symbol4">4</div>
</div>

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

Steer clear of utilizing CSS pseudo-elements like :before and :after

I am seeking a solution to hide a <label> when viewing on a small mobile device. Below is the HTML code: <label class="page_createpassword_label" for="page_createpassword"> <span class="page_label_main">Span Text</span> <span c ...

Tips for including HTML tags in strings without causing issues with nested tags

I am struggling with a string that contains both text and HTML tags, specifically <a href=""></a> tags. My goal is to apply the "i" tag around the text outside of the "a" tags without creating nested tags like: <i><a href=""></a ...

Spot the columns generated by CSS and incorporate a new column using JavaScript

Exploring a column newspaper-like layout has led me to wonder if there is a way to use CSS or JavaScript to identify columns (or column breaks) and dynamically insert content between them with JavaScript. The concept involves having a layout with 100% hei ...

JS switch for numerous container elements

How can I create a toggle slider in HTML for multiple elements displayed using foreach? Each element should open a div block with specific information and have a close button. Unfortunately, I haven't been able to find any suitable code to achieve thi ...

Is the PNG image displaying poorly in the IE10 browser?

My application features a PNG image with actual dimensions of 300px X 300px. I am using a 20% scale of this image as an input image button, which displays correctly in Mozilla and Chrome browsers. However, in IE, the image appears distorted. Please refer t ...

Determining the margin-left value of a centrally positioned table

I have a table with variable columns, meaning the columns of the table are dynamic. The number of columns will keep changing, causing the table to be centralized and the margin-left value to adjust accordingly. Now, I need to calculate the margin-left val ...

Unable to Publish HTML File

I am stumped as to why this particular file is not getting posted. Despite my thorough search for solutions, I have yet to pinpoint the issue. All the necessary ini file variables seem to be in order and I have verified the correct form type. Additionally, ...

Wagtail Django TableBlock not showing up properly on the webpage

Recently delving into the world of Django and programming, I decided to incorporate Wagtail into my website. However, I've encountered a roadblock with the 'TableBlock' functionality Despite setting up the model and block correctly, I&apos ...

Refresh a partial view in Rails using AJAX with JSON feedback

Whenever I make a call to $.getJSON, my goal is to refresh the tbody elements. This means that I need to remove all current elements and replace them with new elements based on the JSON response. Javascript $(document).ready(function(){ $('#positi ...

Submitting data with a checkbox and dropdown menu

Currently, I am working with a HTML table where rows are generated dynamically inside a for loop. Each row consists of a checkbox with a unique value but the same name ("checkboxes"), and at the end of each row there is a select element also with the same ...

The .css() method does not apply any styles to the elements in the document

Having trouble with a jQuery function: .css() This is the HTML code in question: <div class="..." id="about"> // OTHER HTML CODE </div> And here's the corresponding jQuery code: (function($, window, document) { $(function() { ...

Tips for implementing Wordwrap on List Items within a Flexbox Setup

Here's a current look at the elements related to my query: Flexbox Layout I'm aiming to have the text inside the unordered-list elements wrap to the next line if it's too long. Currently, the entire list moves to a new line (refer to the 2n ...

Is it possible to set all UI forms to a readonly/disable mode?

We have a specific requirement where, if the user's access level is set to "READ ONLY", all form input elements should be made readonly. Our coding approach involves using a template HTML that contains widgets which are referenced in the correspondin ...

Tips for displaying the initial slide when a tab is loaded on a multi-tab webpage

I have a total of four tabs, with the first tab containing a slideshow. On page load, I am successfully triggering the first tab, but for some reason, the first slide in the slideshow within that tab is displaying a blank image. I'm struggling to find ...

Struggling to locate the origin of this mysterious border

After thoroughly inspecting and manipulating CSS, I am still unable to determine the source of this mysterious border... Mysterious Border Image - The Origin Unveiled Could someone take a look and provide some insights? It seems like the border is encom ...

CSS. Absolute positioning in relation to a div element

I discovered a way to adjust the positioning of a div using jQuery. Check out this example. Is it possible to achieve the same result using CSS? UPDATE: I came across this solution. I also want the horizontal window scrollbar to appear if the fixed elem ...

The transparency of an image is being lost when it is used in a modal

I am facing an issue when trying to append a modal that contains only a transparent gif. The problem arises when the transparent background of the gif is not displayed properly. There seems to be no issue with the transparency settings of the gifs themselv ...

Is it possible to scroll the grid horizontally?

I have a grid that scrolls vertically, with columns adjusting their width automatically. .grid { display: grid; overflow-y: auto; grid-template-columns: repeat(auto-fit, minmax($min-column-width, 1fr)); } Now, I'm attempting to create a horizon ...

Managing a PUT request received from an HTML form utilizing the Bottle framework

Currently utilizing Bottle, I am facing the challenge of handling a PUT request from HTML to update a nested dictionary. While front-end validation is an option, it is necessary for me to complete this task for an assignment. Below is the dictionary conta ...

What is the best way to display items from top to bottom in React?

Currently, I am working with a list of objects structured in this way: const items = [{name: 'some name', count: 'how many of that name'}, ...] My goal is to display them in the following format: {items.map((item) => ( ...