{"componentChunkName":"component---src-templates-post-jsx","path":"/practical-use-cases-for-the-picture-element/","result":{"data":{"contentfulPost":{"title":"Practical use cases for the picture element","slug":"practical-use-cases-for-the-picture-element","metaDescription":null,"publishDate":"February 01, 2016","publishDateISO":"2016-02-01","tags":[{"title":"Performance","id":"d7a7c8c4-e306-53c5-a10d-348f33697e1d","slug":"performance"},{"title":"Development","id":"0394a67a-444d-51e8-82b0-704a5902de62","slug":"development"}],"heroImage":{"title":"css-rule","fluid":{"aspectRatio":0.9847715736040609,"src":"/archive/v3/ctf/251b5b526cce50c7.jpg","srcSet":"/archive/v3/ctf/2f677f399f0407a1.jpg 388w","srcWebp":"/archive/v3/ctf/683e90762363d1a7.webp","srcSetWebp":"/archive/v3/ctf/fc305290d3d1e42d.webp 388w","sizes":"(max-width: 1800px) 100vw, 1800px"},"ogimg":{"src":"/archive/v3/ctf/251b5b526cce50c7.jpg","width":1800,"height":1828}},"body":{"childMarkdownRemark":{"timeToRead":3,"html":"<p>The brave new world of responsive images has already been explored and explained in great detail all over the web (see <a href=\"#resource-list\">resource list below</a>). Most of these resources advise against using the <code class=\"language-text\">&lt;picture/&gt;</code> element for standard images since it’s far more declarative and doesn’t allow the browser to automagically choose an image based on the user’s network and screen resolution. (hint: srcset does this, so use this method when including images you don’t need declarative control over.) If a majority of the web agrees that the <code class=\"language-text\">&lt;img/&gt;</code> tag accompanied with the new srcset and sizes attributes is the most pragmatic solution where does that leave the poor ‘ol <code class=\"language-text\">&lt;picture/&gt;</code> element? 😥</p>\n<p>The stand alone <code class=\"language-text\">&lt;img/&gt;</code> and <code class=\"language-text\">srcset</code> method trumps in all cases except art direction and type-switching fallback support. This post simply showcases these two use cases for quick reference and experimentation. For more information on the <code class=\"language-text\">&lt;picture/&gt;</code> element and the responsive images spec, refer to the list of resources below.</p>\n<h2 id=\"art-direction\" style=\"position:relative;\"><a href=\"#art-direction\" aria-label=\"art direction permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Art direction</h2>\n<p>The most pragmatic use case for the <code class=\"language-text\">&lt;picture/&gt;</code> element is art direction. The <code class=\"language-text\">srcset</code> and <code class=\"language-text\">media</code> attributes give you full control of serving different images to various viewport sizes. This comes in handy when you need to serve cropped, or different sized images to multiple viewports for art directed media or for readability purposes.</p>\n<p><a class=\"code-example\" href=\"https://codepen.io/coreybruyere/pen/zqgKxp\">Code Example</a></p>\n<div class=\"gatsby-highlight\" data-language=\"html\"><pre class=\"language-html\"><code class=\"language-html\">  <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>picture</span><span class=\"token punctuation\">></span></span>\n    <span class=\"token comment\">&lt;!---[if IE 9]>&lt;video style=\"display: none;\">&lt;![endif]--></span>\n    <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>source</span> <span class=\"token attr-name\">srcset</span><span class=\"token attr-value\"><span class=\"token punctuation\">=</span><span class=\"token punctuation\">\"</span>/image-full.jpg<span class=\"token punctuation\">\"</span></span> <span class=\"token attr-name\">media</span><span class=\"token attr-value\"><span class=\"token punctuation\">=</span><span class=\"token punctuation\">\"</span>(min-width: 1024px)<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span>\n    <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>source</span> <span class=\"token attr-name\">srcset</span><span class=\"token attr-value\"><span class=\"token punctuation\">=</span><span class=\"token punctuation\">\"</span>/image-close.jpg<span class=\"token punctuation\">\"</span></span> <span class=\"token attr-name\">media</span><span class=\"token attr-value\"><span class=\"token punctuation\">=</span><span class=\"token punctuation\">\"</span>(min-width: 768px)<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span>\n    <span class=\"token comment\">&lt;!---[if IE 9]>&lt;/video>&lt;![endif]--></span>\n    <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>img</span> <span class=\"token attr-name\">src</span><span class=\"token attr-value\"><span class=\"token punctuation\">=</span><span class=\"token punctuation\">\"</span>/image-closer.jpg<span class=\"token punctuation\">\"</span></span> <span class=\"token attr-name\">alt</span><span class=\"token attr-value\"><span class=\"token punctuation\">=</span><span class=\"token punctuation\">\"</span>Image Description<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">/></span></span>\n  <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>picture</span><span class=\"token punctuation\">></span></span></code></pre></div>\n<p><em>Resize your browser window and notice how cropped and resized versions are served as you resize to a smaller viewport. Notice how declarative the picture method is? Giving it a new media source at different media queries makes for a practical solution for art direction.</em></p>\n<figure>\n  <picture>\n    <source srcset=\"/archive/v3/ctf/a2eb64b5ff83da81.jpg\" media=\"(min-width: 1024px)\"/>\n    <source srcset=\"/archive/v3/ctf/051690dc322dd0b3.jpg\" media=\"(min-width: 768px)\"/>\n    <img src=\"/archive/v3/ctf/f46203a095867034.jpg\" alt=\"Twin Peaks Red Room\"/>\n  </picture>\n  <figcaption>\n    <blockquote>\n      The picture element is used when you need explicit control\n      over which source is shown at set viewport sizes.\n    </blockquote>\n    <cite>– Picturefill docs</cite>\n  </figcaption>\n</figure>\n<h2 id=\"type-switching\" style=\"position:relative;\"><a href=\"#type-switching\" aria-label=\"type switching permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Type switching</h2>\n<p>The <code class=\"language-text\">&lt;picture/&gt;</code> spec provides a <code class=\"language-text\">type</code> attribute that you can use on any nested <code class=\"language-text\">&lt;source/&gt;</code> element. This provides you with a viable fallback solution for older browser that don’t support new images formats, e.g., <code class=\"language-text\">.webp</code>, <code class=\"language-text\">.svg</code>.</p>\n<p><a class=\"code-example\" href=\"http://codepen.io/coreybruyere/pen/PNMGPY\">Code Example</a></p>\n<div class=\"gatsby-highlight\" data-language=\"html\"><pre class=\"language-html\"><code class=\"language-html\">  <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>picture</span><span class=\"token punctuation\">></span></span>\n    <span class=\"token comment\">&lt;!--[if IE 9]>&lt;video style=\"display: none;\">&lt;![endif]--></span>\n    <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>source</span> <span class=\"token attr-name\">srcset</span><span class=\"token attr-value\"><span class=\"token punctuation\">=</span><span class=\"token punctuation\">\"</span>/pattern-example.svg<span class=\"token punctuation\">\"</span></span> <span class=\"token attr-name\">type</span><span class=\"token attr-value\"><span class=\"token punctuation\">=</span><span class=\"token punctuation\">\"</span>image/svg xml<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span>\n    <span class=\"token comment\">&lt;!--[if IE 9]>&lt;/video>&lt;![endif]--></span>\n    <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>img</span> <span class=\"token attr-name\">src</span><span class=\"token attr-value\"><span class=\"token punctuation\">=</span><span class=\"token punctuation\">\"</span>/pattern-example.png<span class=\"token punctuation\">\"</span></span> <span class=\"token attr-name\">alt</span><span class=\"token attr-value\"><span class=\"token punctuation\">=</span><span class=\"token punctuation\">\"</span>Image Description<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">/></span></span>\n  <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>picture</span><span class=\"token punctuation\">></span></span></code></pre></div>\n<figure>\n  <picture>\n    <!-- [if IE 9]><video style=\"display: none;\"><![endif]-->\n    <source srcset=\" /archive/v3/ctf/17e2ba840ce3e871.svg\" type=\"image/svg+xml\" />\n    <!-- [if IE 9]></video><![endif]-->\n    <img src=\"/archive/v3/ctf/9c1a3b0977cdf06e.png\" alt=\"Image Description\"/></picture>\n  <figcaption>\n    <blockquote>The types attribute is used to send an alternate image source format only to browsers that support that format, and a fallback source to browsers that do not. Unlike existing solutions, the picture element makes these negotiations using a single request.\n    </blockquote>\n    <cite>– Picturefill Docs</cite>\n  </figcaption>\n</figure>\n<h2 id=\"resource-list\" style=\"position:relative;\"><a href=\"#resource-list\" aria-label=\"resource list permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Resource list</h2>\n<ul>\n \t<li><a href=\"http://codepen.io/Tigt/post/when-responsive-images-get-ugly#media-conditions-not-media-queries\">When Responsive Images Get Ugly</a></li>\n \t<li><a href=\"http://ericportis.com/posts/2014/srcset-sizes/\">Srcset and sizes</a></li>\n \t<li><a href=\"http://blog.cloudfour.com/dont-use-picture-most-of-the-time/\">Don’t Use Picture Most of the Time</a></li>\n \t<li><a href=\"http://sarasoueidan.com/blog/svg-picture/\">Better SVG Fallback and Art Direction With The picture Element</a></li>\n \t<li><a href=\"https://css-tricks.com/responsive-images-youre-just-changing-resolutions-use-srcset/\">Responsive Images: If you’re just changing resolutions, use srcset</a></li>\n \t<li><a href=\"http://www.smashingmagazine.com/2014/05/responsive-images-done-right-guide-picture-srcset/\">Responsive Images Done Right: A Guide To <picture> And srcset</picture></a></li>\n \t<li><a href=\"https://usecases.responsiveimages.org/\">Use Cases and Requirements for Standardizing Responsive Images</a></li>\n \t<li><a href=\"https://html.spec.whatwg.org/multipage/embedded-content.html\">Spec</a></li>\n</ul>","excerpt":"The brave new world of responsive images has already been explored and explained in great detail all over the web (see resource list below). Most of these resources advise against using the  element for standard images since it’s far more declarative and doesn’t allow the browser to automagically choose an image based…"}}}},"pageContext":{"slug":"practical-use-cases-for-the-picture-element","prev":{"slug":"css-audit-with-parker","publishDate":"2016-06-06"},"next":null}},"staticQueryHashes":[]}