A place to cache linked articles (think custom and personal wayback machine)
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

4 роки тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  1. title: When Responsive Images Get Ugly
  2. url: http://codepen.io/Tigt/post/when-responsive-images-get-ugly
  3. hash_url: 16d800a1faadde1445d0c2332f5b6f3c
  4. <p>I don’t know about you, but I’m over the current crop of “How to use our new friends <code>&lt;picture&gt;</code> and <code>srcset</code>” tutorials. They show you some Baby’s First Markup which is nice for teaching purposes, but doesn’t prepare you for the ugliness that more… unusual applications require.</p>
  5. <p>I’ve encountered a few corner-cases and quirks to beware of and found a few of said unusual applications. Maybe they can help you.</p>
  6. <nav class="toc">
  7. <h2>Contents</h2>
  8. <ol>
  9. <li><a href="#some-ground-floor-stuff">Some ground floor stuff</a>
  10. <ol>
  11. <li><a href="#why-bother">Why bother?</a></li>
  12. <li><a href="#srcset-is-preferable">srcset is preferable</a></li>
  13. </ol>
  14. </li>
  15. <li><a href="#unintuitive-behavior-gotchas">Unintuitive behavior &amp; gotchas</a>
  16. <ol>
  17. <li><a href="#media-conditions-not-media-queries">Media conditions, NOT media queries</a></li>
  18. <li><a href="#troublesome-css-units">Troublesome CSS units</a></li>
  19. <li><a href="#sizes-will-affect-how-your-image-displays">sizes will affect how your image displays</a></li>
  20. </ol>
  21. </li>
  22. <li><a href="#media-aware-images">Media-aware images</a>
  23. <ol>
  24. <li><a href="#printer-friendly">Printer-friendly</a></li>
  25. <li><a href="#e-ink-friendly">E-ink-friendly</a></li>
  26. <li><a href="#night-mode-bright-sunlight">Night mode &amp; bright sunlight</a></li>
  27. </ol>
  28. </li>
  29. <li><a href="#the-deepest-backwards-compatibility-possible">The deepest backwards-compatibility possible</a></li>
  30. <li><a href="#height-and-width-constrained-srcset">Height and width-constrained srcset</a>
  31. <ol>
  32. <li><a href="#object-fit-cover">object-fit: cover</a></li>
  33. <li><a href="#object-fit-contain">object-fit: contain</a></li>
  34. </ol>
  35. </li>
  36. <li><a href="#responsive-bitmaps-inside-inline-svg">Responsive bitmaps inside inline SVG</a>
  37. <ol>
  38. <li><a href="#fixing-ie">Fixing IE</a></li>
  39. <li><a href="#building-the-sizes">Building the sizes</a></li>
  40. <li><a href="#making-things-worst-with-not-quite-entire-viewport-scaling">Making things worst with not-quite-entire-viewport scaling</a>
  41. <ol>
  42. <li><a href="#the-browser-viewport">The browser viewport</a></li>
  43. <li><a href="#the-svg-element">The <code>&lt;svg&gt;</code> element</a></li>
  44. <li><a href="#the-viewbox">The viewBox</a></li>
  45. <li><a href="#the-image">The <code>&lt;image&gt;</code></a></li>
  46. <li><a href="#assembling">Assembling</a></li>
  47. </ol>
  48. </li>
  49. </ol>
  50. </li>
  51. <li><a href="#thats-all-folks">That’s all, folks</a></li>
  52. </ol>
  53. </nav>
  54. <h2 id="some-ground-floor-stuff">Some ground floor stuff</h2>
  55. <p>I’m assuming you already have a basic grasp of responsive images. If not, I recommend <a href="http://blog.cloudfour.com/responsive-images-101-definitions/">Cloud Four’s introduction series</a>.</p>
  56. <h3 id="why-bother">Why bother?</h3>
  57. <p>If the ensuing code has you despairing that responsive images are more trouble than they’re worth, refer to this section to remember that <strong>overlarge images are painful,</strong> in a variety of ways:</p>
  58. <dl class="bulleted">
  59. <dt>Download size</dt>
  60. <dd>Doubling an image’s pixel density quadruples its area and can <em>sextuple</em> its filesize! Have a data plan? Not anymore you don’t.</dd>
  61. <dt>CPU load &amp; battery drain</dt>
  62. <dd>Images also need to be decoded. Shrinking ‘em with CSS? Now the browser needs to scale and resample, too. The difficulty of all three grows exponentially with image size.</dd>
  63. <dt>Memory usage</dt>
  64. <dd>Big photographs <a href="https://twitter.com/keithclarkcouk/status/605983582709010432">can cause juddering in a mobile browser all by themselves</a>. Resizing with CSS doesn’t help; the browser keeps the full thing around for zooming.</dd>
  65. </dl>
  66. <p>An oversized image wastes time, bandwidth, data, battery, and system resources. Even if you don’t use a polyfill, you should use responsive images for <a href="http://caniuse.com/#feat=srcset">the 56% (and growing!) of browsers that support them</a>; anything else would be irresponsible.</p>
  67. <h3 id="srcset-is-preferable"><code>srcset</code> is preferable</h3>
  68. <p><code>&lt;picture&gt;</code> is darling, but a plain <code>&lt;img srcset&gt;</code> is more flexible: bandwidth-saving modes, browser heuristics, and user choices. It’s also more future-friendly; <code>&lt;picture&gt;</code> is only as good as your media queries. <code>srcset</code> is the smart choice.</p>
  69. <h2 id="unintuitive-behavior-gotchas">Unintuitive behavior &amp; gotchas</h2>
  70. <p>Like everything else on the front-end, responsive images are fraught with edge cases and unforeseen applications. I’ve collected a couple things that have tripped me up, in the hopes that they won’t do the same to you.</p>
  71. <p>The most important <em>and</em> trickiest part of responsive images is the <code>sizes</code> attribute. If it doesn’t tell the browser <em>exactly</em> how big the image is going to be, you risk either a scaled-up mess or a scaled-down bloat. So naturally the <code>sizes</code> syntax is error-prone.</p>
  72. <p>In Standardese, the <code>sizes</code> attribute takes a comma-separated list of <code>&lt;source-size-value&gt;</code>s, which break down into a <b>media condition</b> (not a media query!) and a <b>length</b>, like this:</p>
  73. <pre>
  74. <code>sizes="(media: condition) length,
  75. (media: condition) length,
  76. (media: condition) length"
  77. </code>
  78. </pre>
  79. <p>The length is the width of the image if the media condition is true. It accepts a value, like <code>700px</code>, or a <code>calc()</code> expression.</p>
  80. <p>Media conditions are almost media queries. The spec <a href="http://www.w3.org/TR/mediaqueries-4/#typedef-media-condition">codifies them in a formal CSS grammar</a>, but for us <em>mere mortals</em> the rules are:</p>
  81. <ul>
  82. <li>You cannot use media types (<code>screen</code>, <code>print</code>, <code>tv</code>, etc.)</li>
  83. <li>Use any <code>(feature: value)</code> pairs you like.</li>
  84. <li>You can chain them with the keywords <code>and</code>, <code>or</code>, and <code>not</code>. Do <strong>not</strong> use commas as a substitute for <code>or</code>.</li>
  85. <li>If using the <code>or</code> or <code>not</code> keywords, you must wrap the entire media condition in parentheses.</li>
  86. <li>If empty, it always evaluates to true.</li>
  87. </ul>
  88. <p>But I've always preferred examples. Much simpler to get the shape of things from them than trying to string together a buncha rules.</p>
  89. <figure>
  90. <figcaption>Some valid media conditions:</figcaption>
  91. <ul>
  92. <li><code>(min-width: 500px)</code>
  93. </li><li><code>(max-width: 300px) and (orientation: portrait)</code>
  94. </li><li><code>((min-height: calc(50vw - 100px)) or (light-level: dim))</code>
  95. </li><li><code> </code> (Yep, an empty M.C. is totes O.K.)
  96. </li>
  97. </ul>
  98. </figure>
  99. <figure>
  100. <figcaption>Some valid lengths:</figcaption>
  101. <ul>
  102. <li><code>200px</code>
  103. </li><li><code>40em</code>
  104. </li><li><code>calc(40rem - 10vw)</code>
  105. </li><li><code>78.95vmax</code>
  106. </li>
  107. </ul>
  108. </figure>
  109. <figure>
  110. <figcaption>Some valid <code>&lt;source-size-value&gt;</code>s:</figcaption>
  111. <ul>
  112. <li><code>(min-width: 500px) 200px</code>
  113. </li><li><code>(max-width: 300px) and (orientation: portrait) 40em</code>
  114. </li><li><code>((min-height: calc(50vw - 100px)) or (light-level: dim)) calc(40rem - 10vw)</code>
  115. </li><li><code>78.95vmax</code>
  116. </li>
  117. </ul>
  118. </figure>
  119. <p>Ugly? Sure. Powerful? You bet.</p>
  120. <p>Now, because this is web development, and <em>nothing can ever be easy</em>, browsers aren’t required to have fully implemented media condition grammar before they implement responsive images. You may need backup conditions to work around that:</p>
  121. <pre>
  122. <code>"((min-height: calc(50vw - 100px)) or (light-level: dim)) 500px,
  123. (min-height: calc(50vw - 100px) 500px,
  124. (light-level: dim) 500px"
  125. </code>
  126. </pre>
  127. <p>As always, test, test, test. I recommend <a href="http://placehold.it/">placehold.it</a> for images that explicitly indicate their widths; lessens ambiguity and saves you from having to check the <code>currentSrc</code> every time.</p>
  128. <h3 id="troublesome-css-units">Troublesome CSS units</h3>
  129. <p>
  130. <a href="http://www.w3.org/TR/html51/semantics.html#source-size-value">So sayeth the spec:</a>
  131. </p>
  132. <blockquote>
  133. <p><b>Note:</b> Percentages are not allowed in a <code>&lt;source-size-value&gt;</code>, to avoid confusion about what it would be relative to. The vw unit can be used for sizes relative to the viewport width.</p>
  134. </blockquote>
  135. <p>The browser image preloader wants the proper URL to request ASAP. It’s not waiting around for your CSS to download and parse, it needs units it can use <em>now.</em> And since the <code>%</code> unit only works if you know how big an element’s parents are, the CSS is required to calculate it.</p>
  136. <p><code>rem</code> and <code>em</code> <em>are</em> supported, but be careful. They’re resolved <a href="http://ircbot.responsiveimages.org/bot/log/respimg/2015-06-04#T132199">like they are inside media queries</a>: equal to the user’s default <code>font-size</code>. (Which is often 16px, but not always.) If you’re respecting that default, you should be good to go.</p>
  137. <p>But if you’re doing something like <code>html {font-size: 10px}</code> to make the math easier, well, don’t. That <a href="https://vasilis.nl/nerd/using-pixels-polite/">doesn’t respect the user</a>, and it prevents you from using <a href="http://blog.cloudfour.com/the-ems-have-it-proportional-media-queries-ftw/">the power of <code>em</code>s in media queries</a>.</p>
  138. <p>You also can’t do <code>html {font-size: 62.5%}</code> if you want to use <code>em</code> in <code>sizes</code>. What with preprocessors nowadays you don’t need to, and <a href="http://www.filamentgroup.com/lab/how-we-learned-to-leave-body-font-size-alone.html">embracing the default user <code>font-size</code> is a good practice</a>. Prevents a lot of <a href="https://vasilis.nl/nerd/weird-62-5-antipattern/">inheritance woes</a>.</p>
  139. <p>As for why you’d size an image with <code>em</code>in the first place…</p>
  140. <ul>
  141. <li>If your breakpoints are using <code>em</code>, you'll reflect them in your media conditions.</li>
  142. <li>If you’re sizing your text container with <code>em</code>s for <a href="http://maxdesign.com.au/articles/ideal-line-length-in-ems/">the perfect line length</a>, and images inside it are <code>max-width: 100%</code>, they can end up sized in <code>em</code>s too.</li>
  143. </ul>
  144. <p>As for those weirdo units, like <code>ch</code> or <code>ex</code> or <code>pc</code>… If you use them, Tim Berners-Lee calls Håkon W. Lie, who calls Bert Bos, and <em>he</em> calls the authorities.</p>
  145. <h3 id="sizes-will-affect-how-your-image-displays"><code>sizes</code> <em>will</em> affect how your image displays</h3>
  146. <p><code>sizes</code> appears to be just hints about how your CSS scales the image, but it actually changes how the image will be displayed all on its own. Observe:</p>
  147. <p/><p data-slug-hash="XbRaVP" data-default-tab="result" data-height="900" data-theme-id="14446" class="codepen"/>
  148. <p>If your screen is big, you can see the last image is way enormous. It doesn't have a <code>sizes</code> attribute, and browsers assume <code>sizes="100vw"</code> when that happens. Widespread misuse of this default is why <a href="http://alistapart.com/blog/post/article-update-dont-rely-on-default-sizes">it’s no longer valid to omit <code>sizes</code> on a responsive image</a>.</p>
  149. <p>If you aren’t using CSS to explicitly size the image at all times, like if you’ve got <code>img { max-width: 100% }</code> and not all your images will be as big as their containers, be careful and test to make sure your <code>sizes</code> attribute isn’t running amok. Browsers treat the length of the chosen <code>&lt;source-size-value&gt;</code> like <code>&lt;img width&gt;</code>: if you don’t override it with CSS, that’s how wide it will be.</p>
  150. <p>Using <code>&lt;picture&gt;</code> and the full power of the <code>media</code> attribute, you can make especially important images (logos, diagrams, etc.) adapt best to device capabilities and circumstances.</p>
  151. <h3 id="printer-friendly">Printer-friendly</h3>
  152. <pre>
  153. <code class="html">&lt;picture&gt;
  154. &lt;source srcset="logo-printer-friendly.svg" type="image/svg+xml" media="print, (monochrome: 1)"&gt;
  155. &lt;img src="logo.png" alt="Butt-Touchers Incorporated"&gt;
  156. &lt;/picture&gt;
  157. </code>
  158. </pre>
  159. <p>You’ve probably seen print stylesheet tutorials that <code>display: none</code> images and substitute printer-friendly versions as background images. Since not all browsers print with background images &amp; colors on (at least, not by default), this version is much more robust. For important content images, it’s <em>way</em> better.</p>
  160. <p>I also specify the image for devices that can’t display colors (<code>monochrome: 1</code>), because hey, we already made a B&amp;W version for saving ink.</p>
  161. <h3 id="e-ink-friendly">E-ink-friendly</h3>
  162. <p>A device that qualifies for <code>update-frequency: slow</code> (mostly e-Ink devices) probably wouldn’t play GIFs, but if it did, it would look very muddy and blurry as it would struggle to keep up.</p>
  163. <p>In either case, it would be much nicer to display a still frame that’s representative of the GIF instead. (As opposed to showing the first frame, which is an often unsuitable default.) So that’s what we do here, and hey, print doesn't have an update frequency, right?:</p>
  164. <pre>
  165. <code class="html">&lt;picture&gt;
  166. &lt;source srcset="reaction-frame.png" media="print, (update-frequency: slow)"&gt;
  167. &lt;img src="reaction.gif" alt="Say whaaaaat?"&gt;
  168. &lt;/picture&gt;
  169. </code>
  170. </pre>
  171. <h3 id="night-mode-bright-sunlight">Night mode &amp; bright sunlight</h3>
  172. <pre>
  173. <code class="html">&lt;picture&gt;
  174. &lt;source srcset="floorplan-dark.svg" media="(light-level: dim)"&gt;
  175. &lt;source srcset="floorplan-hicontrast.svg" media="(light-level: washed)"&gt;
  176. &lt;img src="floorplan.png" alt="The plans for how we'll renovate the apartment." longdesc="#our-plans" aria-describedby="our-plans"&gt;
  177. &lt;/picture&gt;
  178. </code>
  179. </pre>
  180. <p>Using light level media queries, we can adapt images to be higher contrast and visible in strong sunlight conditions (<code>washed</code>), or invert the colors to be less distracting in the dark (<code>dim</code>).</p>
  181. <p>These aren’t features you should scramble to implement, but keep them in your back pocket. The print-friendly one is useful today, <code>light-level</code> is gaining implementation (Firefox already supports it), and cheapo e-Ink displays have a wealth of possibilities for hardware innovation we haven’t seen yet.</p>
  182. <p>And you never know; you might someday find an obscure media query is perfect for your project, like <code>color-index</code> or <code>scan</code>.</p>
  183. <h2 id="the-deepest-backwards-compatibility-possible">The deepest backwards-compatibility possible</h2>
  184. <p>Start with a boring ol’ HTML 2.0 <code>&lt;img&gt;</code>:</p>
  185. <pre>
  186. <code class="html">&lt;img src="giraffe.jpg" alt="A giraffe."
  187. height="400" width="300"&gt;
  188. </code>
  189. </pre>
  190. <p>The key realization is that once we add <code>sizes</code> to this <code>&lt;img&gt;</code>, browsers that understand <code>&lt;picture&gt;</code> and width-based <code>srcset</code> will <em>ignore</em> the <code>src</code>, <code>height</code>, and <code>width</code> attributes.</p>
  191. <p><i>“Width-based <code>srcset</code>? What?”</i> Ah, my fine hypothetical friend, there are two kinds of <code>srcset</code> lists:</p>
  192. <dl>
  193. <dt>Width-based</dt>
  194. <dd><code>srcset="giraffe@1x.jpg 300w, giraffe@1.5x.jpg 450w, giraffe@2x.jpg 600w, giraffe@3x.jpg 900w"</code></dd>
  195. <dt>Pixel density-based</dt>
  196. <dd><code>srcset="giraffe@1.5x.jpg 1.5x, giraffe@2x.jpg 2x, giraffe@3x.jpg 3x"</code></dd>
  197. </dl>
  198. <p>The pixel density-based <code>srcset</code> (with the <code>x</code> descriptor) is the only kind that <a href="http://caniuse.com/#search=srcset">works in Safari, some earlier versions of Chrome &amp; Opera, and Microsoft Edge right now</a>. The newer width-based version (with the <code>w</code> descriptor) <a href="http://caniuse.com/#feat=picture">only works in the newest versions of Firefox, Opera, and Chrome</a>.</p>
  199. <p>We want to make our images as responsive as can be, so we should use both kinds of <code>srcset</code>, if possible. And we can! Just not quite how you would think. If we were to use a <code>srcset</code> with both <code>x</code> and <code>w</code> descriptors in it, like this:</p>
  200. <pre>
  201. <code>srcset="giraffe.jpg 100w, giraffe@2x.jpg 2x"
  202. </code>
  203. </pre>
  204. <p>Well, browsers will do <em>something,</em> but I guarantee it won’t be what you want. We’ll have to get multiple <code>srcset</code>s involved, using <code>&lt;picture&gt;</code> and <code>&lt;source&gt;</code>.</p>
  205. <p>First, for the almost-modern browsers, I'll bolt on the <code>x</code> descriptor <code>srcset</code>:</p>
  206. <pre>
  207. <code class="html">&lt;img src="giraffe.jpg" alt="A giraffe."
  208. srcset="giraffe@1.5x.jpg 1.5x,
  209. giraffe@2x.jpg 2x,
  210. giraffe@3x.jpg 3x"
  211. height="400" width="300"&gt;
  212. </code>
  213. </pre>
  214. <p>Unlike the <code>w</code> descriptor <code>srcset</code>, the <code>x</code> kind <em>does</em> take the <code>src</code> attribute into account. It assumes it’s <code>1x</code>, so you don’t have to specify it again. </p>
  215. <p>For <code>x</code> <code>srcset</code>, the <code>width</code> and <code>height</code> attributes just keep on doin’ what they’re doin’; all it does is swap out the image’s source if it’s on a higher-density screen. The new high-resolution source will take up the same amount of “CSS pixels” as before.</p>
  216. <p>Next, we’ll turbocharge the markup for browsers that understand <code>w</code> descriptor <code>srcset</code>:</p>
  217. <pre>
  218. <code class="html">&lt;picture&gt;
  219. &lt;source srcset="giraffe@1x.jpg 300w,
  220. giraffe@1.5x.jpg 450w,
  221. giraffe@2x.jpg 600w,
  222. giraffe@3x.jpg 900w"
  223. sizes="300px" type="image/jpeg"&gt;
  224. &lt;img src="giraffe.jpg" alt="A giraffe."
  225. srcset="giraffe@1.5x.jpg 1.5x,
  226. giraffe@2x.jpg 2x,
  227. giraffe@3x.jpg 3x"
  228. height="400" width="300"&gt;
  229. &lt;/picture&gt;
  230. </code>
  231. </pre>
  232. <p><i>“Hey, I thought you said that we should prefer plain <code>srcset</code>!”</i> Indeed, I did. If you look at the lone <code>&lt;source&gt;</code> element, we don’t have a <code>media</code> attribute, so browsers will use its <code>srcset</code> just like if it were on the <code>&lt;img&gt;</code> element instead. Same effect! (Note: the <code>type="image/jpeg"</code> isn't <em>necessary,</em> but the HTML won't validate without it.)</p>
  233. <p>This setup is as good as it gets, unless you want a responsive image polyfill for browsers that don’t understand <em>any</em> form of <code>srcset</code>. Me, I’m fine serving a sensible fallback <code>src</code> for my sites. Just don’t make it enormous!</p>
  234. <h2 id="height-and-width-constrained-srcset">Height <strong>and</strong> width-constrained <code>srcset</code></h2>
  235. <p>Say you’ve got a nice big, fullscreen image. It’s a content image, not style, so you’re using an actual <code>&lt;img&gt;</code> instead of a <code>background-image</code>. You want to responsify it, because fullscreen on a phone is orders of magnitude different from fullscreen on desktop.</p>
  236. <p>The first question is, is your image acting like <code>contain</code> or <code>cover</code>?</p>
  237. <p>If you know how CSS’s <code>background-size</code> or <code>object-fit</code> properties work, that’s what I’m asking. Is your image going to get as big as it can without cropping itself, or is it covering up an element entirely and not leave any empty space? If you’re confused, <a href="http://davidwalsh.name/demo/background-size.html">try playing with David Walsh’s demo</a>.</p>
  238. <h3 id="object-fit-cover">
  239. <code>object-fit: cover</code>
  240. </h3>
  241. <p>This one is easy: just set <code>sizes</code> to <code>100vw</code>.</p>
  242. <pre>
  243. <code class="html">&lt;img srcset="..." sizes="100vw" alt="A giraffe."&gt;
  244. </code>
  245. </pre>
  246. <h3 id="object-fit-contain">
  247. <code>object-fit: contain</code>
  248. </h3>
  249. <p>This one is hard.</p>
  250. <p>The problem is, <code>srcset</code> currently only does widths. <a href="https://github.com/ResponsiveImagesCG/picture-element/issues/86">There is an <code>h</code> descriptor coming</a>, but it doesn’t do us any good now. Are we doomed to make our images too tall, or to use <code>&lt;picture&gt;</code> and micromanage the browser logic?</p>
  251. <p>I shouted my despair into the ether, that is, <a href="http://stackoverflow.com/questions/29778235/can-i-use-picture-for-both-height-and-width-constrained-images">asked StackOverflow for ideas</a>. The inestimable <a href="https://github.com/aFarkas">Alexander Farkas</a>, maintainer of <a href="https://github.com/aFarkas/html5shiv">the HTML5shiv</a> since 2011, and the author of/contributor to loads of awesome <a href="https://github.com/scottjehl/picturefill">responsive</a> <a href="https://github.com/aFarkas/respimage">image</a> <a href="https://github.com/aFarkas/lazysizes">solutions</a>, swooped in and <a href="http://stackoverflow.com/a/29781000">nailed it in one</a>:</p>
  252. <blockquote>
  253. <p>I think I got it (1/2) is equal to (width/height):</p>
  254. <pre><code>&lt;img
  255. srcset="http://lorempixel.com/960/1920/sports/10/w1920/ 960w"
  256. sizes="(min-aspect-ratio: 1/2) calc(100vh * (1 / 2))"
  257. /&gt;</code></pre>
  258. </blockquote>
  259. <p>Pretty brilliant. I’ll explain this terse code example, because I sure as heck had to go over it several times myself.</p>
  260. <p>The lynchpin of the entire operation is the image’s <strong>aspect ratio.</strong> Without that, we can’t do a blamed thing.</p>
  261. <p>Thankfully, it’s straightforward: divide the image’s width by its height. This gets you the “aspect multiplier” (probably not a real term). The actual aspect <em>ratio</em> is that multiplier written as a simplified fraction. It’s commonly separated like 16:9, but in CSS it’s <code>16/9</code>.</p>
  262. <p>So if you had an image 300 pixels by 500 pixels, and another 600 pixels by 1000 pixels, they’d both have an aspect ratio of 3:5, or <code>3/5</code> in CSS.</p>
  263. <p>Let's pretend we have an image with aspect ratio 4:5. In English, our logic is:</p>
  264. <ol>
  265. <li>If the screen’s aspect ratio is wider (greater) than the image’s aspect ratio, the image will be the full height of the screen.</li>
  266. <li>Otherwise, the image will be the full width of the screen.</li>
  267. </ol>
  268. <p>Using our <code>4/5</code> aspect ratio image from earlier, that translates to:</p>
  269. <pre>
  270. <code class="html">&lt;img sizes="(min-aspect-ratio: 4/5) 80vh, 100vw"&gt;
  271. </code>
  272. </pre>
  273. <p>Whoah! Where did that <code>80vh</code> come from?</p>
  274. <p>This is the part I had trouble with. The first thing to understand is that we’re still telling the browser how <em>wide</em> the image is going to be. <strong>We</strong> know it’s going to be <code>100vh</code> tall, but we need to inform the browser what width that entails. Since we have the aspect ratio, and we have the height, we just need to solve for the width.</p>
  275. <dl class="proof">
  276. <dt class="math">Ratio = width ÷ height</dt>
  277. <dd>An image’s aspect ratio is its width divided by its height</dd>
  278. <dt class="math">⅘ = width ÷ 100vh</dt>
  279. <dd>Fill in our known values</dd>
  280. <dt class="math">width = ⅘ × 100vh</dt>
  281. <dd>Isolate the <var>width</var> variable</dd>
  282. <dt class="math">width = 80vh</dt>
  283. <dd>Simplify.</dd>
  284. </dl>
  285. <p>
  286. <small>(I wish more than just Firefox &amp; Safari implemented MathML.)</small>
  287. </p>
  288. <p>The second <code>sizes</code> argument, the lone <code>100vw</code> length, is for when the viewport is tall enough that the image is width-constrained, which means it will take up the entire screen width. It’s actually optional, since browsers will assume <code>100vw</code> by default if no media conditions are true. So we can slim down to <code>sizes="(min-aspect-ratio: 4/5) 80vh"</code>.</p>
  289. <p>That’s the hard part over with. <code>srcset</code> is just a list of images and widths, after all. Since this image should fill the screen of everyone, no matter the size, we’ll run the gamut between itty-bitty 300 pixels and big honking 4000 pixels. (Though what with smartwatches and those new 5k screens, that may not be enough…)</p>
  290. <pre>
  291. <code class="html">&lt;img sizes="(min-aspect-ratio: 4/5) 80vh"
  292. src="giraffe.png"
  293. srcset="giraffe@0.75x.jpg 300w,
  294. giraffe.png 400w,
  295. giraffe@1.5x.jpg 600w,
  296. giraffe@2x.jpg 800w,
  297. giraffe@2.5x.jpg 1000w,
  298. giraffe@3x.jpg 1200w,
  299. giraffe@3.5x.jpg 1400w,
  300. giraffe@4x.jpg 1600w,
  301. [...pretend I wrote out everything in between]
  302. giraffe@9.5x.jpg 3800w,
  303. giraffe@10x.jpg 4000w"
  304. alt="A giraffe."&gt;
  305. </code>
  306. </pre>
  307. <p>Is that an excessive amount of <code>srcset</code> choices? Yeah, probably. There’s no silver bullet to figure out what widths to include, so you’ll have to experiment with what works for your site. It <strong>is</strong> a lot of markup, but the HTML weight gain is more than paid back with properly-sized images.</p>
  308. <h2 id="responsive-bitmaps-inside-inline-svg">Responsive bitmaps inside inline SVG</h2>
  309. <p>My personal use-case is shoving raster images inside <code>&lt;svg&gt;</code>. You are probably not doing that, but the remainder of this blog post is as good an example as it gets for “how ugly can responsive images really be?”</p>
  310. <p>Answer: <strong>THIS UGLY.</strong> Children and the squeamish are allowed to cover their faces during the upsetting parts.</p>
  311. <p>Anyway, say I embed the raster image like this:</p>
  312. <pre>
  313. <code class="html">&lt;image xlink:href="giraffe.png" x="5" y="13" width="200" height="400"&gt;
  314. &lt;title&gt;A giraffe.&lt;/title&gt;
  315. &lt;/image&gt;
  316. </code>
  317. </pre>
  318. <p>This has two problems:</p>
  319. <ol>
  320. <li>SVG doesn’t have anything like <code>srcset</code>. You get the one <code>xlink:href</code>, <strong>and you’ll like it.</strong></li>
  321. <li>Browser preloaders don’t fetch it like they do with your usual <code>&lt;img src&gt;</code>. This is not a trivial slowdown; other assets (scripts, background images, etc.) can get scheduled before it, blocking the image.</li>
  322. </ol>
  323. <p>We can solve both issues in one fell swoop with <code>&lt;foreignObject&gt;</code>:</p>
  324. <pre>
  325. <code class="html">&lt;foreignObject x="5" y="13" width="200" height="400"&gt;
  326. &lt;img src="giraffe.png" srcset="image@2x.png 2x, etc..." alt="A giraffe."&gt;
  327. &lt;/foreignObject&gt;
  328. </code>
  329. </pre>
  330. <p>It can get as fancy as we want in there; <code>&lt;foreignObject&gt;</code> accepts any HTML that can go inside <code>&lt;body&gt;</code>. <strong><em>Except in Internet Explorer.</em></strong> (Edge, thankfully, does support <code>&lt;foreignObject&gt;</code>.)</p>
  331. <h3 id="fixing-ie">Fixing IE</h3>
  332. <p>IE9, 10, &amp; 11 all don’t support <code>&lt;foreignObject&gt;</code>, but hope is not lost: just use <code>&lt;switch&gt;</code> to give them <code>&lt;image</code> instead:</p>
  333. <pre>
  334. <code class="html">&lt;switch&gt;
  335. &lt;foreignObject x="5" y="13" width="200" height="400"
  336. requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"&gt;
  337. &lt;img src="giraffe.png" srcset="image@2x.png 400w, etc..." alt="a giraffe"&gt;
  338. &lt;/foreignObject&gt;
  339. &lt;image xlink:href="giraffe.png" x="5" y="13" width="200" height="400"&gt;
  340. &lt;title&gt;a giraffe&lt;/title&gt;
  341. &lt;/image&gt;
  342. &lt;/switch&gt;
  343. </code>
  344. </pre>
  345. <p>That <code>requiredFeatures</code> attribute lets IE know that the <code>&lt;foreignObject&gt;</code> element requires the features described at that URL. IE doesn’t like, look it up or anything, the URLs of what a browser supports are hardcoded in.</p>
  346. <p>A neat side-effect is that IE’s preloader will grab what’s inside the <code>src</code> it won’t use, but it’s the same URL as the <code>xlink:href</code> it <em>will</em> use. Bonus!</p>
  347. <h3 id="building-the-sizes">Building the <code>sizes</code></h3>
  348. <p>I’ve got a remaining wrinkle to deal with. The images don’t take up all of their <code>&lt;svg&gt;</code> parent’s space, and they each have their own aspect ratios! </p>
  349. <p>The first step to making sense of the mess is finding out what percentage of the <code>&lt;svg&gt;</code>’s dimensions each of the images’ dimensions take up. For a setup like this:</p>
  350. <pre>
  351. <code class="html">&lt;svg viewBox="0 0 1400 1600" preserveAspectRatio="xMidYMid"&gt;
  352. &lt;image x="200" y="200" width="1000" height="1200"/&gt;
  353. &lt;/svg&gt;
  354. </code>
  355. </pre>
  356. <p>(On the <code>&lt;svg&gt;</code> element, I’ve placed <code>preserveAspectRatio="xMidYMid"</code>, which has the viewBox act like <code>object-fit: contain</code> and center itself. )</p>
  357. <p>The image is roughly 71.43% the width of the <code>&lt;svg&gt;</code> (1000 ÷ 1400), and 75% the height (1200 ÷ 1600). We could write a basic program to do that math for us. But assembling that information into a usable <code>sizes</code> eluded me. Here were my conditions:</p>
  358. <ol class="proof">
  359. <li>If the screen’s aspect ratio is wider (greater) than the <code>&lt;svg&gt;</code>’s aspect ratio (7:8), the <code>&lt;svg&gt;</code> is height-constrained. Otherwise, it’s width-constrained.</li>
  360. <li><p>When the <code>&lt;svg&gt;</code> is width-constrained:</p>
  361. <dl>
  362. <dt class="math">Image’s display width = 100vw × 71.43%</dt>
  363. <dt class="math">Image’s display width = 71.43vw</dt>
  364. <dd>The <code>&lt;svg&gt;</code> will be 100vw wide, and the image will be 71.43% the width of that.</dd>
  365. </dl>
  366. </li><li><p>When the <code>&lt;svg&gt;</code> is height-constrained:</p>
  367. <dl>
  368. <dt class="math">Image’s display height = 100vh × 75%</dt>
  369. <dt class="math">Image’s display height = 75vh</dt>
  370. <dd>The <code>&lt;svg&gt;</code> will be 100vh tall, and the image will be 75% the height of that. However, we need its <em>width</em> for <code>sizes</code>.</dd>
  371. <dt class="math">Ratio = width ÷ height</dt>
  372. <dd>We’ll use the definition of aspect ratio again…</dd>
  373. <dt class="math">⅚ = width ÷ 75vh</dt>
  374. <dd>Substitute the values we know…</dd>
  375. <dt class="math">62.5vh = width</dt>
  376. <dd>And solve.</dd>
  377. </dl>
  378. </li>
  379. </ol>
  380. <p>Phew. After crunching the numbers, our <code>sizes</code> will look like this:</p>
  381. <pre>
  382. <code>sizes="(min-aspect-ratio: 7/8) 71.43vw, 62.5vh"
  383. </code>
  384. </pre>
  385. <p>Which, despite the contortions I took to make it, isn’t too scary-looking. Putting everything together, we end up with:</p>
  386. <pre>
  387. <code class="html">&lt;svg viewBox="0 0 1400 1600" preserveAspectRatio="xMidYMid"&gt;
  388. &lt;switch&gt;
  389. &lt;foreignObject x="200" y="200" width="1000" height="1000" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"&gt;
  390. &lt;img src="giraffe.png" srcset="image@2x.png 400w, etc..." alt="A giraffe." sizes="(min-aspect-ratio: 7/8) 71.43vw, 62.5vh"&gt;
  391. &lt;/foreignObject&gt;
  392. &lt;image xlink:href="giraffe.png" x="200" y="200" width="1000" height="1200"&gt;
  393. &lt;title&gt;A giraffe.&lt;/title&gt;
  394. &lt;/image&gt;
  395. &lt;/switch&gt;
  396. &lt;/svg&gt;
  397. </code>
  398. </pre>
  399. <p>Complicated, yes. But so is the Web.</p>
  400. <h3 id="making-things-worst-with-not-quite-entire-viewport-scaling">Making things worst with not-quite-entire-viewport scaling</h3>
  401. <p>As a final problem, I’m going to have interface chrome for navigating these SVGs, so I won’t have the full viewport available. I’ll need a fancier <code>sizes</code>.</p>
  402. <p>First, I need to know how much space the chrome will use. It’s set up to behave like this:</p>
  403. <ol>
  404. <li>If the screen is wider than it is tall (<code>orientation: landscape</code>), there’s chrome on all four sides. The horizontal chrome takes up 200 pixels, and the vertical chrome takes up 100 pixels.</li>
  405. <li>Otherwise, the chrome only takes up vertical space. It will be 200 pixels tall.</li>
  406. </ol>
  407. <p>So now I’ve got <strong>four</strong> boxes to analyze:</p>
  408. <ol>
  409. <li>The browser viewport</li>
  410. <li>The available space within that viewport once the chrome is done, taken up by the <code>&lt;svg&gt;</code> element</li>
  411. <li>The SVG viewport (“viewBox”)</li>
  412. <li>The <code>&lt;image&gt;</code> as constrained by the viewBox.</li>
  413. </ol>
  414. <p>(If any parts of this process have horrible browser bugs, <em>I’m going to be the first to find out.</em>)</p>
  415. <h4 id="the-browser-viewport">The browser viewport</h4>
  416. <p>Simple enough:</p>
  417. <ul>
  418. <li>width = 100vw</li>
  419. <li>height = 100vh</li>
  420. <li>aspect-ratio = 100vw/100vh</li>
  421. </ul>
  422. <h4 id="the-ltsvg-element">The <code>&lt;svg&gt;</code> element</h4>
  423. <ol class="proof">
  424. <li>
  425. <p>If the viewport is wider than it is tall (<code>orientation: landscape</code>):</p>
  426. <dl>
  427. <dt class="math">width = <code>calc(100vw - 200px)</code></dt>
  428. <dd>The chrome will take up 200px of width, so the width available is the full width (100vw) minus that (200px).</dd>
  429. <dt class="math">height = <code>calc(100vh - 100px)</code></dt>
  430. <dd>The chrome will take up 100px of height, and we calculate what’s left like we did with the width.</dd>
  431. <dt class="math">aspect ratio = <code>calc((100vw - 200px) / (100vh - 100px))</code></dt>
  432. <dd>Substitute the values into the equation for calculating aspect ratios.</dd>
  433. </dl>
  434. <p><small>(I’m using <code>calc()</code> because the units don’t match, and only the browser will know how to calculate them together.)</small></p>
  435. </li>
  436. <li>
  437. <p>Otherwise:</p>
  438. <dl>
  439. <dt class="math">width = 100vw</dt>
  440. <dd>There’s no horizontal chrome at all when the browser is <code>orientation: portrait</code>, so it’s just 100vw.</dd>
  441. <dt class="math">height = <code>calc(100vh - 200px)</code></dt>
  442. <dd>There is 200px of vertical chrome, though.</dd>
  443. <dt class="math">aspect ratio = <code>calc(100vw / (100vh - 200px))</code></dt>
  444. <dd>And substitute again.</dd>
  445. </dl>
  446. </li>
  447. </ol>
  448. <h4 id="the-viewbox">The viewBox</h4>
  449. <p>We’ll use the <code>&lt;svg&gt;</code> from last time:</p>
  450. <pre>
  451. <code class="html">&lt;svg viewBox="0 0 1400 1600" preserveAspectRatio="xMidYMid"&gt;
  452. ...
  453. &lt;/svg&gt;
  454. </code>
  455. </pre>
  456. <p>Unlike the browser viewport and the <code>&lt;svg&gt;</code> element, the aspect ratio of the viewBox doesn’t change when the orientation does. It’s also defined right inside the <code>viewBox</code> attribute. We could technically use <code>1400/1600</code>, but I’ll simplify it to <code>7/8</code>.</p>
  457. <p>As for the height and width, we have two media conditions that we need to check:</p>
  458. <ol>
  459. <li>Is the browser viewport wide or tall?`</li>
  460. <li>Does the available space have a wider or narrower aspect ratio than the viewBox?</li>
  461. </ol>
  462. <p>The first one is still <code>(orientation: landscape)</code>, but the chrome has complicated the second. Ideally, I could do <em>this</em>:</p>
  463. <pre>
  464. <code>(min-aspect-ratio: calc((100vw - 200px) / (100vh - 100px)))
  465. </code>
  466. </pre>
  467. <p>…but CSS requires <code>X/Y</code> string values for its <code>aspect-ratio</code> queries. Using <code>calc()</code> is right out.</p>
  468. <p>Instead, we have to <strong>reverse-engineer the media query.</strong> Querying the aspect ratio, after all, is just syntactic sugar for crunching the viewport width and height. Like so:</p>
  469. <p class="math">viewport’s aspect-ratio = viewport width ÷ viewport height = 100vw ÷ 100vh</p>
  470. <p>Let’s think about what we want using actual math symbols, instead of CSS’s hecked-up <code>min-</code>/<code>max-</code> system:</p>
  471. <dl class="proof">
  472. <dt class="math">aspect-ratio ≥ 7/8</dt>
  473. <dd>We want to know if the viewport’s aspect ratio is greater than or equal to 7/8 (the viewBox’s aspect ratio).</dd>
  474. <dt class="math">100vw ÷ 100vh ≥ 7/8</dt>
  475. <dd>Since the viewport’s aspect ratio equals 100vw/100vh, we substitute that.</dd>
  476. <dt class="math">100vw ≥ 7/8 × 100vh</dt>
  477. <dd>Multiply both sides by 100vh.</dd>
  478. <dt class="math">width ≥ 7/8 × 100vh</dt>
  479. <dd>Since 100vw is the width of the viewport, substitute that back in.</dd>
  480. <dt><code>min-width: calc(7/8 * 100vh)</code></dt>
  481. <dd>Convert into CSS syntax.</dd>
  482. </dl>
  483. <p>Now, if that’s all we wanted, I could precalculate the work for everyone’s browsers and use <code>(min-width: 87.5vh)</code>. But I haven’t introduced the chrome’s space requirements yet.</p>
  484. <ol>
  485. <li>When <code>(orientation: landscape)</code>, the available width = 100vw - 200px and the available height = 100vh - 100px</li>
  486. <li>Otherwise, the available width = 100vw and the available height = 100vh - 200px</li>
  487. </ol>
  488. <p>So with those complicating factors, what aspect ratio do we really want?</p>
  489. <ol class="proof">
  490. <li>
  491. <p>If <code>(orientation: landscape)</code>:</p>
  492. <dl>
  493. <dt class="math">aspect-ratio of available space = (100vw - 200px) ÷ (100vh - 100px)</dt>
  494. <dd>We want to find out if this is larger than 7/8.</dd>
  495. <dt class="math">(100vw - 200px) ÷ (100vh - 100px) ≥ 7/8</dt>
  496. <dd>Like this. We need to isolate either the 100vw or the 100vh so we can turn it into the <var>width</var> or <var>height</var> viewport dimension.</dd>
  497. <dt class="math">100vw - 200px ≥ 7/8 × (100vh - 100px)</dt>
  498. <dd>Multiply both sides by (100vh - 100px).</dd>
  499. <dt class="math">100vw ≥ (7/8 × (100vh - 100px)) + 200px</dt>
  500. <dd>Add 200px to both sides.</dd>
  501. <dt class="math">width ≥ (7/8 × (100vh - 100px)) + 200px</dt>
  502. <dd>Subsitute <var>width</var> for 100vw.</dd>
  503. <dt><code>min-width: calc((7/8 * (100vh - 100px)) + 200px)</code></dt>
  504. <dd>Convert to CSS syntax. (Now <em>that</em> is an ugly media condition.)</dd>
  505. </dl>
  506. </li>
  507. <li>
  508. <p>Otherwise:</p>
  509. <dl>
  510. <dt class="math">aspect-ratio of available space = 100vw/(100vh - 200px)</dt>
  511. <dd>Second verse, same as the first. This time the chrome isn’t quite as complicated.</dd>
  512. <dt class="math">100vw ≥ 7/8 × (100vh - 200px)</dt>
  513. <dd>Multiply both sides by (100vh - 200px).</dd>
  514. <dt class="math">width ≥ 7/8 × (100vh - 200px)</dt>
  515. <dd>Substitute <var>width</var> for 100vw.</dd>
  516. <dt><code>min-width: calc(7/8 * (100vh - 200px))</code></dt>
  517. <dd>Convert to CSS syntax.</dd>
  518. </dl>
  519. </li>
  520. </ol>
  521. <p>Okay, cool. Now we have the second half of our media conditions. So we need the dimensions of the viewBox for each of these:</p>
  522. <ol>
  523. <li><code>"(orientation: landscape) and (min-width: calc((7/8 * (100vh - 100px)) + 200px))"</code></li>
  524. <li><code>"(orientation: landscape)"</code></li>
  525. <li><code>"(min-width: calc(7/8 * (100vh - 200px)))"</code></li>
  526. <li><code>""</code></li>
  527. </ol>
  528. <p>Alright, let’s take it from the top.</p>
  529. <ol class="proof">
  530. <li>
  531. <p>If <code>"(orientation: landscape) and (min-width: calc((7/8 * (100vh - 100px)) + 200px))"</code>, the viewBox is height-constrained. That means we’ll need the height to figure out the width.</p>
  532. <dl>
  533. <dt class="math">viewBox’s height = available height = <code>calc(100vh - 100px)</code></dt>
  534. <dd>The viewBox will take up the entire height the <code>&lt;svg&gt;</code> element does.</dd>
  535. <dt class="math">aspect-ratio = height ÷ width</dt>
  536. <dt class="math">7/8 = <code>calc(100vh - 100px)</code> ÷ width</dt>
  537. <dd>Since we set the aspect ratio of the viewBox to be 7/8, we can substitute that and the width value we just figured into the aspect ratio equation.</dd>
  538. <dt class="math">7/8 × width = <code>calc(100vh - 100px)</code></dt>
  539. <dd>Multiply both sides by <var>width</var>.</dd>
  540. <dt class="math">width = <code>calc(100vh - 100px)</code> × 8/7</dt>
  541. <dd>Divide both sides by 7/8.</dd>
  542. <dt>width = <code>calc((100vh - 100px) * 8/7)</code></dt>
  543. <dd>Convert to CSS syntax.</dd>
  544. </dl>
  545. </li>
  546. <li>
  547. <p>If not, and if <code>"(orientation: landscape)"</code>, the viewBox is width-constrained.</p>
  548. <p>That means the the viewBox will take up all of the width of the <code>&lt;svg&gt;</code> element. Therefore, the width of the viewBox is also <code>calc(100vw - 200px)</code>.</p>
  549. </li>
  550. <li>
  551. <p>If not, and if <code>"(min-width: calc(7/8 * (100vh - 200px)))"</code>, the viewBox is height-constrained.</p>
  552. <dl>
  553. <dt class="math">height = <code>calc(100vh - 200px)</code></dt>
  554. <dd>The height that the <code>&lt;svg&gt;</code> element is taking up.</dd>
  555. <dt class="math">aspect-ratio = height ÷ width</dt>
  556. <dt class="math">7/8 = <code>calc(100vh - 200px)</code> ÷ width</dt>
  557. <dd>Substitute like before.</dd>
  558. <dt class="math">width = <code>calc(100vh - 200px)</code> × 8/7</dt>
  559. <dd>I isolated the width variable in one step this time. if you forget how to do it, look back at Step 1.</dd>
  560. <dt>width = <code>calc((100vh - 200px) * 8/7)</code></dt>
  561. <dd>Convert to CSS syntax.</dd>
  562. </dl>
  563. </li>
  564. <li>
  565. <p>Otherwise, it’s width-constrained. This time, there’s no horizontal chrome at all, so the width is just 100vw.</p>
  566. </li>
  567. </ol>
  568. <p>We’re getting there!</p>
  569. <h4 id="the-image">The image</h4>
  570. <p>Like the viewBox, we’ll use the <code>&lt;image&gt;</code> from last time:</p>
  571. <pre>
  572. <code class="html">&lt;svg viewBox="0 0 1400 1600" preserveAspectRatio="xMidYMid"&gt;
  573. &lt;image x="200" y="200" width="1000" height="1200"/&gt;
  574. &lt;/svg&gt;
  575. </code>
  576. </pre>
  577. <p>We’re in the home stretch. Once we get the width values for this final box, we can assemble the <code>sizes</code>.</p>
  578. <p>Unlike before, I’m not going to need the step-by-step equation solving. (If you were feeling patronized, don’t be… those were for <strong>me</strong> to understand what the hell I was doing.) We can take the width values of the viewBox and then multiply those by the percentage of the viewBox the image takes up. Which is…</p>
  579. <p class="math">image-width ÷ viewBox-width = 1000/1400 = 71.42%</p>
  580. <p>Of course, if you remember from way back at the top of this post, we can’t use percentages, so we’ll go with 0.7142 instead. Staple those widths onto the back of the media conditions and voilà:</p>
  581. <ol>
  582. <li><code>(orientation: landscape) and (min-width: calc((7/8 * (100vh - 100px)) + 200px)) calc((100vh - 100px) * 8/7 * 0.7142)</code></li>
  583. <li><code>(orientation: landscape) calc((100vw - 200px) * 0.7142)</code></li>
  584. <li><code>(min-width: calc(7/8 * (100vh - 200px))) calc((100vh - 200px) * 8/7 * 0.7142)</code></li>
  585. <li><code>71.42vw</code></li>
  586. </ol>
  587. <p>What have I done.</p>
  588. <h4 id="assembling">Assembling</h4>
  589. <p>Putting it all together for the final markup:</p>
  590. <pre>
  591. <code class="html">&lt;svg viewBox="0 0 1400 1600" preserveAspectRatio="xMidYMid"&gt;
  592. &lt;switch&gt;
  593. &lt;foreignObject x="200" y="200" width="1000" height="1200" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"&gt;
  594. &lt;picture&gt;
  595. &lt;source srcset="giraffe@0.75x.jpg 300w,
  596. giraffe.png 400w,
  597. giraffe@1.5x.jpg 600w,
  598. giraffe@2x.jpg 800w,
  599. giraffe@2.5x.jpg 1000w,
  600. giraffe@3x.jpg 1200w,
  601. giraffe@3.5x.jpg 1400w,
  602. giraffe@4x.jpg 1600w,
  603. giraffe@4.5x.jpg 1800w,
  604. giraffe@5x.jpg 2000w,
  605. giraffe@5.5x.jpg 2200w,
  606. giraffe@6x.jpg 2400w,
  607. giraffe@6.5x.jpg 2600w,
  608. giraffe@7x.jpg 2800w,
  609. giraffe@7.5x.jpg 3000w,
  610. giraffe@8x.jpg 3200w,
  611. giraffe@8.5x.jpg 3400w,
  612. giraffe@9x.jpg 3600w,
  613. giraffe@9.5x.jpg 3800w,
  614. giraffe@10x.jpg 4000w"
  615. type="image/jpeg"
  616. sizes="(orientation: landscape) and
  617. (min-width: calc((7/8 * (100vh - 100px)) + 200px))
  618. calc((100vh - 100px) * 8/7 * 0.7142),
  619. (orientation: landscape)
  620. calc((100vw - 200px) * 0.7142),
  621. (min-width: calc(7/8 * (100vh - 200px)))
  622. calc((100vh - 200px) * 8/7 * 0.7142),
  623. 71.42vw"&gt;
  624. &lt;img src="giraffe.jpg" alt="A giraffe."
  625. srcset="giraffe@1.5x.jpg 1.5x,
  626. giraffe@2x.jpg 2x,
  627. giraffe@2.5x.jpg 2.5x,
  628. giraffe@3x.jpg 3x,
  629. giraffe@3.5x.jpg 3.5x,
  630. giraffe@4x.jpg 4x,
  631. giraffe@4.5x.jpg 4.5x,
  632. giraffe@5x.jpg 5x"
  633. height="400" width="300"&gt;
  634. &lt;/picture&gt;
  635. &lt;/foreignObject&gt;
  636. &lt;image xlink:href="giraffe.png" x="200" y="200" width="1000" height="1200"&gt;
  637. &lt;title&gt;A giraffe.&lt;/title&gt;
  638. &lt;/image&gt;
  639. &lt;/switch&gt;
  640. &lt;/svg&gt;
  641. </code>
  642. </pre>
  643. <p>If it weren’t in the requirements to support photographic images, I could replace <em>all of that</em> with a single <code>&lt;image xlink:href="giraffe.svg"&gt;</code>. But I guess I built character this way.</p>
  644. <h2 id="thats-all-folks">That’s all, folks</h2>
  645. <p>I hope that you don’t have to do what I’m doing, partially because of everything you just saw, but mostly because it won’t make you any money. (<em>Trust me.</em>). But if you run into tricky responsive image situations, hopefully something from this post will guide you a little.</p>
  646. <p>Thanks for sticking with me!</p>
  647. <p>
  648. <small>(Code for the Web, they said. It’s fun, they said.)</small>
  649. </p>