Most images on the web are superfluous. If I might be a jerk for a bit, 99% of them aren’t even that helpful at all (although there are rare exceptions). That’s because images don’t often complement the text they’re supposed to support and instead hurt users, taking forever to load and blowing up data caps like some sort of performance tax.
Thankfully, this is mostly a design problem today because making images performant and more user-friendly is so much easier than it once was. We have better image formats like WebP (and soon, perhaps, JPEG XL). We have the magic of responsive images of course. And there are tons of great tools out there, like ImageOptim, as well as resources such as Addy Osmani’s new book.
Although perhaps my favorite way to improve image performance is with lazy loading:
<img href="image.webp" alt="Image description" loading="lazy">
This image will only load when a user scrolls down the page so it’s visible to the user — which removes it from the initial page load and that’s just great! Making that initial load of a webpage lightning fast is a big deal.
But maybe there are images that should never load at all. Perhaps there are situations where it’d be better if a person could opt-into seeing it. Here’s one example: take the text-only version of NPR and click around for a bit. Isn’t it… just so great?! It’s readable! There’s no junk all over the place, it respects me as a user and — sweet heavens — is it fast.
So! What if we could show images on a website but only once they are clicked or tapped? Wouldn’t it be neat if we could show a placeholder and swap it out for the real image on click? Something like this:
Well, I had two thoughts here as to how to build this chap (the golden rule is that there’s never one way to build anything on the web).
<img>
without a src
attribute
Method #1: Using We can remove the src
attribute of an <img>
tag to hide an image. We could then put the image file in an attribute, like data-src
or something, just like this:
<img data-src="image.jpg" src="" alt="Photograph of hot air balloons by Musab Al Rawahi. 144kb">
By default, most browsers will show a broken image icon that you’re probably familiar with:
Okay, so it’s sort of accessible. I guess? You can see the alt
tag rendered on screen automatically, but with a light dash of JavaScript, we can then swap out the src
with that attribute:
document.querySelectorAll("img").forEach((item) => {
item.addEventListener("click", (event) => {
const image = event.target.getAttribute("data-src");
event.target.setAttribute("src", image);
});
});
Now we can add some styles and ugh, oh no:
Ugh. In some browsers there’ll be a tiny broken image icon in the bottom when the image hasn’t loaded. The problem here is that browsers don’t give you a way to remove the broken image icon with CSS (and we probably shouldn’t be allowed to anyway). It’s a bit annoying to style the alt
text, too. But if we remove the alt
attribute altogether, then the broken image icon is gone, although this makes the <img>
unusable without JavaScript. So removing that alt
text is a no-go.
As I said: Ugh. I don’t think there’s a way to make this method work (although please prove me wrong!).
Method #2: Using links to create an image
The other option we have is to start with the humble hyperlink, like this:
<a href="image.jpg">Photograph of hot air balloons by Musab Al Rawahi. 144kb<a>
Which, yep, nothing smart happening yet — this will just render a link to an image:
That works accessibility-wise, right? If we don’t have any JavaScript, then all we have is just a link that folks can optionally click. Performance-wise, it can’t get much faster than plain text!
But from here, we can reach for JavaScript to stop the link from loading on click, grab the href
attribute within that link, create an image element and, finally, throw that image on the page and remove the old link once we’re done:
document.querySelectorAll(".load-image").forEach((item) => {
item.addEventListener("click", (event) => {
const href = event.target.getAttribute("href");
const newImage = document.createElement("img");
event.preventDefault();
newImage.setAttribute("src", href);
document.body.insertBefore(newImage, event.target);
event.target.remove();
});
});
We could probably style this placeholder link to make it look a bit nicer than what I have below. But this is just an example. Go ahead and click the link to load the image again:
And there you have it! It isn’t groundbreaking or anything, and I’m sure someone’s done this before at some point or another. But if we wanted to be extremely radical about performance beyond the first paint and initial load, then I think this is an okay-ish solution. If we’re making a text-only website then I think this is definitely the way to go.
Perhaps we could also make a web component out of this, or even detect if someone has prefers-reduced-data
and then only load images if someone has enough data or something. What do you think?
This is really interesting! I might implement something like this on my blog.
You don’t need JavaScript for this, you can just use the classic checkbox hack (or radio, focus, etc. depending on the UI you’re going for). Browsers supporting loading=lazy should do the right thing.
To follow this up with an example:
Stick a tiny and transparent 1x1px GIF in the previously empty
src
value (e.g.src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"
and you won’t get the broken image icon. The performance impact should be neglible, even if the data-uri string is repeated multiple times in the document then any compression like Gzip or Brotli should mitigate that.Re: my earlier comment…
I’ve just re-read the article and realised that my suggestion is no good, as then the
alt
text wouldn’t show! Silly me :) I was thinking of non-native lazy-loading libraries where using a data-uri like is a common trick to avoid the broken image icon, but of course they don’t need to visually show the alt text…I wonder using an inlined SVG (with a ‘Click to load’ prompt) would work? You’d lose out on the alt text though, unless you could use a server-side tool or JavaScript to generate the SVG markup dynamically and include the alt text in it.
This would help if I wanted to either save on bandwidth, or just create an interactive section. I like it.
Remindes me of the reading mode of Firefox. It tries to clean up the article from all unrelated stuff.
Putting the JS stuff somehow introduces a further delay (for those obsessed of speed and performance) first because they have to click something then at JS execution. I know, it’s quick, but still visibly annoying.
I would better use an automated loading after few sec after page loaded (maybe with defer, too) like a lazy loading but not after the scroll.
People need not to click anything this way, visual experience is not broken (so they never see a poor front page) however above the fold the alt might show something as ‘image here’.
However the entire logic is flawed: no dev would consider his own images annoying as much as to hide them behind a click, in my opinion.
And why not to put a pixel transparent image with a simple code like this
onclick="this.src='realimage.jpg'"
instead of all convoluted JS code earlier. Sometimes we try to solve a non existent problem :)
Another approach is to use details/summary: https://codepen.io/dannievinther/pen/e9e49d946cffa57ba96d76b207a840fa along with
loading="lazy"
Sweet idea Robert! Another idea is you could render a super light placeholder SVG that fetches the real image on click. Then you might be able to prevent layout shift by making the SVG the same dimensions as the proper image. This is a bit more overhead than your examples though!
Thank you for these posts, is it possible to do this using just CSS? Loading the img on CSS and after clicking on a button the img appears?