I am wondering if it is possible to swap out an image when hovering over an icon link by only using CSS and HTML

I am working on a layout where an image is displayed in a col-8 next to 4 icons and a paragraph in a col-4. I want to keep the formatting consistent on this page. Specifically, I would like the image linked to the first icon to automatically display when the page loads. Then, when the user hovers over the other three icons, I want the corresponding images to replace the initial one.

If anyone has a solution or can point me towards a helpful resource, such as a video or article, I would greatly appreciate it. Unfortunately, I had to remove some code related to the svg icons for ease of reading and file size constraints, but you should get the gist of it. https://i.sstatic.net/u13pz.jpg

HTML

<div class="container-right">
            <div class="row pb-5 pt-3">
                <div class="col-4 ">
                    <h1 class="pb-2 text-center text-md-left">Products</h1>
                    <b>Customer satisfaction is a top priority. At Hills Industrial, we want our customers to be astounded with the choices we offer, excited by the innovation we provide and satisfied with our service and reliability. Providing superior customer experience in a consistent manner is our primary goal.</b>
                    <div class="row mt-4">
                        <!-- Controls -->
                        <div class="col-5">
                            <a href="#"><div class="icon-background"><svg class="product-icon" </svg></div></a>
                        </div>
                        
                        (Icons continuation omitted for brevity)
                    
                    </div>
                </div>
                <div class="col-8">
                    <img src="img/motor-stock.jpg" class="product-photo"></img>
                </div>
            </div>
        </div>

CSS

.product-icon{
  width:50px;
  fill: #ffff;
  transition:fill 0.3s ease-out;
}

 .icon-background:hover .product-icon{
  fill: #fb0203;
}

.product-photo{
  border-radius:400px 0px 0px 400px ;
  max-width: 1000px;
  height: 100%;
}

.container-right {
  padding-left: 15px;
  /* padding-right: 15px; */
}
(Media queries and CSS classes continue for responsiveness and styling)

Answer №1

To avoid diving too deeply into your code, one suggestion is to forego using an img tag and instead set the image as a background-image on the div; additionally, arrange the links on the same level as the image div so that you can utilize the sibling combinator for image changes.

One approach involves leveraging CSS grid (with images sourced from ):

.container {
  display: grid;
  grid-template-areas:
    "content content image image"
    "link1 link2 image image"
    "link3 link4 image image";
}

.content {
  grid-area: content;
}

.link {
  display: inline-grid;
  place-content: center;
  height: 50px;
  width: 50px;
  background-color: lightblue;
  border-radius: 100%;
  cursor: pointer;
}

.link:hover {
  background-color: blue;
  color: white;
}

#link-1 {
  grid-area: link1;
}

#link-2 {
  grid-area: link2;
}

#link-3 {
  grid-area: link3;
}

#link-4 {
  grid-area: link4;
}

.image {
  grid-area: image;
  background-image: url("https://kaboompics.com/cache/8/4/3/a/0/843a085ea22499ba621d5808a4f33fba35045a6d.jpeg");
  background-size: cover;
  height: 500px;
  width: 700px;
}

#link-1:hover ~ .image {
  background-image: url("https://kaboompics.com/cache/d/1/9/b/2/d19b234473ad74fce320c208f5e0f8ddfc20d1f3.jpeg");
}

#link-2:hover ~ .image {
  background-image: url("https://kaboompics.com/cache/b/d/b/1/b/bdb1b249aacef7ebcb74a4ffd66f211f9abd98c0.jpeg");
}

#link-3:hover ~ .image {
  background-image: url("https://kaboompics.com/cache/4/3/a/a/c/43aac8eb9d62e85b1f99e916384d08f498bb4810.jpeg");
}

#link-4:hover ~ .image {
  background-image: url("https://kaboompics.com/cache/5/c/6/d/f/5c6dfadb3207b778e2d8478b2502781f7220592f.jpeg");
}
<div class="container">
  <div class="content">
    <h1 class="pb-2 text-center text-md-left">Products</h1>
    <p>Customer satisfaction is a top priority. At Hills Industrial, we want our customers to be astounded with the choices we offer, excited by the innovation we provide and satisfied with our service and reliability. Providing superior customer experience in a consistent manner is our primary goal.</p>
  </div>
  
  <div class="link" id="link-1">Link 1</div>
  <div class="link" id="link-2">Link 2</div>
    <div class="link" id="link-3">Link 3</div>
    <div class="link" id="link-4">Link 4</div>
  
  <div class="image"></div>
</div>

Best of luck!

Edit: According to this MDN article, reserve the use of CSS images for cases where the image lacks semantic meaning (strictly decorative); if your images carry content significance, consider using img tags and managing their visibility via JavaScript.

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

Tips for positioning and styling submenu on dropdown using CSS

I am facing an issue with my navigation menu in the design. The sub menu is not displaying when hovered. Is there a solution to fix this problem? Could it be due to missing CSS styles? nav ul {list-style-type: none; overflow: hidden; background: #000; p ...

The combination of Javascript and CSS allows for interactive and visually

I'm currently working on a project where I had to create a simulated weather page using only Javascript. However, I am struggling with the overall layout and have a few questions that I hope someone can help me with: Despite trying various methods ...

Set maximum size for background image in div

I'm facing some challenges with setting the maximum height of a background image within a div. I want the max height to be equal to the actual height of the image, without stretching it. Ideally, if there is excess content in the div, it should stop a ...

Processing .dat files using JavaScript

Currently, I am attempting to utilize JavaScript to upload a file named example.dat. Initially, I believed that the correct approach would be using fileReader; however, it appears that this method is unable to process this specific format. The objective i ...

Add a CSS class to a different element when hovering

I have the following HTML structure: <div class="container"> <div class="list-wrapper"> <ul> <li class="item-first"> </li> </ul> </div> <div class="description-wrapper"> <d ...

Modify the color of every element by applying a CSS class

I attempted to change the color of all elements in a certain class, but encountered an error: Unable to convert undefined or null to object This is the code I used: <div class="kolorek" onclick="changeColor('34495e');" style="background-c ...

Animate images using mouse vertical movement

Creating websites is just a hobby for me, not something professional. Recently, I came across a beautifully designed website that had some unique features I hadn't seen before, like placing three images in one div (which I researched and learned how t ...

XPaths are used to verify the accuracy and relevance of text content

Here's a snippet of HTML I need help with: HTML Code: <a id="pINumber" href="csf.ajax?et=p.preparationinsurance">PASS170XX</a> XPATH to identify the above element //*[@id="pINumber"] I am looking to create an XPATH that checks for the ...

Invisible Angular Material Date Picker Issue

I am facing an issue with the angular material datepicker in my project. The datepicker itself remains invisible, but it functions when I click on its location or where the calendar icon is placed. To make it more clear for users, I have added a button nex ...

What is the best way to assign a value to the param tag of an applet using JavaScript variables

This is my external javaScript file named myJs.js function search(){ var hint = document.getElementById("sChoice").value; var item = document.getElementById("sKey").value; document.getElementById("c").value=hint; document.getElementById ...

Is Socket.io combined with Redis the best architecture for an interactive gaming experience?

My current setup involves a NodeJS scaling architecture with Redis, specifically designed for a real-time multiplayer game. However, I'm facing challenges in configuring separate lobbies for players, as the load balancer assigns users to different ser ...

Creating a new webpage that automatically populates with multiple images from a server using PHP

I'm inquiring about generating a webpage automatically from an HTML template, How can I create a new webpage from an HTML template and display multiple images from the server on the generated page? Please review my code below, Thank You. The follo ...

Is it possible to use a JavaScript string as a selector in jQuery?

So, my issue is with the following JavaScript code snippet: for ( i=0; i < parseInt(ids); i++){ var vst = '#'+String(img_arr[i]); var dst = '#'+String(div_arr[i]); } I'm wondering how I can proceed in jQuery to handle ...

Error encountered in jQuery's addClass and removeClass functions: Unable to read the property 'length' of an undefined value

Upon loading my page, I aim to have some of the div elements hidden initially and display only one. Here is a script that accomplishes this goal: <script> $(document).ready(function () { $(".total").click(function () { $("#pi ...

When a table cell is hovered over, a left border will be added

I've been working on adding a left border to the text in a way that highlights the first cell's border when hovering over a row, providing a clear indication of the current row for users. Feel free to check out my progress so far on this fiddle: ...

Invoking a Directive within another Directive

Feel free to check out this demo on Plunkr. I've set up a basic structure: <body ng-app="myApp"> <div ng-controller="myController"> <parent-directive></parent-directive> <child-directive></child-direc ...

Finding JPG images using Jquery selector

I am looking to utilize jQuery to specifically target <a href=""> elements that have 'href' attributes with file extensions '.JPG' or '.jpg' For instance: To only select these 'links' <a target=& ...

Seeking assistance in developing a dynamic footer complete with a gallery of images

Attempting to create a footer packed with images but struggling to ensure it looks good on both PCs and mobile devices. The goal is to align the bronze sponsor kitten to the left, and supporter kittens to the right, while keeping the footer at a reasonabl ...

Creating a CSS animation to repeat at regular intervals of time

Currently, I am animating an SVG element like this: .r1 { transform-box: fill-box; transform-origin: 50% 50%; animation-name: simpleRotation,xRotation; animation-delay: 0s, 2s; animation-duration: 2s; animation-iterat ...

The functionality of displaying one div while hiding another upon click seems to be malfunctioning

Can anyone lend a hand? I'm having trouble with my code - the section isn't showing up after clicking the button. <script> $("#img1").on('click', function() { $("#div1").fadeIn(); $("#div2,#div3,#div4").fadeOut(); }); $(" ...