Responsive Media
There is a big discussion going on at the moment covering the actual point of how to deal with images and media et al. on mobile-phones and other devices as there are some things that don’t work as on a desktop computer:
- Bandwidth
- Screen size
- Performance / Velocity
The question is how to deliver responsive images on a mobile website.
You maybe use something like img { max-width: 100%; height: auto; }
in your responsive design to prevent images from being bigger then the screen of a mobile device.
In most of the cases this shrinks images in its displayed size, which is a processor intensive thing to do – especially when you are on a mobile device and don’t have endless processor capacity this hurts.
The shrinking itself does not change the file-size that has to be loaded; it remains the same as on a desktop for example. As bandwidth is the bottleneck these days if you are on a mobile device, this is the issue where people want to see some improvement.
What can we do about it?
There are a couple of proposals and known techniques on how to solve the problem:
1. Resize images with PHP
This is something you could only do if you know the screen-size before you load the images them self. So you have to inject HTML through JavaScript for example and change the sources for images accordingly.
The Adaptive Images framework a similar technique. Check out the source on GitHub.
2. Request different images using data-attributes
An article by Nicolas Gallagher covers this method using CSS to detect which source would be best to use.
The method could work like this: You have an image which has a really small file size and is served for every device at first (mobile first approche).
<img src="image.jpg" alt="" data-src-600px="image-600px.jpg" />
Now you are going to change the file if the screen is bigger by using the content
-property of CSS and its attr()
function.
@media (min-device-width:600px) { img[data-src-600px] { content: attr(data-src-600px, url); } }
Please have a look at the article for detailed information.
An idea that goes beyond the replacement of images with the attr()
-function is the usage of content: url(myimage.jpg) replaced;
. This is something suggested by Tab Atkins Jr. on the W3C www-styles mailing list – called replaced content.
Something about replaced content appears in the CSS-spec but it does not look to be exactly what Tab is referring to.
3. Introduce new attributes to the <img>-tag
Anselm Hannemann shares the idea of introducing a media-attribute that has some sort of “sizing-parameter”. It is supposed to be combined with the media-src-attribute which also has the sizing-parameter.
The HTML could look something like this:
<img src="myimage_xs.jpg" media-xs="(min-device-width:320px and max-device-width:640px)" media-src-xs="myimage_xs.jpg" media-m="(min-device-width:640px and max-device-width:1024px)" media-src-m="myimage_m.jpg" media-xl="(min-device-width:1024px)" media-src-xl="myimage_xsl.jpg">
As you can see the media
-attributes contain media-queries which describe when to take a certain image-source.
4. Using JavaScript
There are some ideas on how to deal with this topic using JavaScript.
The FilamentGroup for example published its “Responsive Images”-project on GitHub. They are using “Mobile-First” to present a small-sized image on every device and replace this with JavaScript on window load.
You can find out more about how it works on their blog. They also published a demo.
Peter-Paul Koch presented a possible way on how to combine media queries with JavaScript. Which is not really new I think but a good summery.
Another approach could be a new attribute for the script
-tag: Scott Jehl suggested to call it preparse
. The script could then be used to replace sources in HTML fitting the current client-need.
[Image]
5. A new image-format containing different sizes
People aso had the idea of creating an own image-format which contains for example four layers with different image sizes. The image then “decides” which format to take and serves this to the client without its three sibling-layers.
At the moment there is a format called MrSID developed by LizardTech which can have one or more compression rates.
Another approach is the JPEG 2000 file-format. It can carry out different optimization-levels for images. Read more about it on the Wikipedia-page I linked above.
Media? But this was only about images…
Yup, right! Images are just one aspect when it comes to responsive media.
Videos for example are another thing. YouTube offers different video-sizes for watching. You have to choose manually which one to use. A possibility to select this resource automatically, build into the browser would be awesome!
JPEG 2000 has a video-equivalent developed by the same group of people as the JPEG-format called Motion JPEG 2000. Find more information about this here.
Conclusion
As it turns out many developers are not aware of the problems that come up with a real responsive design – even HTML5-specification writer are calling it “a niche problem”. This is why it is hard to develop a specification for a native API as for example Anselm Hannemann suggests.
But maybe the decision which technique might be best for solving the problem of serving responsive media has to be finished first before anything is spec’d up.
Moar!
Some more resources where the topic is discussed:
- A List Apart started the whole “Responsive Design”-discussion with an article by Ethan Marcotte
- jQuery Standards Team
- Scott Gilbertson on Webmonkey
- Another CSS-only idea by Harry Roberts
Thanks!
Thanks goes out to Anselm, for reading what I wrote and helping me with some further information.