Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

There is an alternative at source tag to deliver media base on screen size using CSS property background-image

I want to deliver the lighter image as possible for my website. To accomplish that, usually use source with that sort of HTML code :

<picture>
    <source
      srcset="assets/images/compressed/litopia-s4-256.avif"
      media="(max-width: 512px)"
      type="image/avif"
    />
    <source
      srcset="assets/images/compressed/litopia-s4-512.avif"
      media="(max-width: 1024px)"
      type="image/avif"
    />
    <source
      srcset="assets/images/compressed/litopia-s4-1024.avif"
      media="(max-width: 2048px)"
      type="image/avif"
    />
    <!-- webp -->
    <source
      srcset="assets/images/compressed/litopia-s4-256.webp"
      media="(max-width: 512px)"
      type="image/webp"
    />
    <source
      srcset="assets/images/compressed/litopia-s4-512.webp"
      media="(max-width: 1024px)"
      type="image/webp"
    />
    <source
      srcset="assets/images/compressed/litopia-s4-1024.webp"
      media="(max-width: 2048px)"
      type="image/webp"
    />
    <!-- jpeg -->
    <source
      srcset="assets/images/compressed/litopia-s4-256.jpg"
      media="(max-width: 512px)"
      type="image/jpeg"
    />
    <source
      srcset="assets/images/compressed/litopia-s4-512.jpg"
      media="(max-width: 1024px)"
      type="image/jpeg"
    />
    <source
      srcset="assets/images/compressed/litopia-s4-1024.jpg"
      media="(max-width: 2048px)"
      type="image/jpeg"
    />
    <img src="/assets/images/litopia-port-s4.png" alt="Litopia Season 4">
</picture>

And I would like to know if there is something similar when I intend to deliver an image using background-image in CSS

like this :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

.bg-img{
  background-image: url("/assets/images/litopia-s4.png");
  background-size:cover;
}

>Solution :

You can definitely do that with media queries :

@media (max-width: 1024px) {
  .bg-img{
     background-image: url("/assets/images/litopia-s4-512.png");
     background-size:cover;
  }
}
@media (max-width: 512px) {
  .bg-img{
     background-image: url("/assets/images/litopia-s4-256.png");
     background-size:cover;
  }
}

Pay attention to the sequential order of Media Queries : it matters. (from the highest to lowest resolution)

Edit

Didn’t see @Kaiido comment before posting. But yeah, to check if your media format is handle, Modernizr would be the way to go I think.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading