Switch the background image of one div when hovering over a different div

Can anyone assist me with creating an interactive map where continents light up as you hover over them? I am using multiple images within div elements in order to achieve this effect. Specifically, I want to change the background image of one div when hovering over certain areas of the background.

I have implemented the tilde selector in my CSS code but it doesn't seem to be working properly. Can someone help me troubleshoot this issue?

Here is the HTML Code:

<html>
<div class="map">
 <div class="americas">
 </div>

 <div class="emeari">
 </div>

 <div class="emeari2">
 </div>

 <div class="ap">
 </div>
</div>
</html> 

And here is the CSS Code:

 .map {
  position:fixed;
  background-image: url(World_Map.jpg); 
  width:960px;
  height:540px;
 }


.americas {
  position:absolute;
  width:364.8px;
  height:540px;
  border: 1px solid black;
}

.emeari {
  position: absolute;
  width: 297.6px;
  height: 540px;
  left: 364.8px;
  border: 1px solid black;
}

.emeari2 {
  position:absolute;
  width:297.6px;
  height:183.6px;
  right: 0px;
  border: 1px solid black;
}

.ap {
  position:absolute;
  width:297.6px;
  height:356.4px;
  bottom: 0px;
  right: 0px;
  border: 1px solid black;
}


/* Hover */

.americas:hover ~ .map{
  background-image: url(Americas_Map.jpg);
}

.emeari:hover ~ .map{
  background-image: url(Emeari_Map.jpg);
}

.emeari2:hover ~ .map{
  background-image: url(Emeari_Map.jpg);
}

.ap:hover ~ .map{
   background-image: url(Asia_Map.jpg);
}

Thank you for your assistance!

Answer №1

The ~ selector is known as the sibling selector, specifically targeting elements at the same level. However, in your code, the .map element serves as the parent and there isn't a direct "parent" selector in CSS.

To resolve this issue, you can restructure your code by nesting the .map within a container element.

.map,
.map-wrapper {
  position:fixed;
  width:960px;
  height:540px;
}

/* Additional CSS rules for various map sections */

/* Hover effects on map sections */

.americas:hover ~ .map{
  background-image: url(Americas_Map.jpg);
}

.emeari:hover ~ .map{
  background-image: url(Emeari_Map.jpg);
}

.emeari2:hover ~ .map{
  background-image: url(Emeari_Map.jpg);
}

.ap:hover ~ .map{
  background-image: url(Asia_Map.jpg);
}
<div class="map-wrapper">
   <div class="americas"></div>

   <div class="emeari"></div>

   <div class="emeari2"></div>

   <div class="ap"></div>
   
   <div class="map"></div>
</div>

Answer №2

Regrettably, CSS does not have a parent selector feature at the moment. However, this functionality can be achieved through JavaScript. In the following example, jQuery is utilized to modify the background color of the parent element (the background image would need to be updated).

VIEW WORKING FIDDLE

HTML:

<div class="map">
 <div class="americas">
 </div>

 <div class="emeari">
 </div>

 <div class="emeari2">
 </div>

 <div class="ap">
 </div>
</div>

CSS

.map {
  position:fixed;
  background-image: url(World_Map.jpg); 
  width:960px;
  height:540px;
 }


.americas {
  position:absolute;
  width:364.8px;
  height:540px;
  border: 1px solid black;
}

.emeari {
  position: absolute;
  width: 297.6px;
  height: 540px;
  left: 364.8px;
  border: 1px solid black;
}

.emeari2 {
  position:absolute;
  width:297.6px;
  height:183.6px;
  right: 0px;
  border: 1px solid black;
}

.ap {
  position:absolute;
  width:297.6px;
  height:356.4px;
  bottom: 0px;
  right: 0px;
  border: 1px solid black;
}

JS

$(".americas").hover(function () {
    $(this).parent().css("background", "red"); // MODIFY IMAGE PROPERTY HERE
});

$(".emeari").hover(function () {
    $(this).parent().css("background", "blue"); // MODIFY IMAGE PROPERTY HERE
});

$(".emeari2").hover(function () {
    $(this).parent().css("background", "green"); // MODIFY IMAGE PROPERTY HERE
});

$(".ap").hover(function () {
    $(this).parent().css("background", "orange"); // MODIFY IMAGE PROPERTY HERE
});

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

Table does not have appropriate rowspan and colspan attributes

I've created a table using bootstrap, but it's not displaying as intended. I used row-span and col-span to format the table, and while most rows are correct, some are not appearing on the page. To better illustrate the issue, I have included an ...

The issue at hand is that one of the JavaScript buttons is functioning properly, while the other is not playing the video as intended. What steps can

I've been working on a script for my school project website that should make buttons play videos, but I'm running into an issue where it only works for the first button (btn). Programming isn't my strong suit, and I put this together based o ...

Could you please provide instructions on how to manipulate the :root CSS variable using JQuery?

Is there a way to update the :root css variable using jquery? :root { --color: #E72D2D; } .box-icon { background-color: var(--color); } I tried but it doesn't seem to work $("#blueColor").click(function(e) { $(":root").css("-- ...

Which CSS properties can I implement to ensure that the text remains legible regardless of the background behind it?

Looking ahead, I am displaying an issue where the colors are almost perfect, but when different percentages are applied, some or all of the text becomes obscured. My goal is to assign the font color based on the background difference. I vaguely remember s ...

jQuery, trigger a function when the document has finished loading

I have created the following code in an attempt to display a message to only Internet Explorer users visiting the page: <script src="jquery.reject.js"></script> <script> $(document).ready(function() { $.reject({ reject: { a ...

Unable to retrieve input using jquery selector

I am attempting to detect the click event on a checkbox. The Xpath for the element provided by firebug is as follows, starting with a table tag in my JSP (i.e. the table is within a div). /html/body/div[1]/div/div/div[2]/div/div[2]/div[1]/div/div[2]/table ...

Slow rotation in CSS styling

.badge { -webkit-transform-style: preserve-3d; -webkit-perspective: 1000; position: relative; } .back, .front { position: absolute; -webkit-backface-visibility: hidden; -webkit-transition: -webkit-transform 1s ease-in; width: ...

The capability to scroll within a stationary container

Whenever you click a button, a div slides out from the left by 100%. This div contains the menu for my website. The problem I'm encountering is that on smaller browser sizes, some of the links are hidden because they get covered up. The #slidingMenu ...

What could be the reason for the absence of the loading sign in Chrome, even though it appears when the code is run on Firefox?

I implemented a function to display a loading screen on my HTML page with Google Maps integration. However, when I called the function popUpLoadingScreen() and dismissLoadingScreen() to show and hide the loading message while rendering map markers, the loa ...

SyntaxError: Unexpected '<' symbol found in JavaScript file while attempting to import it into an HTML document

This issue is really frustrating me In my public directory, there is an index.html file Previously, I had a newRelic script embedded within the HTML in script tags which was functioning properly Recently, I moved the script to a separate JavaScript file ...

"Running experiments with Google Ads and Google Analytics scripts results in a blank page being displayed

Currently, I am working on a plain HTML file and conducting tests to ensure that the Google Ads and Analytics scripts are functioning correctly before implementing them on other pages. A colleague who has an AdSense account provided me with the script code ...

Is there a way to verify the results of a Python script within a PHP webpage?

For my school project, I am creating a PHP website where I want to implement a Python code Quiz. I envision a scenario where users can input Python code in an on-page editor/IDE and the output is checked automatically using PHP If-function to determine cor ...

If I dared to eliminate the emphasized line, this code would completely fall apart

<!DOCTYPE html> <html> <head> </head> <body> <h1 id="message-el">Ready to play?</h1> <p id="cards-el"></p> <p id="sum-el"></p> <butto ...

Various image resolutions designed for extra small, small, medium, and large screens

My image has a relative width and currently the src points to a single, large file (approximately 1000px). The issue arises on small devices with Opera and IE where the browser scaling results in pixelated images. Conversely, using a smaller image leads to ...

Personalized AngularJS required text

After utilizing the built-in AngularJS directive required and encountering a validation error, I receive a small popup near the field displaying "Please fill out this field". My issue lies in needing the text to be displayed in another language. How should ...

Using SVG Mask to enhance shape Fill

I am having trouble achieving the desired effect of darkening the fill of objects based on a specified gradient. Instead, when the mask is applied over the fill, it actually lightens it. I suspect that this issue arises from the color blending method being ...

Tips for deleting directory path from a file name

Similar Question: How to extract file name from full path using PHP? echo '<td width="11%" class="imagetd">'. ( ( empty ($arrImageFile[$key]) ) ? "&nbsp;" : htmlspecialchars( is_array( $arrImageFile[$key] ) ? implode(",", $arrImag ...

The default action is not triggered when the click event occurs

Hey there, I have been working on this <ol> list of events using jQuery (version 1.4.2). Everything is set up within the $(document).ready() function. Something strange is happening where when I click on the <li>, it triggers a click on the co ...

What is the best way to post an image using nodejs and express?

Recently, I've been working on a CMS for a food automation system and one feature I want to incorporate is the ability to upload pictures of different foods: <form method="post" enctype="multipart/form-data" action="/upload"> <td>< ...

Can a JavaScript function be used with images to operate across multiple elements?

How can I make a function work on multiple elements? let image = document.getElementById("opacityLink"); image.onmouseover = function() { image.style = "opacity:0.9"; }; image.onmouseout = function() { image.style = "opacity:1"; }; <div class=" ...