Mobile-specific HTML
Use the viewport tag to properly fit the content to the screen:
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
Use the tel scheme for telephone numbers, allowing users to initiate calls by clicking:
<a href="tel:18005555555">Call us at 1-800-555-5555</a>
Or use the sms scheme to initiate an SMS message:
<a href="sms:18005555555"> <a href="sms:18005555555,18005555556"> <!-- multiple recipients --> <a href="sms:18005555555?body=Text+goes+here"> <!-- predefined message body -->
You also have access to several Apple-specific tags to use in your web application (iPhone and iPod Touch):
// iOS 1.1.3+: this is the icon that's used when the user adds your app to the home screen <link rel="apple-touch-icon" href="/custom_icon.png"/> // iOS 3+: full-screen startup splash screen image <link rel="apple-touch-startup-image" href="/startup.png"> // enable full-screen mode <meta name="apple-mobile-web-app-capable" content="yes" /> // controls the appearance of the status bar in full-screen mode <meta name="apple-mobile-web-app-status-bar-style" content="black" />
And also some handy attributes to turn off annoying autocorrect features (works on iPhone, but anything else?):
<input autocorrect="off" autocomplete="off" autocapitalize="off">
Mobile-specific CSS
Media queries enable you to target specific features (screen width, orientation, resolution) within CSS itself. You can use them two ways: 1) inline in a CSS stylesheet or 2) as the “media” attribute in the link tag, which targets an external stylesheet. The following is an example of inline CSS that’s applied only when the device is in portrait mode:
@media all and (orientation: portrait) {
body { }
div { }
}
Here’s the same media query using the other method, which points to an external stylesheet (not recommended, as it creates another network request):
<link rel="stylesheet" media="all and (orientation: portrait)" href="portrait.css" />
Here’s a few examples:
// target mobile devices
@media only screen and (max-device-width: 480px) {
body { max-width: 100%; }
}
// recent Webkit-specific media query to target the iPhone 4's high-resolution Retina display
@media only screen and (-webkit-min-device-pixel-ratio: 2) {
// CSS goes here
}
// should technically achieve a similar result to the above query,
// targeting based on screen resolution (the iPhone 4 has 326 ppi/dpi)
@media only screen and (min-resolution: 300dpi) {
// CSS goes here
}
Read more: Media queries (Mozilla Developer Center)
(edit: removed section on body[orient='portrait'], which requires JavaScript and is in error. See Part 6: Dealing with device orientation for more details.)
Mobile-specific JavaScript
window.devicePixelRatio: determine screen resolution (analogue to the media query). (iPhone 4 has the value 2, while Nexus One has the value 1.5).
window.navigator.onLine (boolean): not strictly mobile, but helpful for apps to determine if they’re being run offline
window.navigator.standalone (boolean, iOS 2.1+): determine if it’s running in full-screen mode (Apple only?)
touch events (iOS, Android 2.2+): touchstart, touchmove, touchend, touchcancel
gesture events (Apple only, iOS 2+): gesturestart, gesturechange, gesturend give access to predefined gestures (rotation, scale, position)
window.orientation and orientationchange event: triggered every 90 degrees of rotation (portrait and landscape modes)
MozOrientation (Fennec/Firefox Mobile, Firefox 3.5+): also not strictly mobile. Gives access to the device’s accelerometer (x-y-z orientation data), updated periodically. Works on Android phones (I tested on the Nexus One), Windows Mobile, etc. On the desktop this works with laptops such as Thinkpads and MacBooks.
-near future: camera/microphone access in Android 2.3 (announced at Google I/O 2010) through the Capture API
If you’re developing for a BlackBerry Widget, you have access to proprietary information through the blackberry object (which gives access to useful information such as blackberry.network [returns values such as CDMA and Wi-Fi] and blackberry.system).
You also have the option to use PhoneGap, which augments JavaScript and gives you access to more phone features that native apps would have access to.
Use a mobile-optimized JavaScript library
Because smartphone browsers are standards-based, the aim of a JavaScript library on mobile is less towards API normalization and more towards providing an actual UI framework, usually to emulate the feel of native apps (and to provide easier workarounds to lack of access to position:fixed). We’ve seen a few libraries released that emulate the iPhone UI, and in the future we might see libraries emulating the Android UI, as well as entirely new UIs.
There’s also a bit to be said about simply loading full desktop JavaScript libraries into mobile clients. In my opinion this doesn’t particularly make sense, especially in a world where latency and bandwidth are so much more of a concern. It doesn’t make sense to force the user wait longer and download code that’s ultimately useless to them (hacks for desktop browsers such as IE 6, etc).
Here’s a few of the mobile libraries now available:
-Sencha Touch
-baseJS
-XUI
-jQTouch: jQuery-style mobile library that emulates the look of the iPhone UI
-iUI: the first JavaScript library to be released, emulates the look of the iPhone UI. Originally coded by Joe Hewitt.
-PastryKit: Apple proprietary non-public JavaScript library with no available documentation
Take advantage of new stuff!
While not specific to mobile, there’s a lot of new stuff in general that you can use. If you limit yourself to the top smartphones (iPhone, Android, and maybe webOS), compared to the desktop you immediately have access to an even wider array of new stuff, especially many Webkit proprietary features, since most of these top smartphones have browsers based on Webkit.
-HTML: new tags (HTML5 (I’m sure you’ve heard of it by now…))
-CSS: 2d transforms, 3d transforms, animation, border radius, custom fonts with @font-face, etc.
-JavaScript: strict mode, constants, block scope, Date.now(), etc.
Related posts
This post is part of a series on the mobile web. You can read the other parts of the series here:
Part 1: The viewport metatag
Part 2: The mobile developer’s toolkit
Part 3: Designing buttons that don’t suck
Part 4: On designing a mobile webpage