How can I use CSS to hide an image when a different image or item is clicked?

I have a task that needs to be completed, and I want to achieve it without using jQuery. I came across a functioning example, but when I tried to implement it myself, the image would not hide as desired. Here is the portion of CSS code that is supposed to control the hiding of an image when another one is clicked on:

    /*Selected image display*/
    .image-gallery .big-image img:target{display:block;}
    /*on select image dusplay none the default image*/
    .image-gallery .big-image img:target ~ img#default{display:none;}
    /*Show Default Image in first load*/
    .image-gallery .big-image img#default{display:block;}

Below is the part of my HTML code where the CSS should take effect:

    <div id="image-wrap">
     <div class="image-gallery">
      <div class="big-image">           
        <a href="http://i.ebayimg.com/00/s/MTYwMFgxNjAw/z/JEkAAOxyThVTcEM~/$_57.JPG"><img style="max-height: 400px" id="merge" src="http://i.ebayimg.com/00/s/MTYwMFgxNjAw/z/JEkAAOxyThVTcEM~/$_57.JPG" /></a>
        <a href="http://i.ebayimg.com/00/s/MTYwMFgxMDIy/z/2cEAAOxyYANTcENF/$_57.JPG?rt=nc"><img style="max-height: 400px" id="fullbody" src="http://i.ebayimg.com/00/s/MTYwMFgxMDIy/z/2cEAAOxyYANTcENF/$_57.JPG?rt=nc" /></a>
       <a href="http://i.ebayimg.com/00/s/MTYwMFgxMTg1/z/36kAAOxy9X5TcENO/$_57.JPG?rt=nc"><img style="max-height: 400px" id="closeup" src="http://i.ebayimg.com/00/s/MTYwMFgxMTg1/z/36kAAOxy9X5TcENO/$_57.JPG?rt=nc" /></a>
        <img style="max-height: 400px" id="default" src="http://i.ebayimg.com/00/s/MTYwMFgxNjAw/z/JEkAAOxyThVTcEM~/$_57.JPG" />
      </div>
 <ul>
<li>
  <a href="#merge"><img style="max-height: 70px" src="http://i.ebayimg.com/00/s/MTYwMFgxNjAw/z/JEkAAOxyThVTcEM~/$_57.JPG" /></a></li>
<li>
  <a href="#fullbody"><img style="max-height: 70px" src="http://i.ebayimg.com/00/s/MTYwMFgxMDIy/z/2cEAAOxyYANTcENF/$_57.JPG?rt=nc" /></a>
</li>
<li>
   <a href="#closeup"><img style="max-height: 70px" src="http://i.ebayimg.com/00/s/MTYwMFgxMTg1/z/36kAAOxy9X5TcENO/$_57.JPG?rt=nc" /></a>
</li>



  </div>

 </div>  

For a visual representation and to see the work in progress, you can visit this link: http://jsfiddle.net/sq72x/

Answer №1

Take a look at the CSS code in action below. I have made some modifications, specifically adding an a tag. Your existing code only closes and displays all img tags, including the thumbnail versions.

I have increased specificity in this updated version.

View the DEMO to see it live.

/*Selected image display*/
.image-gallery .big-image a:target img{display:block;}
/*on select image dusplay none the default image*/
.image-gallery .big-image a:target ~ img#default{display:none; width:1px;}
/*Show Default Image on first load*/
.image-gallery .big-image img#default{display:block;}

Answer №2

To achieve the desired functionality, it's important to target the A tag instead of the IMG tag in your code.

Below is a sample CSS code snippet:

/*Selected image display*/
.image-gallery .big-image a:target img{display:block;}
/*Hide default image on selecting another image*/
.image-gallery .big-image a:target ~ img#default{display:none; width:1px;}
/*Display default image initially*/
.image-gallery .big-image img#default{display:block;}

For an updated version, refer to this fiddle: http://jsfiddle.net/sq72x/1/

Edit: Remember to adjust the HTML structure by moving the ID attributes from big images to their parent A tags. Check the modified HTML and CSS in the provided fiddle for further clarification.

Answer №3

The original code was effective, but the default image remained visible.

To solve this issue, I placed the default image at the bottom and added overflow: hidden to .big-image. However, this caused the default image to appear further down the page with a portion missing. Adjusting the top position to -100px resolved this, but led to the default image overlapping with my gallery. To address this, I assigned all other images the class .mainpic

 .mainpic {position: relative; z-index: 10;}

I designated the default image as .defaultpic

 .defaultpic {position: relative; z-index: 5;}

The use of z-index ensures that .mainpic always displays in front of .defaultpic

It may be a bit chaotic, but it gets the job done!

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

What is the best way to include a div element with a dynamic animation on a webpage?

I'm attempting to create a laser beam that can shoot enemies on the screen, much like in classic games such as Space Invaders or Galaga. However, I am encountering difficulties getting the laser to move when I click the button. Below is the code I hav ...

How can I achieve slanted edges using CSS?

Can a menu be created solely with CSS that features sloped borders? For example, something like this: http://jsfiddle.net/wmgmqrr9/, but with slanted edges. <ul> <li class="active">home</li> <li>about</li> <li ...

Utilizing highcharts to visualize non-linear time data pulled from a CSV file

I am seeking guidance on implementing a simple graph based on data from a CSV file in web development. I lack experience in this area and have struggled to find a suitable example to follow. The CSV file contains data in the format of a unix timestamp, hu ...

Utilizing numbered lists through HTML, CSS, and/or JavaScript

I am looking to accomplish something similar to this... My guess is that the images should be stored in an array in Javascript with the image links. However, I am uncertain if this can be done using only HTML and CSS. I found a solution on this thread, w ...

"Creating a link to a button with the type of 'submit' in HTML - a step-by-step

Found the solution on this interesting page: In a list, there's a button: <form> <ul> <li> <a href="account.html" class="button"> <button type="submit">Submit</button> ...

Choose the text value of the jQuery select menu from the specified column in the table

I need to search through all the select elements in a specific table column with the id "tdcities". I want to compare their ids with the row id and if there is a match, I need to select the text value. Below is the code I am currently using: $("#tdcities ...

Button group malfunctions on smaller screens

After integrating a button group into my new website, I noticed that the first two buttons stop functioning properly on smaller screens. Surprisingly, if I remove the text-center div, all buttons cease to work entirely. Despite attempting various solution ...

Guide on restricting the character count and displaying the leftover characters using PHP with Ajax

I've been working on implementing a feature to display the remaining characters in a PHP AJAX call. I was successful using JavaScript, but I'm having trouble doing it with AJAX in PHP. Can someone provide assistance? <script type="text/javasc ...

Is it possible for CSS to automatically style paragraphs based on the preceding heading class?

Looking to create some unique CSS rules for formatting interview transcripts. The format I have in mind is as follows: <h2 class="interviewer">Alice: [00:00:00]</h2> <p>Is it ok if I ask you a question now?</p> <h2 class="inter ...

Encountered a runtime error while trying to insert a list item <li> into a paragraph <p> element

Take a look at this code snippet: <%@ Page Title="Home Page" Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication18._Default" %> <!DOCTYPE html> <html> <body> <p id="someId"></p& ...

Finding a page element based on its title attribute in Selenium and Java

My goal is to locate and click on an image with the word "Collector" in its title. Below is the HTML code for the image and its link: <a href="javascript:*command*" title="Level III: KPI Collector RYG of D D/Testing - SYS"> <img src="*unusable l ...

ColdFusion encounters an HTML form error, presenting the message: "There is an undefined element in the FORM."

I'm attempting to submit an HTML form that contains checkboxes for each day of the week. Whenever a checkbox is checked, I assign a value of 1 to it. To handle unchecked checkboxes, I set a default value of 0 using a CFPARAM tag in the form action pag ...

How can we filter the input type file browse window to display only image files?

I'm trying to figure out how to filter the window popup so that it only shows image files when I click on the input type file. <input type="file" /> Any help would be greatly appreciated as I have been struggling to find a solution for this. ...

Why is my Bootstrap Carousel Switching Images but Refusing to Slide?

I've encountered a problem with my bootstrap carousel. The slide effect doesn't seem to be working, even though I have added the "slide" CSS tag. Instead of sliding, the images just quickly change without any transition. Here are some key points ...

As you scroll, this smooth marquee effect gently shimmers with each movement

I've been trying out this code to create a scrolling marquee text, but the issue is that after 7000 milliseconds it starts to jitter, which doesn't make the text look good. Any suggestions on how I can fix this problem? Let me know! <script ...

Is it possible to implement the 'opacity' feature on the homepage of my HTML website?

I am currently working on my website and I would like to display a notice to members when they open the site. This notice should only appear once, right above the HTML homepage, as the homepage content may vary for different members. I plan to use 'op ...

"Using the d-flex class with Bootstrap 4 tables does not allow them to expand to

Since the latest version of Bootstrap 4, it has become more challenging to apply classes like col-md-2 directly to the tr or td elements of a table. A possible solution is to use class="d-flex" in the parent element: <tr class="d-flex"> However, w ...

Using jQuery to animate two images simultaneously in opposite directions

Trying to make two images animate using a single function. The issue is that one image needs to animate the left attribute while the other needs to animate the right. Here's what I have so far: $(document).ready(function(){ v ...

Making the navbar color modifications to the dropdown menu

I am currently working on applying color changes to the text in my navbar for the links that have been visited, are active, and are being hovered over. I have successfully implemented this for my regular hyperlink elements. However, I am facing an issue wi ...

Tips for keeping a div element at the top of the page

I am looking to have a div stick to the top of the page when someone scrolls down When the page is scrolled, the green div with class stickdiv should automatically stick to the top var left = document.getElementsByClassName("stickdiv"); for( var i = 0; ...