There are still millions of legacy, desktop-heavy websites. Often made with ASP.NET or other unspeakable trickery.
One of my project was converting one of these websites into a nice and mobile friendly format. I first tried to do it solely with media queries, which worked good for the visual appearance. But it was bad for the experience, because it was very slow and the mobile browser had to downscale images.
Mobile-first!
As LukeW points out, there are good reasons to treat the mobile visitor as first class citizen.
And so I started to develop this concept to make a website desktop and mobile optimized:
CSS
- have general css rules in one place (applicable to all screen widths)
- for each set of resolutions, load extra css files just containing the required rules
- use link, not @import
- and use the media queries on link level to prevent unecessary css downloads
Example
HTML
<img class="dynamic"
data-large-src='/ImageGen.ashx?width=122&image=/media/123/logo.png'
data-small-src='/ImageGen.ashx?width=61&image=/media/123/logo.png'
/>
(in that particular case I’m using a special resizing library to serve the image in the proper size)
Javascript
Depending on what you need, you might just get away with inserting JQuery and a little width check, like this:
$(document).ready(function() {
var detectSmallScreen = function() {
window.OnSmallScreen = document.documentElement.clientWidth < 701;
if(OnSmallScreen) {
window.ScreenSizePrefix = 'small';
} else {
window.ScreenSizePrefix = 'large';
}
};
detectSmallScreen();
$(window).resize(detectSmallScreen);
$('img.dynamic').show();
$('img.dynamic').each(function(){
var el = $(this);
el.attr('src', el.attr('data-'+ScreenSizePrefix+'-src'));
});
});
Conclusion
With just a few lines of Javascript its possible to replace images on resize with smaller versions of it. Thats great!
I started to write this article two days ago and since then I extended a lot of it and integrated parameters to dynamically insert the image resizing into the url.