Display elements on hover of thumbnails using CSS

I'm struggling with the logic of displaying images when hovering over corresponding thumbnails using only CSS. If necessary, I can do it in JavaScript. Here's my latest attempt.

<div id='img-container' class='grd12'>

    <img id='img1' class='slide-images' src='images/10086115704_15ab56a165_o.jpg' alt='1'>
    <img id='img2' class='slide-images' src='images/9917938624_0a8778f8b1_o.jpg' alt='2'>
    <img id='img3' class='slide-images' src='images/PIA18847.jpg' alt='3'>
    <img id='img4' class='slide-images' src='images/sun-large.jpg' alt='4'>

</div>

     <img id='thumb1' class='grd3 thumbnail' src='images/10086115704_e36e457d2b_q.jpg' alt='##'>

     <img id='thumb2' class='grd3 thumbnail' src='images/9917938624_1ed12deaa2_q.jpg' alt='##'>

    <img id='thumb3' class='grd3 thumbnail' src='images/PIA18847.jpg' alt='##'>

   <img id='thumb4' class='grd3 thumbnail' src='images/sun-large.jpg' alt='##'>

Here is the CSS code:

#img-container{
    position:relative;
    top:0px;
    left:0px;
    height:950px;
}
.slide-images{
    position:absolute;
    top:0px;
    left:0px;
 }
 .thumbnail > img{
    margin-left:auto;
    margin-right:auto;
    display: inherit;
}
img#thumb4:hover ~ #img4>#image4{
    display:none;
}

Answer №1

In my opinion, achieving this effect using just CSS may be possible but not very feasible in terms of scalability. It might be more practical to utilize JavaScript instead. For instance:

img#thumb1:hover ~ #img4>#image4{
    display:none;
}

The selector you have used is incorrect. The general sibling selector only selects elements that come after the initial match. In this scenario, your image thumb comes after your image, whereas the selector is searching for an image after an image thumb. This contradicts the intended behavior. There isn't a 'sibling before' selector available in CSS.

An easier approach, as opposed to manipulating CSS selectors, would be to assign each thumbnail a click event that alters the source of a single image tag every time (or alternatively, transitions between images with animation). By doing so, you reduce the amount of markup needed, minimize positioning concerns, and can dynamically generate the image display.

For example, to obtain the ID of an image, you could link a click event to each thumbnail and then retrieve the image ID stored within a data attribute:

$('.thumbnail').on('hover', function() {
    var activeImg = $(this).data('imgid');
    // Proceed to set the main image with the corresponding image source
}

Answer №2

It is completely achievable using only CSS. The key lies in adjusting the structure of your HTML. Take a look at this sample:

  • Every thumbnail and full-size image is contained within a div element

  • The full-size image initially has opacity: 0; to hide it

  • On hover, the full-size image within the container gets opacity: 1, producing a smooth fade-in effect thanks to the transition property

  • z-index: 1 ensures that the full-size images appear above the thumbnails

Complete Code Example

.item {
  float: left;
  position: relative;
}
img {
  display: block;
  cursor: pointer;
  margin: 5px;
}
.fullsize {
  position: absolute;
  opacity: 0;
  transition: opacity 0.6s;
  z-index: 1;
  top: 0;
  left: 0;
  pointer-events: none;
}
.item:hover .fullsize {
  opacity: 1;
}
<div class="item">

  <img class="fullsize" src="http://lorempixel.com/output/people-q-c-600-600-9.jpg" />
  <img class="thumb" src="http://lorempixel.com/output/people-q-c-200-200-9.jpg" />
</div>

<!-- Repeat for additional items -->

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

Exporting a React parent function to a child component

I have a React project where the rendering works fine, but I am facing an issue with calling a function declared in the parent component. Parent import React, { useState, useMemo, useRef, useCallback, useEffect } from "react"; import { AgGridRe ...

Error: The value of 'v4' property is not defined and cannot be read

I am encountering an issue with Vue.js: vue.esm.js?efeb:628 [Vue warn]: Error in v-on handler: "TypeError: Cannot read property 'v4' of undefined" found in ---> <AddTodo> at src/components/AddTodo.vue <App> at src/Ap ...

Simulating an API request using Vue and Jest/Vue test utils

Utilizing Vue for the frontend and Python/Django for the backend, I aim to create tests that verify the functionality of my API calls. However, I am encountering difficulties when attempting to mock out the Axios calls. I suspect there might be an issue w ...

Can I increase the top margin by more than mt-5?

Seeking help to align elements on my website. I've applied mt-5 in the HTML, yet require additional margin space. Any suggestions on how to achieve this? ...

Attempting to use express and nodemailer to send an email, but there is absolutely no output appearing in either the console or the terminal

When I click the send email button, it should send the data to a mailhog inbox. However, nothing happens - no errors in the console or terminal. My goal is to use nodemailer to send the name, email, chosen plan, and message from the form to the mailhog add ...

Ways to distinguish XmlHttpRequest Access-Control-Allow-Origin issues from regular network errors

When making an ajax request, there is a possibility of encountering an error, indicating a failure to establish communication with the intended target (no status code returned). To handle these errors, you can use the following code: var oXhr = new XMLHt ...

Creating a tooltip for new list items: A step-by-step guide

I'm currently working on creating a tooltip using JQuery for incoming list items. Users will input text in a textfield, press enter, and those values will be displayed in a visible list on the website. I intend to have a tooltip associated with each i ...

Utilizing CSS for Page Orientation

For my current project, I was tasked with designing a print view using HTML & CSS. This view is then converted into a PDF on the server and sent to an A5 printer for physical output. One of the specific requirements of this assignment is that the first pa ...

Implementing a NextJS client component within a webpage

I am currently working with NextJS version 14 and I am in the process of creating a landing page. In one of the sections, I need to utilize the useState hook. I have specified my component as "use-client" but I am still encountering an error stating that " ...

It is imperative that the query data is not undefined. Be certain to provide a value within your query function that is not undefined

I am utilizing a useQuery hook to send a request to a graphql endpoint in my react.js and next.js project. The purpose of this request is to display a list of projects on my website. Upon inspecting the network tab in the Chrome browser, the request appear ...

Floating with Bootstrap Utility

Have you ever wondered about the proper usage of bootstrap float utility? Check out this example: <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/> <div class="row"> <div class=" ...

Why is the Border Radius hiding the corners of my image?

I've never experienced this issue before, but no matter how much padding I add to the picture, the edges are still slightly covered up. Here is an example image (notice that it doesn't happen for every icon - the third one has a normal round back ...

Using jQuery to load and parse a JSON file stored on a local system

I am a beginner in scripting languages and recently searched for ways to load and parse JSON files using jQuery. I found helpful resources on Stack Overflow. The JSON file I am working with is called new.json. { "a": [ {"name":"avc"}, ...

Is it possible to insert an image directly into the <img> tag without having to first send it to the server?

I've created an upload form that allows users to submit images: <form> <input accept="image/jpeg, image/gif, image/png" type="file" name="image" id="image" class="UploadInput" onchange="submitImageUploaderForm()"/> </form> Once t ...

Looking to authenticate a CSS span class within an XML document using Oxygen Program?

This is in connection with my initial query. The XML does not recognize the CSS span class. <l rend="indent"> <span class="face_dropcap_ ">C</span><hi rend="initial_roman">E</hi>stoit alors que le prefent des <span class= ...

Tips for utilizing a shared controller in an Angular Material popup dialogue

Within my project, Angular Material is being used for various dialogs, primarily for alert purposes. However, I now find myself in need of a more complex dialog. The example provided on the Angular Material site showcases how to create a dialog: function ...

When generating a fresh event and attempting to link the event ID to its creator, unexpected obstacles emerged

I am currently working on an application that revolves around events. One of the key features requires users to be logged in to create events, and upon creation, I need to update the user's events array with the event ID. However, I have encountered a ...

Fetching an item from Local Storage in Angular 8

In my local storage, I have some data stored in a unique format. When I try to retrieve the data using localStorage.getItem('id');, it returns undefined. The issue lies in the way the data is stored within localStorage - it's structured as a ...

Identify whether the page is being accessed through the Samsung stock browser or as an independent web application

Hey there! I am on a mission to determine whether my webpage is being accessed through Samsung's default browser or as a standalone web app saved on the homescreen. Unfortunately, the javascript codes I have come across only seem to work for Safari an ...

Creative CSS Techniques: Styling Initial Capital Letters (Drop Caps) in a CSS3 Multicolumn Layout

For the past year, the multicolumn css3 property has grown in popularity among different browsers. It's a good reason to consider implementing it on your website for improved design and readability. I decided to take it a step further and incorporate ...