{"id":214,"date":"2012-05-13T13:53:17","date_gmt":"2012-05-13T13:53:17","guid":{"rendered":"http:\/\/www.w3.org\/community\/respimg\/?p=214"},"modified":"2012-05-14T16:13:30","modified_gmt":"2012-05-14T16:13:30","slug":"an-alternative-proposition-to-and-srcset-with-wider-scope","status":"publish","type":"post","link":"https:\/\/www.w3.org\/community\/respimg\/2012\/05\/13\/an-alternative-proposition-to-and-srcset-with-wider-scope\/","title":{"rendered":"An alternative proposition to picture and srcset, with wider scope"},"content":{"rendered":"<h2>Managing responsive designs is hard, so let&#8217;s use our &lt;head&gt;<\/h2>\n<p>Here is what I believe is the best proposition yet for managing our responsive designs. This includes managing our CSS, JavaScript, and inline &lt;img&gt;\u2019s.<\/p>\n<pre>&lt;head&gt;\r\n  &lt;meta name='case' data='breakpoint1' media='min-width:350px' \/&gt;\r\n  &lt;meta name='case' data='breakpoint2' media='min-width:1000px' \/&gt;\r\n&lt;\/head&gt;<\/pre>\n<h2>Deconstructing the case for breakpoint management in &lt;head&gt;<\/h2>\n<p>To re-cap our current problems with breakpoint management; we write our Media Query\u2019s dozens of times in CSS files, we write the same tests in our JS files, and we will soon write them inside every &lt;picture&gt; element too. The same tests, in every case, but with a different \u201cdo something if true\u201d instruction. That\u2019s wasteful in terms of repetition, becomes very hard to manage, requires continuous re-processing of a test that never changes, and is future unfriendly.\u00a0<a href=\"http:\/\/mattwilcox.net\/archive\/entry\/id\/1081\/\" target=\"_blank\" rel=\"nofollow\">Read more detail here<\/a>.<\/p>\n<p>So why does that code up there solve all these issues?<\/p>\n<p>At first glance, that code is going to cause many people a few frowns. We are not used to seeing this sort of thing &#8211; meta elements are supposed to describe a property of the URI aren\u2019t they? Well, no. We already set meta elements that do not describe a property of the loaded URI &#8211; all of us do. Every time we set &lt;meta name=&#8221;robots&#8221; content=\u2019index,follow\u2019 \/&gt; or any associated instruction. Or when we tell Google Translate to not run on the page. Or when we tell IE not to use its picture menus. These are all examples of using a meta tag to control the behaviour of some software that\u2019s consuming the URI\u2019s HTML, and not to describe properties of the page itself.<\/p>\n<p>What the code above does is set \u2018case\u2019 to equal a particular value, when a media query expression is matched. And if that\u2019s done in the HTML &lt;head&gt; we know that absolutely everything else can rely on it &#8211; the &lt;head&gt; is the very first thing parsed by a browser, even before anything inside &lt;body&gt;. Anyone familiar with working on the \u2018responsive images\u2019 problem should at this point be getting very excited. It means that the browser can be aware of the need to adapt\u00a0<em>before<\/em>\u00a0any pre-fetch behaviours have been triggered by finding an &lt;img \/&gt; element in the mark-up.\u00a0That is a major advantage and is how this solution solves the problem of image pre-fetch.<\/p>\n<p>Some people will be uncomfortable with having the queries in the head, but I&#8217;d say putting the queries into the CSS is, most of the time, backward \u2013 it\u2019s just that we\u2019re used to doing it that way so it makes a certain kind of sense.\u00a0Remember that the order we actually build a site goes like this:<\/p>\n<p>1) Consider a device size<br \/>\n2) Create a design for that size on paper or in Photoshop<br \/>\n3) Write the code to implement it<br \/>\n4) Increase device size and goto 1<\/p>\n<p>All of our code is actually hanging off the same media query breakpoints, because they&#8217;re the design breakpoints. So why re-define the same tests in each technology rather than do it once in HTML and have the CSS and JS just reference them? We could still do specific tasks in the &#8220;old&#8221; way, if desired.<\/p>\n<h2>How would we use these &lt;meta&gt; elements?<\/h2>\n<h3>In HTML<\/h3>\n<p>Addressing first how we might use it to control &lt;img\/&gt; in our mark-up:<\/p>\n<pre>&lt;body&gt;\r\n  &lt;img src='\/content\/images\/{case}\/photo.jpg' alt='' \/&gt;\r\n&lt;\/body&gt;<\/pre>\n<p>What has this bought us?<\/p>\n<ol>\n<li>Well, we have a single image element with no custom properties or other code, which will adapt to any number of breakpoints you want to declair, without ever adding another character to the image code.<\/li>\n<li>We have something that is backward compatible\u00a0<em>right now<\/em>: either create a directory called \u201c[case]\u201d so that a file exists there, or if you want to be cleverer about it, set your server to alias [case]\u2019s path to an already existing path.<\/li>\n<li>The image is cachable by proxies and CDNs because the source for each version of the image has a unique URI.<\/li>\n<li>UPDATE: By using {} instead of [] this solution is actually complying with the <a href=\"http:\/\/code.google.com\/p\/uri-templates\/\" target=\"_blank\" rel=\"nofollow\">URI Template<\/a>\u00a0too (See the <a href=\"http:\/\/tools.ietf.org\/html\/rfc6570\" target=\"_blank\" rel=\"nofollow\">RFC<\/a>) &#8211; <a href=\"#comment-761\">thanks Brett<\/a>!<\/li>\n<\/ol>\n<p>Are there any drawbacks?<\/p>\n<p>None that I can see. Even with the current\u00a0proposal, we should never change the alt attribute because all resources are supposed to have the same semantic meaning.<\/p>\n<h3>In CSS<\/h3>\n<p>Rather than writing endless clauses like this, where a test must be performed each time:<\/p>\n<pre>@media only screen and (min-width: 700px) { ... }<\/pre>\n<p>We could write this instead:<\/p>\n<pre>@media (case: breakpoint1) { ... }<\/pre>\n<p>What has this bought us?<\/p>\n<ol>\n<li>We are abstracting the test itself away from the block of code that defines a response. This means if we decide to change the breakpoint condition, we do it\u00a0<em>once<\/em>\u00a0in the &lt;head&gt; and everything else works automatically.<\/li>\n<li>We reduce the number of tests required of the browser. It\u2019s not re-running a test, it\u2019s instead checking the value of case &#8211; which has already been computed.<\/li>\n<li>We have smaller code. Yeah, i know, it\u2019s a tiny point but it\u2019s a benefit non the less.<\/li>\n<\/ol>\n<p>Are there any drawbacks?<\/p>\n<p>Not that I can see, because you can still carry on using existing syntax if you like, this is just an additional capability we may want to take advantage of. It can also be used in all the same ways you use any existing media-query.<\/p>\n<h3>In the case of JavaScript<\/h3>\n<p>It\u2019s largely the same as for CSS. Instead of doing its own tests, it can just use whatever value is stored in the \u2018case\u2019 node. There is prior art for this: the behaviour here would be exactly the same as the wonderful shiv that a lot of people are now using <a href=\"http:\/\/adactio.com\/journal\/5429\/\" target=\"_blank\" rel=\"nofollow\">Conditional CSS<\/a>, but it&#8217;d check against the meta element and not the body:before pseudo element.<\/p>\n<h2>So why might this not work?<\/h2>\n<p>I\u2019ve only analysed this from the perspective of an author. I do not know how much effort it might be for a browser vendor to actually implement, and whether they would be willing to do so. For me, this is the only potential downside to the whole idea &#8211; it needs the vendors to implement it. In terms of functionality and benefits of the approach, i can see no downsides to the idea itself.<\/p>\n<p>I think vendors may object initially to the idea of browsers having to do this:<\/p>\n<p>1) detect the existence of a specific meta tag<\/p>\n<p>2) if it exists scan all src=&#8221;&#8221; properties for the variable string to replace with the variable value<\/p>\n<p>If no meta tag was there, it wouldn&#8217;t have to do anything with the src strings.<\/p>\n<p><strong>How likely are variables?<\/strong><\/p>\n<p>There is a history of our core technologies not supporting Variables, but there is a trend of adding them recently:<\/p>\n<p>1) We now have variables in CSS (\u00a0<a href=\"http:\/\/dev.w3.org\/csswg\/css-variables\/\" rel=\"nofollow\" target=\"_blank\">http:\/\/dev.w3.org\/csswg\/css-variables\/<\/a>\u00a0)<br \/>\n2) We now have variables in URIs (\u00a0<a href=\"http:\/\/code.google.com\/p\/uri-templates\/\" rel=\"nofollow\" target=\"_blank\">http:\/\/code.google.com\/p\/uri-templates\/<\/a>\u00a0)<br \/>\n3) We\u2019ve always had variables in JS<\/p>\n<p>So, HTML\u2019s the last one standing without support for variables.<\/p>\n<form action=\"http:\/\/mattwilcox.net\/processing\/comment.php\" method=\"post\"><\/form>\n<p>&#8212;&#8212;&#8212;&#8211;<\/p>\n<p>N.b., This post is a re-post from a blog entry I wrote, it was suggested on Twitter that the Community Group should see it. Source: http:\/\/mattwilcox.net\/archive\/entry\/id\/1091\/<\/p>\n<p>The suggestions here are a mix from an <a href=\"http:\/\/mattwilcox.net\/archive\/entry\/id\/1082\/\" target=\"_blank\" rel=\"nofollow\">older suggestion of mine<\/a>\u00a0which worked with &lt;picture&gt;, and <a href=\"http:\/\/www.w3.org\/community\/respimg\/2012\/05\/11\/respimg-proposal\/#comment-709\">the ideas of Denis LeBlanc<\/a><\/p>\n<p>&#8212;&#8212;&#8212;&#8211;<\/p>\n<p>UPDATE: Philip Ingrey has <a href=\"http:\/\/pci.github.com\/metavar_polyfill\/\" target=\"_blank\" rel=\"nofollow\">written a polyfil for this soultion<\/a>!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Managing responsive designs is hard, so let&#8217;s use our &lt;head&gt; Here is what I believe is the best proposition yet for managing our responsive designs. This includes managing our CSS, JavaScript, and inline &lt;img&gt;\u2019s. &lt;head&gt; &lt;meta name=&#8217;case&#8217; data=&#8217;breakpoint1&#8242; media=&#8217;min-width:350px&#8217; \/&gt; &hellip; <a href=\"https:\/\/www.w3.org\/community\/respimg\/2012\/05\/13\/an-alternative-proposition-to-and-srcset-with-wider-scope\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1289,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_s2mail":"yes","footnotes":""},"categories":[1],"tags":[],"class_list":["post-214","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.w3.org\/community\/respimg\/wp-json\/wp\/v2\/posts\/214","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.w3.org\/community\/respimg\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.w3.org\/community\/respimg\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.w3.org\/community\/respimg\/wp-json\/wp\/v2\/users\/1289"}],"replies":[{"embeddable":true,"href":"https:\/\/www.w3.org\/community\/respimg\/wp-json\/wp\/v2\/comments?post=214"}],"version-history":[{"count":13,"href":"https:\/\/www.w3.org\/community\/respimg\/wp-json\/wp\/v2\/posts\/214\/revisions"}],"predecessor-version":[{"id":216,"href":"https:\/\/www.w3.org\/community\/respimg\/wp-json\/wp\/v2\/posts\/214\/revisions\/216"}],"wp:attachment":[{"href":"https:\/\/www.w3.org\/community\/respimg\/wp-json\/wp\/v2\/media?parent=214"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.w3.org\/community\/respimg\/wp-json\/wp\/v2\/categories?post=214"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.w3.org\/community\/respimg\/wp-json\/wp\/v2\/tags?post=214"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}