Google Webmaster Central Blog - Official news on crawling and indexing sites for the Google index

Responsive design – harnessing the power of media queries

Monday, April 30, 2012 at 6:07 PM

Webmaster Level: Intermediate / Advanced

We love data, and spend a lot of time monitoring the analytics on our websites. Any web developer doing the same will have noticed the increase in traffic from mobile devices of late. Over the past year we’ve seen many key sites garner a significant percentage of pageviews from smartphones and tablets. These represent large numbers of visitors, with sophisticated browsers which support the latest HTML, CSS, and JavaScript, but which also have limited screen space with widths as narrow as 320 pixels.

Our commitment to accessibility means we strive to provide a good browsing experience for all our users. We faced a stark choice between creating mobile specific websites, or adapting existing sites and new launches to render well on both desktop and mobile. Creating two sites would allow us to better target specific hardware, but maintaining a single shared site preserves a canonical URL, avoiding any complicated redirects, and simplifies the sharing of web addresses. With a mind towards maintainability we leant towards using the same pages for both, and started thinking about how we could fulfill the following guidelines:
  1. Our pages should render legibly at any screen resolution
  2. We mark up one set of content, making it viewable on any device
  3. We should never show a horizontal scrollbar, whatever the window size


Stacked content, tweaked navigation and rescaled images – Chromebooks
Implementation

As a starting point, simple, semantic markup gives us pages which are more flexible and easier to reflow if the layout needs to be changed. By ensuring the stylesheet enables a liquid layout, we're already on the road to mobile-friendliness. Instead of specifying width for container elements, we started using max-width instead. In place of height we used min-height, so larger fonts or multi-line text don’t break the container’s boundaries. To prevent fixed width images “propping open” liquid columns, we apply the following CSS rule:

img {
  max-width: 100%;
}


Liquid layout is a good start, but can lack a certain finesse. Thankfully media queries are now well-supported in modern browsers including IE9+ and most mobile devices. These can make the difference between a site that degrades well on a mobile browser, vs. one that is enhanced to take advantage of the streamlined UI. But first we have to take into account how smartphones represent themselves to web servers.

Viewports

When is a pixel not a pixel? When it’s on a smartphone. By default, smartphone browsers pretend to be high-resolution desktop browsers, and lay out a page as if you were viewing it on a desktop monitor. This is why you get a tiny-text “overview mode” that’s impossible to read before zooming in. The default viewport width for the default Android browser is 800px, and 980px for iOS, regardless of the number of actual physical pixels on the screen.

In order to trigger the browser to render your page at a more readable scale, you need to use the viewport meta element:

<meta name="viewport" content="width=device-width, initial-scale=1">


Mobile screen resolutions vary widely, but most modern smartphone browsers currently report a standard device-width in the region of 320px. If your mobile device actually has a width of 640 physical pixels, then a 320px wide image would be sized to the full width of the screen, using double the number of pixels in the process. This is also the reason why text looks so much crisper on the small screen – double the pixel density as compared to a standard desktop monitor.

The useful thing about setting the width to device-width in the viewport meta tag is that it updates when the user changes the orientation of their smartphone or tablet. Combining this with media queries allows you to tweak the layout as the user rotates their device:

@media screen and (min-width:480px) and (max-width:800px) {
  /* Target landscape smartphones, portrait tablets, narrow desktops

  */
}

@media screen and (max-width:479px) {
  /* Target portrait smartphones */
}


In reality you may find you need to use different breakpoints depending on how your site flows and looks on various devices. You can also use the orientation media query to target specific orientations without referencing pixel dimensions, where supported.


@media all and (orientation: landscape) {
  /* Target device in landscape mode */
}

@media all and (orientation: portrait) {
  /* Target device in portrait mode */
}



Stacked content, smaller images – Cultural Institute
A media queries example

We recently re-launched the About Google page. Apart from setting up a liquid layout, we added a few media queries to provide an improved experience on smaller screens, like those on a tablet or smartphone.

Instead of targeting specific device resolutions we went with a relatively broad set of breakpoints. For a screen resolution wider than 1024 pixels, we render the page as it was originally designed, according to our 12-column grid. Between 801px and 1024px, you get to see a slightly squished version thanks to the liquid layout.

Only if the screen resolution drops to 800 pixels will content that’s not considered core content be sent to the bottom of the page:


@media screen and (max-width: 800px) {
  /* specific CSS */
}


With a final media query we enter smartphone territory:


@media screen and (max-width: 479px) {
  /* specific CSS */
}


At this point, we’re not loading the large image anymore and we stack the content blocks. We also added additional whitespace between the content items so they are more easily identified as different sections.

With these simple measures we made sure the site is usable on a wide range of devices.


Stacked content and the removal of large image – About Google
Conclusion

It’s worth bearing in mind that there’s no simple solution to making sites accessible on mobile devices and narrow viewports. Liquid layouts are a great starting point, but some design compromises may need to be made. Media queries are a useful way of adding polish for many devices, but remember that 25% of visits are made from those desktop browsers that do not currently support the technique and there are some performance implications. And if you have a fancy widget on your site, it might work beautifully with a mouse, but not so great on a touch device where fine control is more difficult.

The key is to test early and test often. Any time spent surfing your own sites with a smartphone or tablet will prove invaluable. When you can’t test on real devices, use the Android SDK or iOS Simulator. Ask friends and colleagues to view your sites on their devices, and watch how they interact too.

Mobile browsers are a great source of new traffic, and learning how best to support them is an exciting new area of professional development.

Some more examples of responsive design at Google:


The comments you read here belong only to the person who posted them. We do, however, reserve the right to remove off-topic comments.

38 comments:

seo said...

Thanks sharing, great article useful for all web developers. Its helpful to improve business user experience.

gabriel dumitru said...

Everybody is going crazy about the Penguin and your silence is killing us ! Instead of posting this article you should say something about the "penguin" ! Will this be forever , will you change things ? What should we do ? Should we create low quality websites ?

squibble said...

If you applied this concept to your product forums it would be great.

GD said...

As this approach inevitably entails legitimately hiding some content from smaller viewports, has Google adapted its approach to hidden text and links?

Beben Koben said...

this is very usefully for learning
ty ty ty \m/

id said...

Beside of @media screen and `(max-width:479px){}` and `@media all and (orientation:landscape){}` use shorter syntax `@media (max-width:479px){}` and `@media (orientation:landscape){}`.

CyberBytes Design said...

Oh google and your taking off of page-speed rating for your site server-side image resizing, does the page-speed.. What, Am I supposed to have 4 different image sizes so each time the site grabs a different image when the screen is resized? I mean if you don't do this then that gives the mobile site a crappy pagespeed score even though it loads quite qickly, if you really want to maximize your pagespeed you have to take every image and keep saving different resolution images which is a pain... What should we do to keep sain/yet continue to get a good page speed? My site has over a 90 page speed on the normal browser, but when the screen is resized the server-scaled images due to responsive design brings it down to like a 65... I have some images get bigger when in column view for for mobile (so they don't need to use the javascript image viewer on mobile which goofs up from mobile browser to mobile browser) but then that hurts my pagespeed score for my default browsers because every image is a tiny bit too big, but scaled down a tiny bit for browsing...

I hope you can understand what I mean through all of this, any input would be appreciated,

Thanks!

Alex
(The site I'm talking about is at www.bathandshowerworks.com to get an example of what I'm talking about. I'm using the "Skeleton" layout)

Thanks!

jamesnowland said...

Would highly recommend checking out this book(http://www.abookapart.com/products/responsive-web-design) if you want a more in-depth understanding of responsive design and also the mindset behind it.

Anat Kumar said...

A great post. This post will help to design and promote to both designer as well promoter.

PhotographyByIan said...

My site has over 700 static pages not including the several hundred wordpress blog content. For many of our key words we are very high ranking. We also have a separate (smaller) mobile friendly site that we have blocked from the search engines through robot.txt file, so that we do not get seen as having duplicate content.
If we switch the entire site to a more mobile friendly version, what effects will this have on our rankings? Will we loose rankings because the site changes from pages that end in .htm and .html to .php or whatever?
Making major changes to a site that is ranking well is a scary proposition.

Yesi said...

I didn't know this technique had a name, thanks for the post and techniques. As the lead designer at Eyeona.com, I've been employing this technique with out site to have one HTML base that renders the site properly both on desktop and mobile browsers. Personally, I've preferred overriding a few style's when the mobile user agent is detected. Check our site in desktop and your mobile device to see our results. Eyeona.com

mohinish said...

Thanks a ton. I've included this thing into my site's CSS. n it's working great. thanku :)

ajithkumarcc said...

Really useful, for the all the new sites of mine (wordpress based) i buy responsive themes, but your tips looks very useful and I am going to implement them in my old sites as well.

Thanks for the useful post. I can now get over the panda update and focus on building responsive themes

Sathyam said...

Thanks for you valuable information . Keep Sharing Guys !!!!!!!!!!

Premium WordPress Themes said...

thanks for the useful things.....

CyberBytes Design said...

It won't affect any of that.. All you do is change some of the classes in the coding and mainly just the .CSS.. You don't have to change any of the file extensions or anything.. Definately look more into it because you seem to be understanding it pretty wrong.. All it does is mess around with the column widths having max and min widths and width 100% etc.. You don't mess with your file extensions at all.

Hope this helps you understand a bit, it should have absolutely NO bearing on your search results/rankings.

Åke Järvklo said...

Brilliant, but how come you can't use "pinching" to zoom text sizes on this ( most/all ?) responsive designs when you need to?
Isn't that an elephant in the room so to speak if this "brand new and shiny" actually breaks basic accessibility (on Safari for IPhone and IPad in my case)?

Åke Järvklo said...

My bad, this site inhibits text zooming only on IPhone...

Lup said...
This comment has been removed by the author.
Cheok Lup said...

I personally recommend web development frameworks like Twitter Bootstrap or Foundation by ZURB. Their pre-defined CSS caters to responsive design.

Freq said...

It's a very useful info about responsible design implementation. Will use some tips on my next project...

Tom said...

The Google example page..
Not working in IE6-7.
Alvays convenient just to code and forget.

Unknown said...

I'm all about responsive design, and all my sites are responsive design now. But you guys need to make ADSENSE compatible with responsive design if you want to promote it. Fixed width ads don't work.

Legend axis said...

oh admin !! You've just save my work.
Thanks for your post.

Louise Manfe said...

Is there anything in particular that one needs to know or to do special to properly submit a responsive design site to Google Mobile? My concern is having a separate sitemap for the mobile directory that would be exactly the same for the standard search directory. I do not want to get penalized for a duplicate sitemap.

Sanjeev Sharma said...

I wrote a post providing a basic introduction to responsive design (for noobs): Responsive Design – an introduction: http://bit.ly/L05U9a

Dan Jones said...

Too bad your Blogger template doesn't follow this model.

Sergio Rus said...

Actually, Google is not following the third guideline ("We should never show a horizontal scrollbar, whatever the window size") in lots of its new clean and shiny websites.

My monitor is set to 1024x768 resolution and I see the horizontal bar with all the following sites:

https://developers.google.com/mobile/
https://developers.google.com/games/
https://developers.google.com/chrome/
https://developers.google.com/+/
https://developers.google.com/google-apps/
https://developers.google.com/tv/
https://developers.google.com/speed/
https://developers.google.com/startups/

In fact, 1024x768 is still one of the most common resolutions, as you can see on StatCounter:

http://gs.statcounter.com/#resolution-ww-monthly-201105-201205

So, we are still lots of people who can not properly see all those google pages!
Although, this is not the only reason to make a good responsive design.

Hope you fix it, Google.

Cheers!

ammarisawake said...

Just sharing, here's an example of responsive web design that allows the mini icon at the top right of the page to appear upon resizing the browser or when using any mobile devices. The work is done by using Adobe CS6 utilizing CSS 3 and HTML 5.

Respnsive demo page: http://www.geckostudio.com.my/responsive/showcase.php

FIJINET said...

Excellent post Jessica. Indeed responsive design is a must these days going forward as mobile and tablet coverage will soon overtake old school desktops. This was an excellent tutorial! Thank you

Canis Canning said...

I think counting pixels is the wrong method. Think especially of the high resolution screens coming in now. All media queries should ask for the size in centimeters instead of pixels.

For the images server side scripts including caches could be used to resize them.

I all realized it on the little hobby page http://mm.ymir.de . Just try to slowly make the browser window narrow and watch the magic.

Raj Veer said...

Thanks a lot for this awesome sharing. Your list is informative and this will definitely be helpful for other people who are searching such information. Thanks again.

Upeksha Wisidagama said...

Actually iOS and Android simulators are really useful when debugging responsive layouts. Thanks for sharing this.

djdaniel150 said...

Media Queries are great ;) I've been using them on my new site, and they work wonderfully. You make any element scale to different sizes depending on the resolution of the device, browser window, aspect ratio, etc. And it works! My site scales perfectly to both a netbook and 32 inch LCD monitor. Best technology ever, and makes our jobs as web designers alot easier.

Kelson said...

@Canis Canning, this is already handled in the CSS spec. A CSS "pixel" isn't necessarily the same size as a device pixel. Previously it was treated as 1 pixel on a 96 dpi device. Onscreen, it would be an actual pixel, but when you printed at 600dpi, that 1px would end up being closer to 6 pixels across on the printer.

Now, with high-definition displays common, CSS3 actually defines 1px as 1/96 of an inch.

http://www.w3.org/TR/css3-values/#absolute-lengths

Brian Mohr said...

I work for a startup and we're using RWD. We use media queries to dispense the correct css per page layout. My question is: We have about 10-12 pages that have different layouts. One layout may have a ".5-column" class with a width of say 300px and another page layout may have a ".5-column" class with a different width. What's the best way to handle this so I don't have to create a different styleshhet for each page layout?

Brian Mohr said...

Has Google been able to put a monetary benefit for RWD sites as opposed to native apps?

Jonathan Zacharias said...

This is very helpful for creating websites that look good in all browser sizes and all browser types.