Curated by Carsonified

Learn with Treehouse

Accessibility, CSS3, Design, Django, HTML & CSS, HTML5, JavaScript, jQuery, NoSQL, PHP, Responsive Web Design, Ruby, Ruby on Rails, Tools, UX, Version Control, WordPress, iOS and more

Article 11

Responsive Design Image Gotcha

By @keirwhitaker

17 August 2010 | Category: Uncategorized

As Ryan mentioned last week we are in the process of redesigning and developing Think Vitamin. One of the things we are keen to integrate into the design is "responsiveness".

Apart from being all the rage and a hot topic it makes a lot of sense, let's make one site and adapt it for the device and resolution.

The Problem

Mike's been hard at work on the design and has moved onto the "build" phase. Part of this process is "playing" around with the relevant media queries to see the effect they have in the iPhone simulator. One thing we couldn't quite understand was the effect on images, they all appeared much smaller than we had expected given their size and the width in pixels of the iPhone screen.

After reading through the CSS of a number of sites including 8faces and HicksDesign we came upon the solution. It's all about the meta!

The Solution

In order for your images (and text) to maintain their “real” size all you need to do is add in a meta tag (below) to your site’s head section.

<meta name="viewport" content="width=device-width; initial-scale=1.0">

Effectively this is telling Safari (these meta tags are Apple specific) to set the scale to 1, in other words render everything at the original/native size.

What this means is that an image with a width of 280px will be rendered as 280px wide and will therefore take up the majority of the width of the iPhone screen in portrait mode. Without this setting the default is to shrink the image proportionally.

Here’s a visual comparison using Elliot’s 8faces logo (I hope he doesn’t mind). For reference this page is being rendered using a media query to target the device, in this case the iPhone.

No initial-scale set

initial-scale set to 1.0

As you can see from the screen grabs both the image and the text are scaled up. This may or may not be the desired end result depending on your needs.

Learning from Others

Here’s the meta tags from both 8faces and the dConstruct 2010 site for reference. Notice that they both use other settings, more info on these viewport values can be found on the Safari HTML reference site.

8faces.com

<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0" />

dConstruct 2010

<meta name="viewport" content="width=450">

Obvious When you Know

We scratched our heads on this for a while so we thought it would be worth a quick write up. It’s obvious when you know but we hope that it comes in handy if you haven’t ventured into the world of “responsive design” as yet.

Follow @thinkvitamin on Twitter Please check out Treehouse

Other Posts You Might Find Interesting

  • Sorry - No Related Posts Found

Comments

  • http://www.geteval.com Benjamin Alijagić

    Cool tip! Gonna try that on my new app (www.geteval.com).

  • http://neilcowburn.com/ Neil Cowburn

    While the viewport meta tag was initially created for Mobile Safari on the iPhone, other vendors & browsers (such as Android and Firefox Mobile) are adopting it. So it’s no longer Apple-specific, as stated in the article.

  • Keir Whitaker

    Hey Neil, thanks for clarifying that. Makes sense that they are adopting it.

  • http://misteroneill.com Patrick O’Neill

    On the topic of responsive design gotchas, the issue I have with just using media queries (and meta tags) is wasted bandwidth and degraded performance.

    On dConstruct, you don’t even see the portrait images, so you’re downloading 200kb you don’t need. In no way is this a trivial amount.

    In some browsers, scaling images has been known to cause performance degradation – especially when hidden. This is less of an issue with good WebKit-based browsers, so probably not a major concern on iOS Safari, but it’s something to watch out for.

    We should approach responsive design as more than media queries and meta tags. These are fine solutions for style, but can and should be used in conjunction with other techniques – such as resolution-dependent asset management via JavaScript or serving up different assets by reading user agent strings on the server-side (the less bulletproof option).

  • Luca Salvini

    Or you could serve higher resolution images using media queries and a custom css file. Something like:

  • Luca Salvini

    Or you could serve higher resolution images using media queries and a custom css file. Something like:

  • http://www.codegeek.net Ron Zasadzinski

    A helpful piece to the puzzle for sure. I’d seen that meta tag in the HTML files for a few “responsive” websites and had wondered what its purpose was. Thanks for filling in that detail.

  • tomi jarrye

    Such a wonderful topic for me.I read this article and agree with this article.Thanks to share your thought with us.

    Acai Detox Pure

  • Cinnak

    Hm, you don’t usually see spam slip through on wordpress blogs

  • http://Www.nogginbox.co.uk Richard

    Can you stop the screen rotating on an iPhone using the viewport meta tag?

  • lisa

    I found that the content on the screen kept moving around because of the scaling (even a site like http://www.hicksdesign.co.uk/ suffers the same problem when viewed on an iPhone.).
    Removing the scaling makes the screen scroll beautifully only up and down: But then I loose the larger images of course. :(

    Is threre any way I can do an initial-scale=1.0 but not have the screen moving around after for example clicking or going from landscape to portrait view?

Badges for Treehouse

Treehouse

Learn iOS, Rails, CSS3, jQuery, Node.js, HTML5, UX and more in less than 8 minutes per day. New videos added regularly. Sign up today and get a free Web Design Toolkit.

Ads Via The Deck

Think Vitamin Radio
Episode #34: Amazon Fire and Responsive Roundtable

Check out our bi-weekly radio show. Covering the hot topics on the web.

Audio clip: Adobe Flash Player (version 9 or above) is required to play this audio clip. Download the latest version here. You also need to have JavaScript enabled in your browser.

Download Podcast as mp3

Advisory Board

The Think Vitamin Advisory Board in place make sure that you receive the best content possible.