<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Responsive Design on Brendan Quinn</title>
    <link>http://brendan-quinn.xyz/categories/responsive-design/</link>
    <description>Recent content in Responsive Design on Brendan Quinn</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    <lastBuildDate>Tue, 09 Feb 2016 16:30:47 -0500</lastBuildDate>
    <atom:link href="http://brendan-quinn.xyz/categories/responsive-design/index.xml" rel="self" type="application/rss+xml" />
    
    <item>
      <title>Using HTML5 and Media Queries for Responsive Images</title>
      <link>http://brendan-quinn.xyz/post/using-html5-and-media-queries-for-responsive-images</link>
      <pubDate>Tue, 09 Feb 2016 16:30:47 -0500</pubDate>
      
      <guid>http://brendan-quinn.xyz/post/using-html5-and-media-queries-for-responsive-images</guid>
      <description>

&lt;p&gt;Using &lt;code&gt;&amp;lt;picture&amp;gt;&lt;/code&gt; &lt;code&gt;&amp;lt;source&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;img&amp;gt;&lt;/code&gt; HTML5 elements combined with the &lt;code&gt;media&lt;/code&gt;, &lt;code&gt;srcset&lt;/code&gt;, and &lt;code&gt;size&lt;/code&gt; attributes, it is possible and even necessary to make responsive images for your users.  Doing so will minimize the size of your images, make it your site load faster, and therefore create a better UX. There are two approaches.  To decide, ask:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Q: Do I want to take into account device orientation and/or screen resolution?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3 id=&#34;answer-1-no:7ea357097035431ef70c20e57337b2ed&#34;&gt;Answer 1: &amp;lsquo;No.&amp;rsquo;&lt;/h3&gt;

&lt;p&gt;Thats cool.  This approach is simpler, which makes sense. It is for a simpler use-case.  Heres an example of an HTML5 responsive &lt;code&gt;&amp;lt;img /&amp;gt;&lt;/code&gt; element.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-css&#34;&gt;/* CSS */
@media only screen and (max-width: 799px) {
  img {
    width: 50%;
  }
}

@media only screen and (min-width: 800px) and (max-width: 1419px) {
  img {
    width: 75%;
  }
}

@media only screen and (min-width: 1240px) {
  img {
    width: 100%;
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;pre&gt;&lt;code class=&#34;language-html&#34;&gt;&amp;lt;!--  HTML --&amp;gt;
&amp;lt;img sizes=&amp;quot;(min-width: 1240px) 100vw,
    (min-width: 800px) 75vw,
    (min-width: 0px) 50vw&amp;quot;
  srcset=&amp;quot;http://brendan-quinn.xyz/css/images/responsive-images-portrait-sm-2x.jpg 200w,
    http://brendan-quinn.xyz/css/images/responsive-images-portrait-sm-2x.jpg 400w,
    http://brendan-quinn.xyz/css/images/responsive-images-portrait-md.jpg 528w,
    http://brendan-quinn.xyz/css/images/responsive-images-portrait-lg.jpg 1000w,
    http://brendan-quinn.xyz/css/images/responsive-images-portrait-md-2x.jpg 1046w,
    http://brendan-quinn.xyz/css/images/responsive-images-portrait-lg-1.5x.jpg 1500w,
    http://brendan-quinn.xyz/css/images/responsive-images-portrait-lg-2x.jpg 2000w&amp;quot;
  src=&amp;quot;http://brendan-quinn.xyz/css/images/responsive-images-portrait-sm.jpg&amp;quot;
  alt=&amp;quot;Icelandic Landscape View&amp;quot; /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;Sizes&lt;/code&gt; takes a inequality using &lt;code&gt;min-width&lt;/code&gt; or &lt;code&gt;max-width&lt;/code&gt; and the size of the image at each viewport size. &lt;code&gt;(min-width: 800px) 75vw,&lt;/code&gt; coincide with the css width of the image: &lt;code&gt;@media only screen and (min-width: 800px) and (max-width: 1419px) { img {width: 75%;} }&lt;/code&gt;.  This helps the browser calculate the size of the image that it can then search through &lt;code&gt;srcset&lt;/code&gt; and find the appropriate image. If, for example, a user were browsing on a device which had a viewport size of 910px, the second media statement would be satisfied.  It would then calculate 910 * .75 = 675, and look for the appropriate image in &lt;code&gt;srcset&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Srcset&lt;/code&gt; contains a comma delimited list of identical images of different scale and their width.  for example, &lt;code&gt;&lt;a href=&#34;http://brendan-quinn.xyz/css/images/responsive-images-portrait-md-2x.jpg&#34;&gt;http://brendan-quinn.xyz/css/images/responsive-images-portrait-md-2x.jpg&lt;/a&gt; 1046w,&lt;/code&gt; tells us that image &lt;code&gt;responsive-images-portrait-md-2x.jpg&lt;/code&gt; is &lt;code&gt;1046px&lt;/code&gt; wide.  The best image for the use-case given of a viewport of width 910px and therefore and image width of 675px would then be &lt;code&gt;&lt;a href=&#34;http://brendan-quinn.xyz/css/images/responsive-images-portrait-lg.jpg&#34;&gt;http://brendan-quinn.xyz/css/images/responsive-images-portrait-lg.jpg&lt;/a&gt; 1000w,&lt;/code&gt; since it is the smallest image that will not distort to fit the space.&lt;/p&gt;

&lt;p&gt;Here it is &lt;a href=&#34;http://codepen.io/Mathdrquinn/pen/JGwEjL&#34;&gt;live&lt;/a&gt;:&lt;/p&gt;

&lt;p&gt;&lt;p data-height=&#34;500&#34; data-theme-id=&#34;0&#34; data-slug-hash=&#34;JGwEjL&#34; data-default-tab=&#34;result&#34; data-user=&#34;Mathdrquinn&#34; class=&#39;codepen&#39;&gt;See the Pen &lt;a href=&#39;http://codepen.io/Mathdrquinn/pen/JGwEjL/&#39;&gt;Responsive HTML5 Image Element&lt;/a&gt; by Brendan Quinn (&lt;a href=&#39;http://codepen.io/Mathdrquinn&#39;&gt;@Mathdrquinn&lt;/a&gt;) on &lt;a href=&#39;http://codepen.io&#39;&gt;CodePen&lt;/a&gt;.&lt;/p&gt;
&lt;script async src=&#34;//assets.codepen.io/assets/embed/ei.js&#34;&gt;&lt;/script&gt;&lt;/p&gt;

&lt;h3 id=&#34;answer-2-yes-resolution-and-orientation-are-important:7ea357097035431ef70c20e57337b2ed&#34;&gt;Answer 2: &amp;lsquo;Yes. Resolution and orientation are important.&amp;rsquo;&lt;/h3&gt;

&lt;p&gt;Double Cool.  This approach introduces two additional elements: &lt;code&gt;picture&lt;/code&gt; and &lt;code&gt;source&lt;/code&gt;.  Here&amp;rsquo;s the structure:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-html&#34;&gt;&amp;lt;picture&amp;gt;
  &amp;lt;source srcset=&amp;quot;&amp;lt;!-- url for a device pixel ratio of 1 --&amp;gt; 1x,
      &amp;lt;!-- url for a device pixel ratio of 1.5 --&amp;gt; 1.5x,
      &amp;lt;!-- url for a device pixel ratio of 2 --&amp;gt; 2x&amp;quot;
    media=&amp;quot;&amp;lt;!-- Media conditions here--&amp;gt;&amp;quot; /&amp;gt;
  &amp;lt;source srcset=&amp;quot;&amp;lt;!-- url for a device pixel ratio of 1 --&amp;gt; 1x,
      &amp;lt;!-- url for a device pixel ratio of 1.5 --&amp;gt; 1.5x,
      &amp;lt;!-- url for a device pixel ratio of 2 --&amp;gt; 2x&amp;quot;
    media=&amp;quot;-- Media conditions here--&amp;quot; /&amp;gt;
  &amp;lt;img src=&amp;quot;&amp;lt;!-- default url --&amp;gt;&amp;quot; alt=&amp;quot;&amp;quot; /&amp;gt;
&amp;lt;/picture&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Similar to both answer 1 and a switch statement, the browser will load the first source whose media condition evaluates to true with the correct url to match the pixel ratio of the device.&lt;/p&gt;

&lt;p&gt;Because media is separated out into its own attribute, we have available to us all of the options we would in CSS.  Here is a more flushed out example with media queries that consider orientation.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-html&#34;&gt;&amp;lt;picture&amp;gt;
  &amp;lt;source srcset=&amp;quot;http://brendan-quinn.xyz/css/images/responsive-images-landscape-lg.jpg 1x,
      http://brendan-quinn.xyz/css/images/responsive-images-landscape-lg-1.5x.jpg 1.5x,
      http://brendan-quinn.xyz/css/images/responsive-images-landscape-lg-2x.jpg 2x&amp;quot;
    media=&amp;quot;only screen and (min-width: 1240px)&amp;quot;&amp;gt;
  &amp;lt;source srcset=&amp;quot;http://brendan-quinn.xyz/css/images/responsive-images-landscape-md.jpg 1x,
      http://brendan-quinn.xyz/css/images/responsive-images-landscape-md-1.5x.jpg 1.5x,
      http://brendan-quinn.xyz/css/images/responsive-images-landscape-md-2x.jpg 2x&amp;quot;
    media=&amp;quot;only screen and (min-width: 800px)&amp;quot;&amp;gt;
  &amp;lt;source srcset=&amp;quot;http://brendan-quinn.xyz/css/images/responsive-images-landscape-sm.jpg 1x,
      http://brendan-quinn.xyz/css/images/responsive-images-landscape-sm-1.5x.jpg 1.5x,
      http://brendan-quinn.xyz/css/images/responsive-images-landscape-sm-2x.jpg 2x&amp;quot;
    media=&amp;quot;only screen and (min-width: 0px) and (orientation: landscape)&amp;quot;&amp;gt;
  &amp;lt;source srcset=&amp;quot;http://brendan-quinn.xyz/css/images/responsive-images-portrait-sm.jpg 1x,
      http://brendan-quinn.xyz/css/images/responsive-images-portrait-sm-1.5x.jpg 1.5x,
      http://brendan-quinn.xyz/css/images/responsive-images-portrait-sm-2x.jpg 2x&amp;quot;
    media=&amp;quot;all and (min-width: 0px) and (orientation: portrait)&amp;quot;&amp;gt;
  &amp;lt;img src=&amp;quot;http://brendan-quinn.xyz/css/images/responsive-images-landscape-sm-2x.jpg&amp;quot; alt=&amp;quot;Icelandic Landscape&amp;quot; /&amp;gt;
&amp;lt;/picture&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;a href=&#34;http://codepen.io/Mathdrquinn/pen/rxQPLx&#34;&gt;Live&lt;/a&gt;:&lt;/p&gt;

&lt;p&gt;&lt;p data-height=&#34;325&#34; data-theme-id=&#34;0&#34; data-slug-hash=&#34;rxQPLx&#34; data-default-tab=&#34;result&#34; data-user=&#34;Mathdrquinn&#34; class=&#39;codepen&#39;&gt;See the Pen &lt;a href=&#39;http://codepen.io/Mathdrquinn/pen/rxQPLx/&#39;&gt;Responsive HTML5 Images&lt;/a&gt; by Brendan Quinn (&lt;a href=&#39;http://codepen.io/Mathdrquinn&#39;&gt;@Mathdrquinn&lt;/a&gt;) on &lt;a href=&#39;http://codepen.io&#39;&gt;CodePen&lt;/a&gt;.&lt;/p&gt;
&lt;script async src=&#34;//assets.codepen.io/assets/embed/ei.js&#34;&gt;&lt;/script&gt;&lt;/p&gt;

&lt;p&gt;While I enjoy the flexibility of this method, it is verbose, and while orientation changing works well on my desktop, I&amp;rsquo;m having issue getting orientation to work on mobile.  I&amp;rsquo;m still looking for a solution and will add the details if I find an answer.&lt;/p&gt;

&lt;p&gt;As always, if you&amp;rsquo;re looking for much more detail on these elements and attributes, visit the &lt;a href=&#34;https://www.w3.org/html/wg/drafts/html/master/semantics.html#embedded-content&#34;&gt;W3C specifications&lt;/a&gt; and pour over them.  While this post just skims the topic of responsive images, it would have been a poor one had I not consulted it.&lt;/p&gt;

&lt;p&gt;All the Best,&lt;/p&gt;

&lt;p&gt;Brendan&lt;/p&gt;
</description>
    </item>
    
  </channel>
</rss>