0

I'm looking for a solution to suppress/limit downloads for images that are hidden at certain breakpoints, using srcset. I'd like to avoid using background images and javascript.

Wondering if this is a legitimate approach:

@media screen and (max-width: 768px) {
  .container {
    display: none;
  }
}
<div class="container">
  <img src="http://lorempixel.com/1200/1200" 
       srcset="http://lorempixel.com/1200/1200 1200w, http://lorempixel.com/1000/1000 1000w, http://lorempixel.com/800/800 800w, https://upload.wikimedia.org/wikipedia/commons/c/ce/Transparent.gif 1w" 
       sizes="(max-width: 768px) 1px, 100vw, (min-width: 1920px) 50vw,">
</div>

The idea being that where the viewport ≤ 768px (where the containing div is hidden), the user is served a 1px transparent gif rather than a weightier image.

Jiiiim
  • 1
  • 2

1 Answers1

1

With this approach, you still make the browser download the 1px gif.

Unfortunately, there is currently no way to really prevent downloads on some viewport sizes with only HTML.

You can either:

You can also join this discussion to explain your use case and help the standard evolve on this topic.

Community
  • 1
  • 1
Nicolas Hoizey
  • 1,954
  • 1
  • 17
  • 24