今天早上我分叉并修改了一个简单的代码笔,同时我一直在尝试理解 srcset 和尺寸标记以及如何在某些断点处选择图像背后的数学。这是笔:http ://codepen.io/patrickwc/pen/Ijuwq
复制到这里:
html:
<div class="container">
<div class="container__item">
<img
src="http://placehold.it/320x320"
srcset="http://placehold.it/1536x1536 1536w,
http://placehold.it/1024x1024 1024w,
http://placehold.it/900x900 900w,
http://placehold.it/600x600 600w,
http://placehold.it/320x320 320w"
sizes="(min-width: 30em) 50vw, (min-width: 62.5em) 25vw, 100vw"
alt="Testing, testing">
</div><!--
--><div class="container__item">
<img
src="http://placehold.it/320x320"
srcset="http://placehold.it/1536x1536 1536w,
http://placehold.it/1024x1024 1024w,
http://placehold.it/900x900 900w,
http://placehold.it/600x600 600w,
http://placehold.it/320x320 320w"
sizes="(min-width: 30em) 50vw, (min-width: 62.5em) 25vw, 100vw"
alt="Testing, testing">
</div><!--
--><div class="container__item">
<img
src="http://placehold.it/320x320"
srcset="http://placehold.it/1536x1536 1536w,
http://placehold.it/1024x1024 1024w,
http://placehold.it/900x900 900w,
http://placehold.it/600x600 600w,
http://placehold.it/320x320 320w"
sizes="(min-width: 30em) 50vw, (min-width: 62.5em) 25vw, 100vw"
alt="Testing, testing">
</div><!--
--><div class="container__item">
<img
src="http://placehold.it/320x320"
srcset="http://placehold.it/1536x1536 1536w,
http://placehold.it/1024x1024 1024w,
http://placehold.it/900x900 900w,
http://placehold.it/600x600 600w,
http://placehold.it/320x320 320w"
sizes="(min-width: 30em) 50vw, (min-width: 62.5em) 25vw, 100vw"
alt="Testing, testing">
</div><!--
--><div class="container__item">
<img
src="http://placehold.it/320x320"
srcset="http://placehold.it/1536x1536 1536w,
http://placehold.it/1024x1024 1024w,
http://placehold.it/900x900 900w,
http://placehold.it/600x600 600w,
http://placehold.it/320x320 320w"
sizes="(min-width: 30em) 50vw, (min-width: 62.5em) 25vw, 100vw"
alt="Testing, testing">
</div><!--
--><div class="container__item">
<img
src="http://placehold.it/320x320"
srcset="http://placehold.it/1536x1536 1536w,
http://placehold.it/1024x1024 1024w,
http://placehold.it/900x900 900w,
http://placehold.it/600x600 600w,
http://placehold.it/320x320 320w"
sizes="(min-width: 30em) 50vw, (min-width: 62.5em) 25vw, 100vw"
alt="Testing, testing">
</div><!--
--><div class="container__item">
<img
src="http://placehold.it/320x320"
srcset="http://placehold.it/1536x1536 1536w,
http://placehold.it/1024x1024 1024w,
http://placehold.it/900x900 900w,
http://placehold.it/600x600 600w,
http://placehold.it/320x320 320w"
sizes="(min-width: 30em) 50vw, (min-width: 62.5em) 25vw, 100vw"
alt="Testing, testing">
</div><!--
--><div class="container__item">
<img
src="http://placehold.it/320x320"
srcset="http://placehold.it/1536x1536 1536w,
http://placehold.it/1024x1024 1024w,
http://placehold.it/900x900 900w,
http://placehold.it/600x600 600w,
http://placehold.it/320x320 320w"
sizes="(min-width: 30em) 50vw, (min-width: 62.5em) 25vw, 100vw"
alt="Testing, testing">
</div>
</div>
CSS:
*, *:before, *:after {
box-sizing: border-box;
}
body {
margin: 0;
padding: 0;
}
img {
display: block;
max-width: 100%;
height: auto;
}
.container {
margin: 0;
padding: 0;
background: red;
list-style: none;
}
.container__item {
display: inline-block;
vertical-align: top;
margin: 0;
padding: 0;
width: 100%;
}
@media (min-width: 30em) {
.container__item {
width: 50%;
}
}
@media (min-width: 62.5em) {
.container__item {
width: 25%;
}
}
直到62.5em
断点为止,这对我来说是有意义的。这是 1000 像素。考虑到我已经在sizes
属性中指定,at(min-width: 62.5em) 25vw
图片应该占屏幕大小的四分之一,为什么600 x 600最先加载?1000 / 4 = 250,所以 320 的图片就可以了。我已经阅读了很多关于图片填充的内容,并阅读了 Eric Portis 和 Chris Coyier 关于这个主题的一些很棒的帖子。我不明白我做错了什么,或者它只是一个奇怪的错误?