html - 图像 srcset 大小

标签 html css image srcset

我有这个:

  <img class="poster" srcset="https://image.tmdb.org/t/p/original/eSzpy96DwBujGFj0xMbXBcGcfxX.jpg 1024w,
                                        https://image.tmdb.org/t/p/w780/eSzpy96DwBujGFj0xMbXBcGcfxX.jpg 700w,
                                        https://image.tmdb.org/t/p/w500/eSzpy96DwBujGFj0xMbXBcGcfxX.jpg 400w"
                                sizes="100vw" 
            />

我希望浏览器在屏幕的底部加载图像,如果是移动设备 w500、平板电脑 w780 和 pc 原始设备,我该怎么做?

最佳答案

如果您需要为所有视口(viewport)尺寸显示相同的图像,那么我建议您使图像表现得像 flex-box。使其响应。通过这样做,您不必担心 @media (min-width)断点。并且图像将始终填满整个视口(viewport)宽度(100vw)。

Here is such a code :

body {
  margin: 0;
  padding: 0;
}

.poster {
  max-width: 100%;
  display: block;
  height: auto;
}
<img class="poster" src="https://image.tmdb.org/t/p/original/eSzpy96DwBujGFj0xMbXBcGcfxX.jpg" />

Using <img srcset="" sizes=""/> is recommended if you need to change the image for users on different displays.
If you want the userswith mobile to view Image-1,
Users with tablet to view Image-2 and
Users with desktops to view Image-3,
then you can do so easily by using <img srcset="" sizes=""/>.

这是实现它的代码示例,

body {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: #222;
  margin: 0;
  padding: 0;
  height: 100vh;
}
<img src="https://picsum.photos/1024/512?image=0"
     srcset="https://picsum.photos/1024/512?image=2 1024w,
             https://picsum.photos/780/390?image=4 780w,
             https://picsum.photos/500/250?image=6 500w"
     sizes="(min-width: 1024px) 1024px, /* For Desktop users */
            (min-width: 780px) 780px, /* For Tablet users */
            500px">  /* Default for Mobile users */
更改视口(viewport)宽度,您会看到图像在宽度增加的同时发生变化。


希望对您有所帮助。

和平🖖

关于html - 图像 srcset 大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54960795/

相关文章:

php - 分页中的图像加载器

css 继承问题

javascript - 我如何设置 ChartJS 的样式使其始终位于 div 的底部?

css - 如何防止 CSS 渐变 strip ?

java - 使用java计算图像中的亮度、对比度、色调和饱和度

javascript - 使用 jquery 添加 CSS

javascript - 无法弄清楚如何根据日期自动显示文本内容

html - 如何使我的 5 列元素像下图一样对齐

html - 放大dv中的图像

algorithm - 什么是最好的图像缩小算法(质量方面)?