Skip to content

Instantly share code, notes, and snippets.

@wbamberg
Created April 28, 2012 22:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wbamberg/2522381 to your computer and use it in GitHub Desktop.
Save wbamberg/2522381 to your computer and use it in GitHub Desktop.

Developing for the mobile web

This page provides an overview of some of the main techniques needed to design web sites that work well on mobile devices.

We've organized it into two sections, designing for mobile devices and cross-browser compatibility.

Designing for mobile devices

Mobile devices have quite different hardware characteristics compared with desktop or laptop computers. Their screen are usually smaller, obviously, but they also usually automatically switch the screen orientation between portrait and landscape mode. They usually have touch screens for user input. APIs like geolocation or orientation are either not supported on desktops or are much less useful, and these give users new ways to interact with your site.

Working with small screens

Responsive Web Design is a term for a set of techniques that enables your web site to adapt its layout as its viewing environment - most obviously, the size and orientation of the screen - changes. It includes techniques such as:

  • fluid CSS layouts, to make the page adapt smoothly as the browser window size changes
  • the use of media queries to conditionally include CSS rules appropriate for the device screen width and height

The viewport meta tag instructs the browser to display your site at the appropriate scale for the user's device.

Working with touch screens

To use a touch screen you'll need to work with DOM Touch events. You won't be able to use the CSS :hover pseudo-class, and will need to design clickable items like buttons to respect the fact that fingers are fatter than mouse pointers. See this article on designing for touch screens.

You can use the -moz-touch-enabled media query to load different CSS on a touch-enabled device.

Optimizing images

To help users whose devices have low or expensive bandwidth you can optimize images by loading images appropriate to the device screen size and resolution. You do this in CSS by querying for screen height, width, and pixel ratio.

You can also make use of CSS properties to implement visual effects like gradients and shadows without images.

Mobile APIs

Finally, you can take advantage of the new possibilities offered by mobile devices, such as orientation and geolocation.

Cross-browser development

Write cross-browser code

To create web sites that will work acceptably across different mobile browsers:

  • try to avoid using browser-specific features, such as vendor-prefixed CSS properties
  • if you do need to use these features, check whether other browsers implement their own versions of these features, and target them too
  • for browsers that don't support these features, provide an acceptable fallback

For example, if you set a gradient as a background for some text using a vendor-prefixed property like -webkit-linear-gradient, it's best to include the other vendor-prefixed versions of the linear-gradient property. If you don't do that, at least make sure that the default background contrasts with the text: that way, the page will at least be usable in a browser which is not targeted by your linear-gradient rule.

See this list of Gecko-specific properties, and this list of WebKit-specific properties, and Peter Beverloo's table of vendor-specific properties.

Using tools like CSS Lint can help find problems like this in code, and preprocessors like SASS and LESS can help you to produce cross-browser code.

Test on multiple browsers

Test your web site on multiple browsers. This means testing on multiple platforms - at least the iPhone and Android.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment