Responsive & Dynamic Background Image - best practice

For a long time, when I had to create a website with a lot of different pages (and with every pages having a hero section with a different background image), I used to do like this:

<div class="hero" style="background-image: url(my-dynamic-img.jpg)"></div>

And the following css:

.hero {
   background-repeat: no-repeat;
   background-position: center center;
   background-size: cover;
}

The image would have been loaded dynamically for each pages. In a CMS for exemple, the client would have been able to change images on it's own, without changing the css

But now, I realize it's not good at all, because you end up with loading the same image on every device (mobile, tablet, desktop...) .

So I'd like to have your opinion on the best way to do this: having 3 different images (hero-mobile.jpg, hero-tablet.jpg and hero-desktop.jpg) for the same background and target the good one automatically. The declaration of the 3 images has to be in the HTML, not in the css (so the client can change it at any time)

Answers:

Answer

Have you discovered the srcset attribute? What srcset does is it allows you to add more than one image in a <img> tag and set certain conditions to it. Depending on which condition is met, the corresponding image will be shown. Time for an example

If you want an image to be viewed half of the users viewport width you would use

<img src="images/space-needle.jpg" sizes="50vw"
srcset="images/space-needle.jpg 200w, images/space-needle-2x.jpg 400w,
images/space-needle-hd.jpg 600w">  

What this does is it finds how large the users viewport width is, say its 500px wide, so your image images/space-needle.jpg 200w will load for the user.

In this example we specified that the image needs to take up half the screens width sizes="50vw". It would be impossible to show an image at 600px or 400px wide on a 500px wide screen and comfortably have it shown only at half the viewport so it chooses the 200w option.

There are many ways to specify conditions for which image should load, device pixel ratio, screen size, browser size so on and so forth.

Eduational links:
https://www.sitepoint.com/how-to-build-responsive-images-with-srcset/
https://www.udacity.com/course/responsive-images--ud882

Answer

Well there are multiple ways of doing this , but in your case, since you want the client to modify the HTML, you should use the @media in your css:

E.g

Then in the CSS you should have a media query for each

@media (min-width: 430px) {


    .tablet {
        display:block !important;
    }
      .mobile {
        display: none !important;
    }
    .desktop{
      display: none !important;
    }
}

@media (max-width: 600px) {
    .mobile {
        display:block !important;
    }
    .tablet {
       display: none !important;
    }
    .desktop{
      display: none !important;
    }
}

@media (min-width: 900px) {
    .tablet {
       display: none !important;
    }
      .mobile {
       display: none !important;
    }
    .desktop{
      display:block !important;
    }
}

Example : https://plnkr.co/edit/Cet7wG4qrK9DcG1vlOVg?p=preview

Answer

we will use media screen to display images in mobile , laptop and disktop screens

.hero img{
   background-repeat: no-repeat;
   background-position: center center;
   background-size: cover;
   width:100%;
   }
  @media screen and (max-width: 500px){
        .hero img{
            display: block !important;
        }
    }
    @media screen and (min-width: 501px) and (max-width:880px){
        .hero1 img{
            display: block !important;
        }
    }
    @media screen and (min-width: 881px){
        .hero2 img{
            display: block !important;
        }  
    }
    <div class="hero" >
         <img src="https://cdn.pixabay.com/photo/2013/06/09/18/50/tulip-123795_960_720.jpg" alt="" style="display: none">
     </div>
     <div class="hero1">
         <img src="https://cdn.pixabay.com/photo/2012/03/03/23/13/tulips-21598_960_720.jpg" alt="" style="display: none">
     </div>
     <div class="hero2">
         <img src="https://cdn.pixabay.com/photo/2016/10/07/14/01/tangerines-1721620_960_720.jpg" alt="" style="display: none">
     </div>

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.