Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct usage of media viewport for image with background

I am not in any way a front-end dev, but I am writing a simple one-page website which contains an image. I have create few versions of this image to minimize the bits to be downloaded in small-screen devices. This image is supposed to be "expanded" horizontally, so I also created some background images which are supposed to be repeated horizontally. See below mockup:

mockup

Below the code which I wrote. Is this the correct approach? Is there any bug in my code or is this the correct way to use viewports? Although it works fine on my 320px iPhone screen, I can't get the 320px version to work on my desktop browser. Overall I am not satisfied.

HTML:

<div id="image" class="fluid-container">
  <center>
    <picture id="banner">
      <source media="(max-width: 320px)" srcset="320w.jpg">
      <source media="(min-width: 1200px)" srcset="1200w.jpg">
      <source media="(min-width: 800px)" srcset="800w.jpg">
      <source media="(min-width: 480px)" srcset="480w.jpg">
      <img src="800w.jpg">
    </picture>
  </center> 
</div>

CSS:

<style>
    #image {
      background-image: url("r320w.png");
      background-repeat: repeat;
    }
    @media (min-width: 480px) {
      #image {
        background-image: url("r480w.png");
        background-repeat: repeat;
      }
    }
    @media (min-width: 800px) {
      #image {
        background-image: url("r800w.png");
        background-repeat: repeat;
      }
    }
    @media (min-width: 1200px) {
      #image {
        background-image: url("r1200w.png");
        background-repeat: repeat;
      }
    }
</style>
like image 378
JoeSlav Avatar asked Oct 16 '22 20:10

JoeSlav


1 Answers

Have a look at this pen : https://codepen.io/anon/pen/XqJRKM

Essentially, the solution is:

#image {
  background-image: url("http://lorempixel.com/output/sports-h-c-1-480-10.jpg");
  background-repeat: repeat;
  background-size:contain;
  width: 100vw;
  background-color:#f00;
}

@media (min-width: 480px) {
  #image {
    background-image: url("http://lorempixel.com/output/sports-h-c-1-480-5.jpg");
    background-repeat: repeat;
  }
}
@media (min-width: 800px) {
  #image {
    background-image: url("http://lorempixel.com/output/sports-h-c-1-480-6.jpg");
    background-repeat: repeat;
  }
}
@media (min-width: 1200px) {
  #image {
    background-image: url("http://lorempixel.com/output/sports-h-c-1-480-7.jpg");
    background-repeat: repeat;
  }
}

#image img{
  width:80vw;
  height:auto;
  max-width:1200px;
}

html :

<html>

<div id="image" class="fluid-container">
  <center>
    <picture>
        <source media="(max-width: 600px)" srcset="image1.png">
        <source media="(min-width: 600px) and (max-width : 800px)" srcset="image2.jpg">
        <source media="(min-width: 800px) and (max-width : 1200px)" srcset="image3.png">
        <source media="(min-width: 1200px)" srcset="image4.jpg">
        <img src="original_image.jpg">
    </picture>
  </center> 
</div>

Note that with this example, original_image.jpg will only show on old browsers. It will always be overwritten on browsers above Chrome v38

like image 70
Leon Avatar answered Oct 27 '22 07:10

Leon