Does "display:none" prevent an image from loading?
Browsers are getting smarter. Today your browser (depending on the version) might skip the image loading if it can determine it's not useful.
The image has a display:none
style but its size may be read by the script.
Chrome v68.0 does not load images if the parent is hidden.
You may check it there : http://jsfiddle.net/tnk3j08s/
You could also have checked it by looking at the "network" tab of your browser's developer tools.
Note that if the browser is on a small CPU computer, not having to render the image (and layout the page) will make the whole rendering operation faster but I doubt this is something that really makes sense today.
If you want to prevent the image from loading you may simply not add the IMG element to your document (or set the IMG src
attribute to "data:"
or "about:blank"
).
If you make the image a background-image of a div in CSS, when that div is set to "display: none", the image will not load. When CSS is disabled, it still will not load, because, well, CSS is disabled.
The answer is not as easy as a simple yes or no. Check out the results of a test I recently did:
- In Chrome: All 8 screenshot-* images loaded (img 1)
- In Firefox: Only the 1 screenshot-* image loaded that is currently being displayed (img 2)
So after digging further I found this, which explains how each browser handles loading img assets based on css display: none;
Excerpt from the blog post:
- Chrome and Safari (WebKit):
WebKit downloads the file every time except when a background is applied through a non-matching media-query.- Firefox:
Firefox won't download the image called with background image if the styles are hidden but they will still download assets from img tags.- Opera:
Like Firefox does, Opera won't load useless background-images.- Internet Explorer:
IE, like WebKit will download background-images even if they have display: none; Something odd appears with IE6 : Elements with a background-image and display: none set inline won't be downloaded... But they will be if those styles aren't applied inline.
HTML5 <picture>
tag will help you to resolve the right image source depending on the screen width
Apparently the browsers behaviour hasn't changed much over the past 5 years and many would still download the hidden images, even if there was a display: none
property set on them.
Even though there's a media queries workaround, it could only be useful when the image was set as a background in the CSS.
While I was thinking that there's just a JS solution to the problem (lazy load, picturefill, etc.), it appeared that there's a nice pure HTML solution that comes out of the box with HTML5.
And that is the <picture>
tag.
Here's how MDN describes it:
The HTML
<picture>
element is a container used to specify multiple<source>
elements for a specific<img>
contained in it. The browser will choose the most suitable source according to the current layout of the page (the constraints of the box the image will appear in) and the device it will be displayed on (e.g. a normal or hiDPI device.)
And here's how to use it:
<picture>
<source srcset="mdn-logo-wide.png" media="(min-width: 600px)">
<img src="mdn-logo-narrow.png" alt="MDN">
</picture>
The logic behind
The browser would load the source of the img
tag, only if none of the media rules applies. When the <picture>
element is not supported by the browser, it will again fallback to showing the img
tag.
Normally you'd put the smallest image as the source of the <img>
and thus not load the heavy images for larger screens. Vice versa, if a media rule applies, the source of the <img>
will not be downloaded, instead it will download the url's contents of the corresponding <source>
tag.
Only pitfall here is that if the element is not supported by the browser, it will only load the small image. On the other hand in 2017 we ought to think and code in the mobile first approach.
And before someone got too exited, here's the current browser support for <picture>
:
Desktop browsers
Mobile browsers
More about the browser support you can find on Can I use.
The good thing is that html5please's sentence is to use it with a fallback. And I personally intend to take their advise.
More about the tag you can find in the W3C's specification. There's a disclaimer there, which I find important to mention:
The
picture
element is somewhat different from the similar-lookingvideo
andaudio
elements. While all of them containsource
elements, the source element’ssrc
attribute has no meaning when the element is nested within apicture
element, and the resource selection algorithm is different. As well, thepicture
element itself does not display anything; it merely provides a context for its containedimg
element that enables it to choose from multiple URLs.
So what it says is that it only helps you improve the performance when loading an image, by providing some context to it.
And you can still use some CSS magic in order to hide the image on small devices:
<style>
picture { display: none; }
@media (min-width: 600px) {
picture {
display: block;
}
}
</style>
<picture>
<source srcset="the-real-image-source" media="(min-width: 600px)">
<img src="a-1x1-pixel-image-that-will-be-hidden-in-the-css" alt="MDN">
</picture>
Thus the browser will not display the actual image and will only download the 1x1
pixel image (which can be cached if you use it more than once). Be aware, though, that if the <picture>
tag is not supported by the browser, even on descktop screens the actual image won't be displayed (so you'll definitely need a polyfill backup there).
** 2019 Answer **
In a normal situation display:none
doesn't prevent the image to be downloaded
/*will be downloaded*/
#element1 {
display: none;
background-image: url('https://picsum.photos/id/237/100');
}
But if an ancestor element has display:none
then the descendant's images will not be downloaded
/* Markup */
<div id="father">
<div id="son"></div>
</div>
/* Styles */
#father {
display: none;
}
/* #son will not be downloaded because the #father div has display:none; */
#son {
background-image: url('https://picsum.photos/id/234/500');
}
Other situations that prevent the image to be downloaded:
1- The target element doesn't exist
/* never will be downloaded because the target element doesn't exist */
#element-dont-exist {
background-image: url('https://picsum.photos/id/240/400');
}
2- Two equal classes loading different images
/* The first image of #element2 will never be downloaded because the other #element2 class */
#element2 {
background-image: url('https://picsum.photos/id/238/200');
}
/* The second image of #element2 will be downloaded */
#element2 {
background-image: url('https://picsum.photos/id/239/300');
}
You can watch for yourself here: https://codepen.io/juanmamenendez15/pen/dLQPmX