I'm trying to implement the new srcset
attribute for <img>
and I've run into some edge cases that are not working or I'm missing something.
I'm using the picturefill 2.1.0 polyfill, and I've read some articles like http://ericportis.com/posts/2014/srcset-sizes/ or https://html.spec.whatwg.org/multipage/embedded-content.html#embedded-content.
I get the idea of very simple examples, e.g:
<img
src="small.jpg"
srcset="
small.jpg 100w,
medium.jpg 200w,
large.jpg 300w
"
sizes="100vw"
>
The browser loads small.jpg, medium.jpg or large.jpg depending on screen size, pixel density, zoom and other factors. It may load medium.jpg for a 200px simple screen or a 100px hdpi(2x) display. So far so good.
The problem relies on the sizes
attribute. In the previous example we are telling the browser that the picture takes up the whole (100%) viewpoint width (vw).
The project I'm working in uses Foundation, which has fluid, em-padded grids that may have more or less columns based on screen sizes (media queries).
Let's say, for instance, that we want a grid in which small screens have 2 columns, and medium screens (min-width: 40em
) 4 columns. Every column contains an image. What would be the correct sizes
keeping in mind that each column is width-fluid and has a padding defined in em
s?
<ul class="small-block-grid-2 medium-block-grid-4">
<li>
<img
src="small.jpg"
srcset="
small.jpg 160w,
medium.jpg 320w,
large.jpg 480w
"
>
</li>
</ul>
sizes="(min-width: 40em) 25vw, 50vw"
sizes="(min-width: 40em) ???em, ???em"
sizes="(min-width: 40em) ???px, ???px"
The vw
approach ignores the padding of the columns. The em
or px
approach ignores the fact that columns are fluid (and I'm not even sure which values should they have).
Any ideas?
Thanks in advance.
em
s are relative to root font-size orimg
's container font-size? – Petit Saltamartísizes
attribute is only used while pre-loading images? I mean, if I say that an<img>
is100vw
, but for some reason I'm wrong, or the page changes via JS, or whatever, and the image becomes 50vw, the browser will be smart enough to ignore my100vw
declaration and pick up the correct file? – Petit Saltamartí