By nasty

2012-08-28 11:42:08 8 Comments

Every responsive website development tutorial recommends using the display:none CSS property to hide content from loading on mobile browsers so the website loads faster. Is it true? Does display:none not load the images or does it still load the content on mobile browser? Is there any way to prevent loading unnecessary content on mobile browsers?


@Denys Séguret 2012-08-28 11:50:34

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 :

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").

@Denys Séguret 2012-08-28 12:08:05

Probably yes. And also the possibility that the image may be displayed later. And maybe on the general principle that a runtime engine shouldn't try to be too smart and fix all developer's errors.

@Srinivas 2013-07-09 16:27:27

an empty image src is dangerous. It sends an extra request to the server. a good read on this topic…

@Denys Séguret 2013-07-09 16:42:25

@SrinivasYedhuri Yes, you're right. I edited with a better solution.

@Shawn Whinnery 2014-05-23 23:20:12

This answer is only partially correct. I just tested this on Google Chrome (v35) and I can confirm that images with display set to none are not downloaded. This is probably to make responsive design easier on the developer.

@DMTintner 2014-06-18 10:03:22

This answer is no longer correct. See below for updated results of how various browsers, including the latest version of FF, handle this situation differently

@TKoL 2017-10-23 15:04:28

Even today (oct 2017), the most common browser Chrome will download all 'img' element sources, even if they're hidden. It won't download hidden background images.

@J. C. Rocamonde 2018-08-25 14:44:47

Chrome 68.0.3440.106, 25/08/2018: does not load images if parent is hidden (display:none;).

@DMTintner 2014-04-09 18:28:02

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.

Chrome- All 8 screenshot-* images loaded

Firefox- Only the 1 screenshot-* image loaded that is currently being displayed

@Mark Kaplun 2015-03-22 07:06:28

As long as those results are not part of the standards they are quite pointless. Chrome changes rendering engine, opera changes as well, and IE will be replaced with something else. You just can't count on implementation of fringe features like this to remain stable over time. The solution will be a way to hint the browser when an asset should be loaded lazily if at all.

@DMTintner 2015-11-30 16:46:49

@MarkKaplun I was not suggesting that these test results should show you exactly what you can expect to always happen with every browser all the time. I only meant to demonstrate that the answer is "not as simple as yes or no". each browser is currently implementing this differently and that will probably continue to be this way for a while

@bhu Boue vidya 2016-06-17 04:55:30

@DMTintner exactly right. and as of just today i can confirm iOS safari loaded background images of div's that were `display: none'. the best approach is to not assume anything and if you don't want an image loaded, don't reference it in a html tag

@Juanma Menendez 2019-04-24 19:43:06

** 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('');

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>

/* Styles */

#father {
    display: none;

/* #son will not be downloaded because the #father div has display:none; */

#son {
    background-image: url('');

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('');

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('');

/* The second image of #element2 will be downloaded */

#element2 {
    background-image: url('');

You can watch for yourself here:

@commonpike 2016-04-13 10:21:47

If so is there a way to not load the unnecessary content on mobile browsers?

use <img src="" srcset="">

@Yazan Rawashdeh 2020-04-17 12:57:20

do not set the src to ""…

@Evgenia Manolova 2017-03-15 00:52:10

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:

 <source srcset="mdn-logo-wide.png" media="(min-width: 600px)">
 <img src="mdn-logo-narrow.png" alt="MDN">

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

Desktop browsers support

Mobile browsers

Mobile browsers support

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-looking video and audio elements. While all of them contain source elements, the source element’s src attribute has no meaning when the element is nested within a picture element, and the resource selection algorithm is different. As well, the picture element itself does not display anything; it merely provides a context for its contained img 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:

  picture { display: none; }

  @media (min-width: 600px) {
    picture {
      display: block;

 <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">

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).

@Swimburger 2019-01-14 04:32:31

I took a similar route as you, but use a data-img instead :) Here's a post I wrote on it…

@ninja_corp 2018-02-01 17:13:44

The background-image of a div element will load if the div is set do 'display:none'.

Anyway, if that same div has a parent and that parent is set to 'display:none', the background-image of the child element will not load. :)

Example using bootstrap:

<link href="" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<div class="col-xs-12 visible-lg">
	<div style="background-image: url(''); background-repeat:no-repeat; height: 300px;">lg</div>
<div class="col-xs-12 visible-md">
	<div style="background-image: url(''); background-repeat:no-repeat; height: 200px;">md</div>
<div class="col-xs-12 visible-sm">
	<div style="background-image: url(''); background-repeat:no-repeat; height: 100px">sm</div>
<div class="col-xs-12 visible-xs">
	<div style="background-image: url(''); background-repeat:no-repeat; height: 50px">xs</div>

@IsuNas Labs 2017-11-21 11:40:58

No.The image will be loaded as usual and will still use the user’s bandwidth if you are considering the mobile phone user bandwidth saving.What u can do is to use media query and filter the devices that you want your image to be loaded.Your image must be set as a background image of a div,etc and NOT an tag since the the image tag will load the image regardless if the screen size and the media query set.

@Mikkel Fennefoss 2017-05-24 09:06:35

Hi guys I was struggling with the same issue, how to not load an image on mobile.

But I figured out a good solution. First make an img tag and then load a blank svg in the src attribute. Now you can set your URL to the image as an inline style with content: url('link to your image');. Now wrap your img tag in a wrapper of your choice.

<div class="test">
  <img src="data:image/svg+xml,%3Csvg%20xmlns=%22‌​svg%22/%3E" style="content:url('')">

  @media only screen and (max-width: 800px) {
       display: none;

Set the wrapper to display none on the breakpoint where you dont want to load the image. The inline css of the img tag is now ignored since the style of an element wrapped in a wrapper with display none will be ignored, therefore the image is not loaded, until you reach a breakpoint where the wrapper has display block.

There you go, really easy way not to load an img on mobile breakpoint :)

Check out this codepen, for a working example:

@Windgazer 2017-05-24 12:19:18

I love how to web keeps evolving, hadn't seen this trick before, using the content-property to alter the shown image, that is. Can you give some stats on compatibility on this?

@Mikkel Fennefoss 2018-01-19 12:53:34

To bad about Edge :/, at least it works in the latest versions of Firefox, Chome and Safari.

@Duane 2017-01-31 03:35:18

The trick to using display:none with images is to assign them an id. This was there is not a lot of code needed to make it work. Here is an example using media queries and 3 stylesheets. One for phone, one for tablet, and one for desktop. I have 3 images, image of a phone, a tablet, and a desktop. On a phone screen only an image of the phone will display, a tablet will display only the tablet image, a desktop displays on the desktop computer image. Here is a code example to make it work:

Source code:

<div id="content">
<img id="phone" src="images/phone.png" />
<img id="tablet" src="images/tablet.png" />
<img id="desktop" src="images/desktop.png" />

The phone CSS which doesn't need a media query. Its the img#phone that makes it work:

img#phone {
    display: block;
    margin: 6em auto 0 auto;
    width: 70%;

img#tablet {display: none;}
img#desktop {display: none;}

The tablet css:

@media only screen and (min-width: 641px) {
img#phone {display: none;}

img#tablet {
    display: block;
    margin: 6em auto 0 auto;
    width: 70%;

And the desktop css:

@media only screen and (min-width: 1141px) {
img#tablet {display: none;}

img#desktop {
    display: block;
    margin: 6em auto 0 auto;
    width: 80%;

Good luck and let me know how it works for you.

@Evgenia Manolova 2017-03-14 23:10:18

The question is not how to set a display: none on an image. It's: Does “display:none” prevent an image from loading?

@Joakim Ling 2016-11-29 11:06:02

Use @media query CSS, basically we just release a project where we had an enormous image of a tree on desktop at the side but not showing in table/mobile screens. So prevent image from loading its quite easy

Here is a small snippet:

.tree {
    background: none top left no-repeat; 

@media only screen and (min-width: 1200px) {
    .tree {
        background: url(enormous-tree.png) top left no-repeat;

You can use the same CSS to show and hide with display/visibility/opacity but image was still loading, this was the most fail safe code we came up with.

@ats 2016-01-06 20:46:40

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.

Just expanding on Brent's solution.

You can do the following for a pure CSS solution, it also makes the img box actually behave like an img box in a responsive design setting (that's what the transparent png is for), which is especially useful if your design uses responsive-dynamically-resizing images.

<img style="display: none; height: auto; width:100%; background-image: 
url('img/1078x501_1.jpg'); background-size: cover;" class="center-block 
visible-lg-block" src="img/400x186_trans.png" alt="pic 1 mofo">

The image will only be loaded when the media query tied to visible-lg-block is triggered and display:none is changed to display:block. The transparent png is used to allow the browser to set appropriate height:width ratios for your <img> block (and thus the background-image) in a fluid design (height: auto; width: 100%).

1078/501 = ~2.15  (large screen)
400/186  = ~2.15  (small screen)

So you end up with something like the following, for 3 different viewports:

<img style="display: none; height: auto; width:100%; background-image: url('img/1078x501_1.jpg'); background-size: cover;" class="center-block visible-lg-block" src="img/400x186_trans.png" alt="pic 1">
<img style="display: none; height: auto; width:100%; background-image: url('img/517x240_1.jpg'); background-size: cover;" class="center-block visible-md-block" src="img/400x186_trans.png" alt="pic 1">
<img style="display: none; height: auto; width:100%; background-image: url('img/400x186_1.jpg'); background-size: cover;" class="center-block visible-sm-block" src="img/400x186_trans.png" alt="pic 1">

And only your default media viewport size images load during the initial load, then afterwards, depending on your viewport, images will dynamically load.

And no javascript!

@James Westgate 2016-03-11 15:39:34

Another possibility is using a <noscript> tag and placing the image inside the <noscript> tag. Then use javascript to remove the noscript tag as you need the image. In this way you can load images on demand using progressive enhancement.

Use this polyfill I wrote to read the contents of <noscript> tags in IE8

@Superfly5000 2015-11-27 10:05:38

we're talking about images not loading on mobile, right? so what if you just did an @media (min-width: 400px){background-image:thing.jpg}

wouldn't it then only look for the image at above a certain screen width?

@ma11hew28 2015-09-06 19:19:53

To prevent fetching resources, use the <template> element of Web Components.

@durron597 2015-09-06 23:19:45

Could you explain how this would work in more detail please?

@onlyurei 2014-10-29 20:16:13

Quirks Mode: images and display: none

When image has display: none or is inside an element with display:none, the browser may opt not to download the image until the display is set to another value.

Only Opera downloads the image when you switch the display to block. All other browsers download it immediately.

@Dorian 2012-08-28 11:54:33

Yes it will render faster, slightly, only because it doesn't have to render the image and is one less element to sort on the screen.

If you don't want it loaded, leave a DIV empty where you can load html into it later containing an <img> tag.

Try using firebug or wireshark as I've mentioned before and you'll see that the files DO get transferred even if display:none is present.

Opera is the only browser which will not load the image if the display is set to none. Opera has now moved to webkit and will render all images even if their display is set to none.

Here is a testing page that will prove it:

@Brent 2013-03-11 19:01:46

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.

@ryandlf 2013-09-16 06:08:54

This is actually a very useful tip for responsive design in my opinion.

@Brent 2014-02-04 20:06:47

This works in FF, Chrome, Safari, Opera, Maxthon. I haven't tried any IE.

@CoolCmd 2014-05-29 16:13:44

Breaking new from the front! <div hidden style="display:none;width:0;height:0;visibility:hidden;backg‌​round:url('test.jpg'‌​)"></div>. Results: Firefox 29 & Opera 12 don't load. IE 11 & Chrome 34 load.

@Qtax 2015-03-30 15:50:40

@CoolCmd for responsive design this is still a viable option, as you can in your CSS media query set background-image to none for cases where you do not want the image to load. Haven't found any better alternative for this use case that doesn't use JS.

@binaryfunt 2015-09-17 18:34:47

I tested an image slider (that used background images) that had a min-width media query. Below that width, the parent div had CSS display: none;. Network testing with window below that width: Chrome 35, IE11 and Edge didn't load the images

@James Westgate 2016-03-11 15:36:27

But not very semantic or accessible. You will have to do a lot of work to reliably have a screenreader read that out correctly when it's time to show the image.

Related Questions

Sponsored Content

22 Answered Questions

[SOLVED] Vertically align text next to an image?

30 Answered Questions

[SOLVED] How do I give text or an image a transparent background using CSS?

  • 2009-04-30 09:00:02
  • Stijn Sanders
  • 1524794 View
  • 2292 Score
  • 30 Answer
  • Tags:   html css opacity

16 Answered Questions

[SOLVED] MVC4 StyleBundle not resolving images

18 Answered Questions

[SOLVED] What is the difference between visibility:hidden and display:none?

  • 2008-09-25 12:37:47
  • Chris Noe
  • 582841 View
  • 1184 Score
  • 18 Answer
  • Tags:   css visibility

19 Answered Questions

[SOLVED] Responsive css background images

15 Answered Questions

[SOLVED] Managing CSS Explosion

  • 2010-02-12 16:03:34
  • JasCav
  • 45120 View
  • 683 Score
  • 15 Answer
  • Tags:   css organization

14 Answered Questions

[SOLVED] Is there an equivalent to background-size: cover and contain for image elements?

  • 2012-07-26 13:55:20
  • stormpat
  • 188710 View
  • 242 Score
  • 14 Answer
  • Tags:   html image css

3 Answered Questions

[SOLVED] Why do browsers match CSS selectors from right to left?

Sponsored Content