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

本文探讨了在小视口下展示桌面版图片裁剪版本的方法,通过使用HTML的picture元素和source元素,实现了根据不同屏幕尺寸加载不同版本的图片,确保了在小设备上也能获得良好的视觉效果。

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>

 

转载于:https://www.cnblogs.com/Answer1215/p/9832377.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值