RWD (Responsive Web Design) is increasingly popular over past few years. One important problem faced by RWD is lack of a standardised solution to deliver resized images into multiple devices. We have wide range of device categories, screen resolutions, pixel densities and lack of bandwidth/connection detection methods, all made this process difficult. There is no simple silver bullet to solve this issue.
This is a quick post on a simple technique I used to create responsive image. This is a javascript solution, but it will work in javascript disabled environment by using a noscript fallback. This technique will start with mobile image first & there is no need to parse and replace image sources.
I’m using google picasa as my image host. I can resize images dynamically by setting width/height in the url.
Eg: //lh3.googleusercontent.com/-Rw0hpxt70co/ULeX80ncW3I/AAAAAAAAE6g/kIDj6u1zdyI/s640/query-loader1.jpg
Setting s320 will create image with width/Height (resize proportionally) 320px. We can also pass width or height. Setting w320 will create an image with width 320px or passing h320 will constrain height of dynamically created image to 320px. Now we have our server, which can provide us different images sizes upon request.
You can use your own server methods or other resources like src.sencha.io to resize images on the fly.
Technique
Using css fluid image technique scale image to viewpot. Get the device size and insert image using script.
CSS
1 2 3 4 5 | |
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | |
Paste it in your page header. This will set width of the screen into ‘cwResImg’ variable. You can set image breakpoints in this variable cwBpArry = [1382, 992, 768, 480];
HTML
Replace images in HTML with.
1 2 3 4 5 6 7 8 9 10 | |
It has two parts
1) noscript tag contains a standard image for fallback. If js is not supported, this image will show.
2) Script part
Syntax
1 2 3 | |
1
| |
JavaScript variable ‘cwResImg’ will replace by the max width of the device.
When page loads url will be like the following, if its a desktop with resolution more than 1382px
//lh6.googleusercontent.com/-IdiwvKGQ8Lk/UN9KOZvdW7I/AAAAAAAAFDk/iSvy1_SZrrM/s1382/RTT%2520Decodize%2520github.png
Demo
Performance and Bandwidth
After including this there is a noticeable performance improvement in iPhone waterfall view.
Before (11.866s)
After (8.841s)
Drawbacks
Messy HTML.
Accessibility