⚠️ Warning: this is an old article and may include information that’s out of date. ⚠️
(updated June 27, 2011)
Mobile-specific HTML
Viewport tag
Use the viewport tag to properly fit the content to the screen:
|
|
Tel scheme (to initiate phone calls)
|
|
Sms scheme (to initiate text messages)
|
|
Disable automatic telephone number linking
|
|
iOS-specific HTML (some work on Android as well)
You also have access to several Apple-specific tags to use in your iOS applications (iPhone, iPad, and don’t forget the iPod Touch!).
|
|
Turn off autocorrect, autocomplete, and autocapitalize
And also some handy attributes to turn off annoying autocorrect features:
|
|
Mobile-specific CSS
position:fixed and overflow:scroll
Mobile browsers are now starting to support these basic CSS properties better. Position:fixed will work on Android 2.2+ and iOS 5+. Overflow:scroll works with one finger on iOS 5+.
Also, iOS 5 has additional CSS to give the native scrollbar and momentum/intertia to elements with overflow:scroll:
|
|
Media queries
Media queries enable you to target specific features (screen width, orientation, resolution) within CSS itself. Media queries themselves are actually quite old and are not mobile specific (they used to be popular for making a print-friendly version of webpages).
You can use them two ways: 1) inline in a CSS stylesheet or 2) as the “media” attribute in the link tag, which loads an external stylesheet. The following is an example of inline CSS that’s applied only when the device is in portrait mode:
|
|
Here’s the same media query using the other method, which points to an external stylesheet (not recommended):
|
|
This is not recommended because it creates an additional HTTP request (bad for performance). Also, in the case of screen orientation, the separate CSS stylesheet is NOT downloaded when the screen is rotated.
Here’s a few examples of using inline CSS:
|
|
Read more: Media queries (Mozilla Developer Center)
Miscellaneous CSS
-
-webkit-tap-highlight-color
(iOS): override the semitransparent color overlay when a user clicks a link or clickable element. To completely disable it, set the value to ’transparent’ or ‘rgba(0,0,0,0)’ -
-webkit-user-select: none;
- prevent the user from selecting text (also works on desktop WebKit) -
-webkit-touch-callout: none;
- prevent the callout toolbar from appearing when a user touches and holds an element such as an anchor tag.
Mobile-specific JavaScript
window.scrollTo(0,0);
This hides the address bar and takes advantage of the entire device screen. You’ll have to set this in a timeout and make sure to get the timing right. See Remy Sharp’s post for more details.
window.matchMedia()
(iOS 5+) Again, just as CSS media queries aren’t specific to mobile, they do come in especially useful for mobile, so it’s worth mentioning their JavaScript counterpart. window.matchMedia() is a JavaScript-based version of media queries. You can use respond.js as a polyfill for devices that don’t support this functionality natively.
navigator.connection
(Android 2.2+) Determine if the phone is running on WiFi, 3G, etc. Example:
|
|
window.devicePixelRatio
Determine screen resolution (analogue to the CSS media query). (iPhone 4 has the value 2, while Nexus One has the value 1.5).
window.navigator.onLine
Not strictly mobile, but helpful for apps to determine if they’re being run offline.
window.navigator.standalone
(iOS 2.1+): determine if it’s running in full-screen mode
Touch and gesture events
-
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)
Screen orientation (every 90 degrees)
- orientationchange event: triggered every 90 degrees of rotation (portrait and landscape modes). The current orientation is available through window.orientation
Device orientation (more fine-grained)
The deviceorientation event will fire very frequently, and gives more fine-grained information about the device’s orientation in three dimensions.
MozOrientation (or onmozorientation?) (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 running Mobile Firefox. On the desktop this works with laptops such as Thinkpads and MacBooks.
devicemotion (shake gestures, etc.)
- devicemotion fires when the user shakes or moves their device. Devicemotion taps into the accelerometer, which is fires off when the device accelerates. Contrast this with the deviceorientation event, which taps into the device’s gyroscope (if it has one), which only measures the 3D angle orientation, even when the device is at rest.
Media capture API
While iOS is still lacking basic file inputs, Android is forging ahead, giving developers fine-grained control over content users can upload.
|
|
BlackBerry specific
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
I’ve created a separate entry for the available mobile libraries and frameworks.
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).
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.
Slides
More from the Mobile Web series:
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
Part 5: Using mobile-specific HTML, CSS, and JavaScript
Part 6: Dealing with device orientation
Part 7: Mobile JavaScript libraries and frameworks
Comments