7 ms·
First draft of HTML img srcset for responsive images
- alexchamberlain 14y agoI like the concept, but the syntax feels awkward, clunky and very specific. Why not repurpose the media attribute from style tags to all other tags?
- annevk 14y agoBecause it is way more verbose and does not address the pixel density case.
- alexchamberlain 14y agoBut pixel density is a property of the media, which should be dealt with by media queries.
- annevk 14y agoQuerying the pixel density of the device is not the same as describing the pixel density of the resource. This has been a common misconception in the discussion of this feature.
- alexchamberlain 14y agoApologies - I misunderstood the proposal. However, what is the pixel density of a PNG?
- julian37 14y agoPPI can be stored in the "pHYs" chunk in a PNG. And some image processing/editing tools do so, such as Photoshop starting with version 7. http://www.libpng.org/pub/png/book/chapter11.html#png.ch11.div.8 http://www.libpng.org/pub/png/book/chapter11.html#png.ch11.d... http://www.scantips.com/basics9p.html http://www.scantips.com/basics9p.html
- othermaciej 14y agoWhat it's representing is not necessarily an intrinsic property of the image but rather the intended pixel density for presentation. Say I have a 600x200 PNG which fits my design for normal resolution displays where 1 CSS pixel = 1 device pixel (which is most current laptop screens, for instance). But then I'd like to take advantage of the extra resolution on a device that displays at 1 CSS pixel = 2 device pixels, such as a retina display iPad. So then I get a 1200x400 version of the same image (perhaps both downscaled from a super high res original) and serve it as the 2x image. It will take the same logical space in the layout but provide extra quality on a higher resolution display. There's no need to do anything special to the PNG.
- alexchamberlain 14y agoAwesome reply. I now understand. This is still a property of the media though.
- user24 14y ago> the syntax feels awkward, clunky and very specific Yeah, stuff like "2x 100w" looks a bit like magic numbers[1] Also there's potential for confusion in 'x' - does it mean X as in a co-ordinate i.e. width, or zoom? I'd rather see something more verbose like this: <img src="foo.jpg"> <set src="foo1.jpg" width="100"> <set src="foo2.jpg" density="2"> </img> Yes it's more wordy to write, but you read code more than you write it so it makes sense to optimise for readability over byte-shaving. [1] http://catb.org/jargon/html/M/magic-number.html http://catb.org/jargon/html/M/magic-number.html
- othermaciej 14y agoThere are certainly other ways to do it, but this particular way won't work. <img> was historically a "void element" in past versions of HTML, and past browsers, so it can never have child elements.
- annevk 14y agoWe cannot change the way <img> is parsed. See e.g. http://software.hixie.ch/utilities/js/live-dom-viewer/ http://software.hixie.ch/utilities/js/live-dom-viewer/ for the tree you get with that markup. It's not what you expect.
- user24 14y agoFair enough, but that is a technical limitation which is trivial to get around: <setsrc> <img src="foo.jpg"> <set src="foo1.jpg" width="100"> <set src="foo2.jpg" density="2"> </setsrc> Always be wary of allowing arguments that hinge on "it's not technically possible" to shut down good ideas.
- Kilimanjaro 14y agoFTFY <imgset> <img src="foo.jpg"> <img src="foo1.jpg" width="100"> <img src="foo2.jpg" density="2"> </imgset>
- 14y ago
- est 14y agoI think lots of informal URLs consist U+002C COMMA character (,) Why not <img srcset="url_1" srcset="url_2">? Is this against SGML syntax?
- mooism2 14y agoThat is against sgml syntax, yes. There must be at least one height / width / pixel density descriptor for each url, so if you see a comma before the url ends, you know it must be part of the url.
- Herover 14y agoWhy not like the video-tag <img> <source src="house-hd.jpg" width="100px" height="200px" density="2x" /> ... </img> Or something like that.
- alexchamberlain 14y agoSee necolas comment above - ie the propose picture element.
- mooism2 14y ago> "2x" means "maximum pixel density of 2 device pixels per CSS pixel" I don't understand why that's maximum pixel density and not minimum pixel density. If I'm using a super-retina device with 4x pixel density, surely it's better for me to get the 2x image than the default (presumably 1x) image?
- annevk 14y agoThe algorithm actually picks the resource you would expect there so that is a bug in the note. Filed a bug: https://www.w3.org/Bugs/Public/show_bug.cgi?id=17057 https://www.w3.org/Bugs/Public/show_bug.cgi?id=17057
- necolas 14y agoPrevious discussion about this by developers (170+ comments) who have been discussing Responsive Images in a W3C Community Group - http://www.w3.org/community/respimg/2012/05/11/respimg-proposal/ http://www.w3.org/community/respimg/2012/05/11/respimg-propo...
- alexchamberlain 14y ago+1 to the picture syntax.
- codeka 14y agoThe only problem with the <picture> element is how to address backwards compatibility? The srcset attribute is backwards compatible because old browsers will just ignore srcset and use the src, but old browsers will ignore the <picture> element completely -- how do you get something that works for older browsers but also works for new browsers as well?
- othermaciej 14y agoThe <picture> approach also has a sort of solution for degrading gracefully in older browsers. You can nest an <img> in your <picture>. Newer browsers are supposed to ignore it, older browsers would ignore the surrounding <picture> element instead. I think there are other good reasons to prefer the srcset approach, but not this one. The email here provides motivation: <http://lists.w3.org/Archives/Public/public-whatwg-archive/2012May/0247.html> http://lists.w3.org/Archives/Public/public-whatwg-archive/20...;
- necolas 14y agoUnfortunately, at the time when Ian didn't seem to care about the issue of responsive images, I asked him about the possibility of using the existing `<img>` element and he said it was pointless to try and modify existing image parsing. Forget it. Now, he decides that actually this is the way to go. Frustrating. For reference, in the email linked above, Ian Hickson said: > So why not just give the UA the characteristics and a template to use to build the file names itself? That way we still give the UA all the same information, but it is much less verbose and still solves all the same use cases. Thus: <img src="face-600-200@1.jpeg" alt="" src-template="face-%w-%h@%r.jpeg" src-versions="600x200x1 600x200x2 200x200x1"></code>
- karl_nerd 14y agoHere's an interesting example with <img srcset> http://junkyard.damowmow.com/507 http://junkyard.damowmow.com/507 When I saw the last line in the doc linked above, I realized that <img srcset=""> is probably optimized for adding one (1) "retina" image for iPhones and other high-DPI devices.
- tferris 14y agoBetter than this specification hassle is this solution: The browser should send the density of the screen in ppi (calculated by the screen resolution and the screen information provided by the OS) together with all the other standard stuff like OS, screen resolution, etc. With this information the server can decide which images to send.
- ars 14y agoNo, this won't work in the wild. It breaks caching completely. It requires scripting just to serve an image. The same url can have different data - imagine if you resume downloading a file, only to suddenly have a different file.
- alexchamberlain 14y agoI disagree. Caches would have to be intelligent. Remember they already have to pay attention to language headers etc. URLs represent a conceptual resource, rather than a single file.
- ars 14y agoCaches (proxies) already exist. You don't have the option of reprogramming them for this new header.
- michaelmior 14y agoPart of the problem here is that there are a wide range of pixel densities. The server could choose to display the same image for some range of values but trying to get a caching proxy to sort this out is highly nontrivial.
- othermaciej 14y agoScreen PPI (in physical pixels per physical inch) is not the relevant consideration - device pixels per CSS pixel being something other than 1 is the case you care about. And the browser needs to know the intended resolution of the image so it can tell whether to downscale it.
- ars 14y agoSee also: https://etherpad.mozilla.org/responsive-assets https://etherpad.mozilla.org/responsive-assets for some other proposals.
- ZenPsycho 14y agoThis is silly and makes things way more complicated than they need to be. We have HTML tags, to which we can apply attributes directly via blah=blah syntax Or we can add attributes by making a style attribute, and using css syntax inside of it. Or we can have a space separated list of class names, and have attributes applied to an element externally by a CSS stylesheet. Or we can have an ID, and apply attributes using javascript. We can apply attributes with http headers, and the browser can negotiate the resource with http accepts and quality parameters. We have video, audio, and object tags that can all have child elements. We have query parameters and fragment selectors, and path parameters, and post parameters and accept parameters for negotiating and selecting content from a server. (Designed to include ampersands which have to be entity encoded when used in HTML!) We have all these sophisticated overlapping complicated mechanisms for attaching attributes to elements in a webpage. We have huge pieces of software for parsing through this poorly thought through mess. But no, not a single one of these is good enough for this problem. LET'S INVENT A NEW SYNTAX AND ADD TO THE HEAP OF IDIOCY.
- hoppipolla 14y agoFrom an implementation point of view, putting everything in a single attribute is way simpler; the design of <video> is quite problematic. The reason is that with a single attribute everything can be processed atomically. With a design based on multiple elements things can happen between elements being processed, creating many more edge cases. Although in general things that make authoring easier are preferred, designs that are easy to implement in a bug-free way are generally preferred over designs that are complex and likely to lead to interoperability issues. After all, having to fight implementation differences is a rather common complaint about the web platform. It also hasn't been clearly demonstrated that multiple elements are easier to work with. They are more familiar, but significantly more verbose. I don't know how that plays out in practice.
- ZenPsycho 14y agoI gave you a whole list of options but you only focus on these two. why?
- andybak 14y agoI don't care if the syntax is a bit unsightly. Let's just get this into shipping browsers as soon as possible. It's a major requirement for responsive design and this has good fallback behaviour. We can all just stick to 'mobile first' design and a .js polyfill shouldn't be hard.
- ZenPsycho 14y agoOr, here's just a crazy out there thought. What if we save our images in high res progressive format, and program the browsers to stop downloading the images as soon as they've acquired enough resolution? Caching can be done with http range requests. Older user agents just end up downloading larger images than they wanted. Javascript and server script can use the JPEGtran library to retrieve lossless low res versions of jpeg images. Similar things could be done with png. No modifications to any standards need to take place.
- woodall 14y agoIs there a way this can take place entirely server side?
- ZenPsycho 14y agosure, it would be simple enough to copy what was done with the server side image maps thing, and define some query parameters that get requested along with the image. So each different size gets its own url and proxies and caches are happy. servers that don't support it just serve a normal image. a tiny bit of javascript can polyfill it in now. today.
- hoppipolla 14y agoThat is a very appealing design, but sadly it doesn't address all the use cases. In particular one thing that people want to be able to do is to display a different image depending on the viewport dimensions, for example a closer crop on a small screen compared to that shown on a larger screen.
- ZenPsycho 14y agowhat is stopping this approach from addressing that use case?
- michaelmior 14y ago
- bobsy 14y agoLooks like a horrible solution which will add bloat and complexity to a webpage. I would rather see a solution implemented with CSS or even a meta tag. <meta name="srcset" content="2x=HD; 100w=phone; 100w 2x=phone-HD" /> The above example is exactly the same information as in this link. However it is expressed in one place and thus is far simpler. The only reason to do this per image would be if you different naming conventions for individual images.. which would just be silly. You could then have a nosrcset attribute which you can add to images which do not have high-res versions. This would save an http request.
- pagejim 14y agoThey have started testing on different browsers. http://testcases.glitchmr.pl/html/img-001.html http://testcases.glitchmr.pl/html/img-001.html
- fiznool 14y agoAn alternative, completely server-side implementation: http://adaptive-images.com/ http://adaptive-images.com/ It takes the approach of setting a cookie when the page first loads with the viewport size, determined through media queries. Every request to the server then includes this cookie. When an img is requested (as an <img> tag or through CSS background-image) the cookie is read to determine the viewport size of the client. The PHP script then serves up the most appropriate image size to the client. The only thing you really need to do is add the .htaccess rule (to intercept image requests and route them through the PHP script), the php script itself and the highest resolution you need for each image. The script takes care of scaling down the image for smaller screens, caching them for later use. I like the way this requires nothing on the client side, other than a cookie being set. All the heavy lifting is done on the server.
- Kilimanjaro 14y agoI believe this should be moved to the stylesheet. We already have ways to determine media/viewport/device/orientation etc so adding different imgs would be a no brainer. Bonus point, less cluttered HTML.
- jacobr 14y agoThat would mean the browser couldn't start downloading any image before it had downloaded and parsed the stylesheet. Or that it would download all images and the entire point of saving bandwidth is lost.
- Kilimanjaro 14y agoDownloading all images is out of the question. You use the most commonly used image as default and when the stylesheet is done you download whatever img is needed. Or, you use the newly minted 'defer' (I just came up with that) attribute to let the browser know not to download anything until the stylesheet is parsed. <img src='myhugeretinapic.png' defer>
- juddlyon 14y agoResponsive images are obviously needed but this syntax is noisy and confusing. Adding info after the file extension would make it too easy to overlook.
- benwerd 14y agoI agree that the syntax isn't great, and a corresponding set of CSS rules would be welcome, but this does solve a problem. What I'd love to see is something similar for bandwidth sets, so lower bandwidth connections can be sent lower quality images. And then for both features to be made available for video tags.
- pbhjpbhj 14y agoThis really feels like the wrong approach to me - like this will lead to "best viewed with iPhone 5" pages. Whilst there will be ways to automate the setting of such img srcset attributes based on the device (eg the PHP method mentioned that relies on a cookie) this sort of tailoring for specific device [characteristics] seems wrong somehow. IMO this would be better handled by devices declaring their preferred viewport and/or pixel density just as they declare language preferences now, ie browser headers. Then the server can be set to send different images based on the header values and we don't have to worry about having a massive "srcset" attribute on each image tailoring to 500 different device variants. A server config would specify what viewport ranges to use and tell the server how to modify the images sent (or modify the path, or modify the location - eg switch an image with a folder called image.jpg and have filename tagged images for the different variants handled). Perhaps this seems hacky too ... In any case the SRCSET attribute proposal here looks short-sighted and hackish to me.
- deleted 14y ago[deleted]
- WiseWeasel 14y agoYeah, the srcset for every device type on every single image seems excessive. I was hoping we could take advantage of file naming conventions and define a way for user agents to request 'filename' + 'variant key' + 'filename extension' instead of the 'filename.extension' file in the src attribute when it wants non-1x images, where 'variant key' is something like "_2x" added to the filename for 2x images. Then you could just flag img tags as having 2x available boolean. I don't think whatwg likes the idea of file names as metadata, though.
- jacobr 14y agoIan Hickson, the HTML specification editor, reasons in length about responsive images here: http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2012-May/035855.html http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2012-May...