Switch backgrounds of various divs upon hovering

I am looking to achieve a design with 4 divs where one is larger and the other three are smaller. Upon hovering over one of the smaller divs, I want the background-image of the large div to change to match that of the hovered small div. Is there a way to accomplish this using CSS alone?

Here's what I have tried so far:

.mar {
    position: relative;
    height: 400px;
    width: 400px;
    background-image: url("http://www.scrapsyard.com/wp-content/uploads/2015/07/Nature.jpeg");
}

#n1 {
    display: inline-block;
    height: 100px;
    width: 100px;
    background-image: url("https://i.ytimg.com/vi/uZA6pIrwm-I/maxresdefault.jpg");
}

#n2 {
    display: inline-block;
    height: 100px;
    width: 100px;
    background-image: url("http://www.hdwallpapers.in/walls/some_are_different-wide.jpg");
}

#n3 {
    display: inline-block;
    height: 100px;
    width: 100px;
    background-image: url("https://i.ytimg.com/vi/TGLYcYCm2FM/maxresdefault.jpg");
}

.mi:hover ~ .mar {
    height: 600px;
}
<div class="mar">
</div>
<div class="mi" id="n1">
</div>
<div class="mi" id="n2">
</div>
<div class="mi" id="n3">
</div>

My goal is for the background image of the large div with class mar to dynamically change to match the hovered small div with class mi. Can this be achieved through CSS alone?

Answer №1

I simply added the following CSS code:

#n1:hover ~ .mar {
   background-image: url("https://i.ytimg.com/vi/uZA6pIrwm-I/maxresdefault.jpg");  
}

#n2:hover ~ .mar {
   background-image: url("http://www.hdwallpapers.in/walls/some_are_different-wide.jpg");
}

#n3:hover ~ .mar {
  background-image: url("https://i.ytimg.com/vi/TGLYcYCm2FM/maxresdefault.jpg");
}

Then I positioned the .mar div at the end like this:

<div class="mi" id="n1">
</div>
<div class="mi" id="n2">
</div>
<div class="mi" id="n3">
</div>
<div class="mar">
</div>

If you need a visual reference, check out this fiddle. Good luck with your project!

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

Can the background color of HTML header text be altered using JavaScript in JQGRID?

Can the background color of HTML header text be modified using JavaScript? Updated: Oops, I forgot to mention that it is for the header text in jqGrid. My apologies for that oversight. ...

Transforming Input Data into Lowercase Letters

I am working on a form and I need the "username" input to be converted to lowercase letters no matter what the users enter. Can anyone help me with how to achieve this? <div class="usernameformfield"><input tabindex="1" accesskey="u" name="userna ...

The symbol for expanding and collapsing sections in the Bootstrap accordion is not dynamically updating

I'm facing an issue with my bootstrap accordions. I have two accordions and each one has an inner accordion. The first one is set to be opened by default. However, when I click on the second accordion, the icon of the first one does not update to a pl ...

`Is it possible to interpret HTML using AutoHotkey?`

I am struggling with the parsing of HTML files. The examples I have come across are quite complex for a beginner like me, making it difficult to understand. Although I've tried reading the GetNestedTag manual, I believe there might be a simpler soluti ...

JavaScript Animation: Enhancing TextBox Borders

I'm exploring an idea for a loading animation, but I'm feeling a bit lost on where to begin. Basically, I have a text box that gets populated automatically with JavaScript on the page, however, it takes some time to load. I want to create somethi ...

The lower text box on the page being covered by the virtual keyboard on IOS

Our website features a fixed header and footer with scrollable content. We have 20 text boxes on the page, but the ones at the bottom, like Zip and Telephone, are obscured by the iOS virtual keyboard that appears when a text box is clicked. If we could d ...

Using CSS or Bootstrap, align the image to the right side of the content

Struggling to align an image to the right of the content area, inspired by this Dribble shot. Check out the inspiration here This is what I've come up with so far: Link to my progress on CodePen .content { position: relative; } .bg-image { posi ...

Utilizing the calc() function to determine height dimensions

I created a very simple layout with 5 divs, which can be viewed here: http://jsfiddle.net/8tSk6/. In this layout, I want the div with the ID "kubka" to have a height of 100% - 100px (height: calc(100% - 100px);). However, I am running into issues as it i ...

Converting a React JS Div into a PNG image or taking a screenshot of the

Looking for a solution to capture the div with id my-char when char is clicked? I want to take a screenshot without the parent background color and use it on serverside. I've tried some methods but none seem to work. Here is the code snippet and jsFi ...

Hugo/Docsy encountered an error while executing the template at <.url>: unable to assess the url field in type bool

Currently, I'm in the process of creating a static web application using Hugo in combination with the Docsy theme. In my code for the Docsy Partials, I'm attempting to incorporate a conditional statement that appends the word mailTo: to my .url i ...

Round shield progress indicator

https://i.sstatic.net/D3GCr.png I have been trying to create a progress bar by overlaying one SVG on top of another (one translucent and the other colored). Despite many attempts, I have not been successful in solving this problem. Any help or solutions w ...

Personalized styling in material-ui v4.9.0

I recently updated the Material UI version in my project from 0.20 to v4.9. I have successfully changed all the imports to @material-ui/core and my app is compiling without any issues. However, I am facing a problem with the styling. Previously, I did not ...

Utilizing jQuery on the newly inserted <tr> element

I have created a jQuery code that includes 3 pre-existing rows with a text box to add more similar rows. The jQuery functions are initially applied to the first 3 hard-coded rows. I am looking to extend this functionality to dynamically added rows later on ...

Mysterious additional spacing

I am facing an issue with aligning my div elements with a 20px margin. Despite setting the margin to 20px, I notice that there are additional 4 pixels rendered when I view it on the browser. Below is the code snippet: .block{ width:50px; height:50 ...

What is the process for moving a tap-target button with materialize css?

Looking for tips on how to relocate a tap-target button that's currently stuck in the bottom right corner of the screen. I've experimented with various methods like adjusting margins, padding, and even nesting it in an outer div, but nothing seem ...

Inquiring about the usage of div elements in constructing an HTML webpage

When designing my own web pages, I consistently encounter issues with using divs for a multi-column layout. Despite utilizing the float attribute to align columns left or right, resizing the browser window often causes them to stack on top of each other an ...

How to fix the issue of converting system.collections.generic.list to system.collections.generic.ienumerable

I encountered the following issue: Cannot implicitly convert System.Collections.Generic.List to System.Collections.Generic.IEnumerable when attempting to utilize a list for constructing @Html.DropDownList() in a C# Razor page. The problem lies within a ...

Utilizing CSS positioning for layered backgrounds

I'm looking to create a unique background design using three different images. The main background should consist of a 150px x 150px image repeated throughout, with the top line featuring a second image repeating only on the x-axis and the bottom line ...

mandatory data fields for an HTML form

I'm having some trouble creating an HTML form with mandatory input fields. The code I have so far is shown below: <div class="col-sm-6"> <label for="city" class="form-control">City ...

Hover effect with diagonal movement

I'm attempting to recreate the unique diagonal arrow animation featured on this website: For reference, here is a small boilerplate: https://jsfiddle.net/randal923/x0ywchq5/8/ Any advice on the best way to position and animate the arrow would be gre ...