Resizing images in responsive mobile design (Mobilizing websites with responsive design and HTML5 part 6)

This blog post is a part of Mobilizing websites with responsive design and HTML5 tutorial. For all posts please see the Introduction post.

When you change your layout dynamically you need to re-fit media elements to match the new layout dimensions. There exist three basic techniques dealing with images in responsive designs

  • Resizing images using CSS only  – in this case the clients download the full image payload regardless of the image size and image visible sizes are adjusted using CSS styles
  • Co-operative image resizer on the front end web server: when HTML is written from a template the image tags are written in such a way that <img src> attribute points to a resized image. The resizer rewrites image to match the vrowser screen dimensions. This is done e.g. in Web and Mobile for Plone CMS.
  • Independent image resizer on the front end web server: Javascript + server-side stub component is used to resize the images. The server-side component does not depend on the rest of the front-end server system, thus making the front end code less complex. A client-side Javascript scans HTML DOM tree for <img> tags and rewrites <img src> to go through the resizer. For example Adaptive Images (below) is this kind of a solution. Alternatively, an independent image resizer can be a part of the middleware stack (e.g. WSGI in Python)

In this blog post we mostly discuss about the simplest approach: resizing images using CSS only.

1. Resizing images using pure CSS

If your responsive layout includes images, as contentish or style elements, they might not work well with small screens as is. You can fix the situation by resizing the images dynamically with CSS.

In our example case the page has only one image: the site logo. If viewed in a mobile browser using desktop CSS the logo might be too small or too large. Thus

  • We remove any hard-coded width and height values for the <img> in HTML
  • We apply CSS style for the image width where the image takes width from the screen width as percents, but still has max-width set in pixels so that tablet devices with more screen estate don’t scale the image too much

Related CSS:

/* Logo must not appear too large on mobile */
#portal-logo img {
   width: 33%;
   max-width: 180px;
}

Before fix:

After fix:

2. Server-side image resizing solutions

You can also resize images dynamically on the server-side, so that mobile browsers load the reduced size version, saving the precious mobile bandwidth. However, this adds additional server-side component to the mix, increasing the complexity of the solution and especially when dealing with legacy sites consider whether the improved user experience is enough to balance this trade-off.

Also, the dynamic image resizer component is easily subject to denial of service attack because the HTTP responses it serve are usually slow and computationally intensive. If you decide to use one of these components be sure you are aware of these implications.

You don’t need to change the server-side generated HTML to achieve the dynamic images sizes. There exist a technology neutral solutions relying on the client-side Javascript and additional serve-side proxy component.

Adaptive Images is one of server-side image resizing solutions based on Javascript and minimal server-side PHP script. It operates in independent manner and adheres the contemporary best practices of HTML5 and responsive web designs.

\"\" Subscribe to RSS feed Follow me on Twitter Follow me on Facebook Follow me Google+

8 thoughts on “Resizing images in responsive mobile design (Mobilizing websites with responsive design and HTML5 part 6)

  1. Great posts.
    You have covered almost every aspects of the mobi designing.
    I am about to design my website and your tut will help me a lot in designing and optimising the mobi version.
    Thanks for the tuts.

  2. Pingback: My Site is Now Responsive | Flarnie Marchán

  3. Great! After a lot of searching I found your easy-to-follow css code for resizing my client’s logo for mobile!

    Thank you

  4. Hi, Thanks for sharing the informative info.
    I am trying to use small images for mobile devices. How can I ensure if the request is from mobile user use different home page with small images. The site I have is responsive but it takes time to load as it has to adjust the image size.Same site load fast on desktop. Is there any way I can use different pages for mobile and desktop. Its on magento framework. If some one want to have a look try to load Tailor website in google speed test. You will see same site loads fast on desktop but slow in mobile. Thanks in advance. I am looking forward for suggestions. happy coding.

  5. If you’re super-competent at all of these web-development techniques, why is this page built with WordPress?….

Leave a Reply

Your email address will not be published. Required fields are marked *