[HTML5] Show Different Variations of Images Depending on the Viewport Width using Art Direction

For small viewports, we may want to show a variation of the desktop image. A very common use case of this is a cropped, smaller version of the desktop image. In this lesson, we are going to discuss showing a cropped version of the desktop image, but only for smaller viewports.

 

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, maximum-scale=1" />
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/marx/2.0.7/marx.min.css">
  <link rel="stylesheet" href="style.css">
</head>
<body>

  <picture>
    <!-- Show a cropped size image -->
    <source
      media="(max-width: 500px)"
      srcset="https://imgur.com/SXwenFN.jpg"
      sizes="90vw"
    >
    <img
      src="https://imgur.com/stCVVhQ.jpg"
      alt="Mother sheep and her lambs chilling in a field"
    >
  </picture>
    

  
</body>

</html>

 

posted @ 2018-10-22 19:58  Zhentiw  阅读(116)  评论(0编辑  收藏  举报