DEV Community

Image Popup on hover

Sarah Chima on October 12, 2018

Recently, I had the task of making an image popup on hover. This gif above describes what I mean. I did not know how to do it but after some minu...
Collapse
 
moopet profile image
Ben Sinclair • Edited

I have a suggestion to improve what you've coded - if you ditch the thumbnail images and instead resize the large images in the browser you'll save the extra weight of the thumbnails (since you're always downloading the larger image anyway) and use fewer HTTP requests:

<li>
  <img src="img/large1.jpg" alt="adventure" >
  <span class="large">
    <img src="img/large1.jpg" class="large-image" alt="adventure" >
  </span>
</li>
li > img {
  max-width: 40px;
}

What's the benefit here of not using Javascript?

If you're concerned about accessibility then you've swapped one problem for another - you have to modify the HTML to serve twice the number of images (i.e. it's not "pure" CSS) and will have to find a way of hiding those images and their alt attributes from screen readers, etc.

Collapse
 
sarah_chima profile image
Sarah Chima

Sounds like a great suggestion. So instead of requesting for two images, we use just one. This reduces the number of requests made. Thanks for the suggestion.

Collapse
 
ayrtonvwf profile image
Ayrton Fidelis

Nice article!

We could implement some lazy-loading by putting the large image as the background of an element that appears only when we hover the thumbnail.

Here's an example that demonstrate how lazy-loading would work:

<style>
 img:hover + .big {
  background-image: url('https://via.placeholder.com/300x300');
  width: 300px;
  height: 300px;
 }
</style>
<img src="https://via.placeholder.com/100x100">
<div class="big"></div>

Inspecting the network requests, we can see that the large image is just fetched when we hover the thumbnail.

Of course, we'd have to work a little harder with accessibility.

Collapse
 
stevensonmt profile image
stevensonmt

I like Ben Sinclair's suggestion more, but I was going to recommend modifying the display attribute rather than position to achieve a similar effect. I'm not sure it matters that much, but I would expect that to be a little more efficient on first render.

Collapse
 
stevensonmt profile image
stevensonmt

BTW, here's an example my daughter and I put together last year for her to learn about HTML and CSS, which is similar to what you've done but uses transitions and the display property instead of position. We'll work on font choice next year ;)
Repo: github.com/stevensonmt/pheniestem
Site: stevensonmt.github.io/pheniestem/s...

Collapse
 
jeroka profile image
Esteban Rocha

Very good! Love CSS only approaches when possible, BTW! also you could also apply some opacity and transition to that, to create a smooth change.

Collapse
 
sarah_chima profile image
Sarah Chima

That sounds like a great idea! I'll try that.

Collapse
 
david_j_eddy profile image
David J Eddy

Nice Sarah! #backintheday This would have been lines and lines of js logic...then seperate js logic for every browser vendor. Amazing how things have changed...for the better :)

Collapse
 
sarah_chima profile image
Sarah Chima

Wow!!! That must have been tedious.

Collapse
 
andy profile image
Andy Zhao (he/him)

Awesome! Love that it's pure CSS!

Collapse
 
ezizeay profile image
Ezizeay

what about several small hyperlinked images Popup on hover an image?