-1

I am building a responsive website. This website needs a lot of background images (both vector and raster formats).

I want to optimize the website for loading speed but not decrease picture quality.

My understanding is that I should have different versions for the same picture so that for small window devices the user automatically downloads the correct smallest file size.

My question is: What is the best way to deal with this problem? Do I need to use media queries? how to use them such that different browsers actually don't download unnecessary data?

gota
  • 2,338
  • 4
  • 25
  • 45

1 Answers1

1

Well, another post here on SO discussed if an unused css image url(''); is downloaded by the browser even if no element matches the rule. And it's been tested and established that on all major browsers, the images won't be downloaded unless an element actually matches the selector.

Post: Are unused CSS images downloaded?

It means that, if you define media queries for your styles, only the images inside rules that match the specific viewport will be downloaded.

In the following example, if you're in a device which is 480px wide or less, don't worry because large.jpg won't be downloaded.

@media (max-width: 480px) {
    #myDiv {
        background-image: url('small.jpg');
    }
}

@media (min-width: 481px) {
    #myDiv {
        background-image: url('large.jpg');
    }
}

Just reminding that this is a behavior left up to the browsers, so they can change anytime they want.

Community
  • 1
  • 1
LcSalazar
  • 16,524
  • 3
  • 37
  • 69