Detecting device orientation

Introduced in Gecko 1.9.1

(Firefox 3.5 / Thunderbird 3.0 / SeaMonkey 2.0)

Increasingly, web-enabled devices are capable of determining their orientation; that is, they can report data indicating changes to their orientation with relation to the pull of gravity. In particular, hand-held devices such as mobile phones can use this information to automatically rotate the display to remain upright, presenting a wide-screen view of the web content when the device is rotated so that its width is greater than its height.

There are three ways to deal with orientation information in Gecko. The first is the orientation media query. This lets content adjust its layout using CSS, based on whether the device is in landscape mode (that is, its width is greater than its height) or portrait mode (its height is greater than its width).

The second way to handle orientation information is the DeviceOrientationEvent which was added in Firefox 6.  This event is sent when the accelerometer detects a change to the orientation of the device. By receiving and processing the data reported by these orientation events, it's possible to interactively respond to rotation and elevation changes caused by the user moving the device. Previous to this implementation was the experimental MozOrientation event added in Gecko 1.9.2 (Firefox 3.6).

The third way is to listen for the DeviceMotionEvent. This event is sent when a change in acceleration was added. It is different from the DeviceOrientationEvent because it is listening for changes in acceleration as opposed to orientation. Sensors that are commonly capable of detecting DeviceMotionEvent include sensors in laptops to protect moving storage devices. DeviceOrientationEvent are more commonly found in mobile devices.

Adjusting layout when the orientation changes

One of the most common cases in which you'll be interested in orientation changes is when you want to revise the layout of your content based on the orientation of the device. For example, perhaps you want a button bar to stretch along the longest dimension of the device's display. By using a media query, you can do this easily and automatically.

Build the CSS for each orientation

For this use case, you'll need two stylesheets: one to be used when the device is in portrait orientation (its height is greater than its width), and one to be used when the device is in landscape orientation (its width is greater than its height).

Portrait Landscape

#toolbar {
  width: 100%;

#toolbar {
  min-height: 500px;
  width: 125px;
  margin-right: 8px;
  float: left;

As you can see if you study the two CSS fragments above, when in portrait mode, we make the toolbar stretch horizontally across the top of the window, in order to maximize the horizontal space available for content. In landscape mode, the toolbar stretches vertically down the left side of the document.

Applying the correct CSS based on the current orientation

All you have to do now is establish the media queries that will automatically select the correct CSS file based on the orientation of the device. This is simple:

<link rel="stylesheet" media="all and (orientation:portrait)" href="portrait.css">
<link rel="stylesheet" media="all and (orientation:landscape)" href="landscape.css">

From this point on, loading the content results in the correct layout being applied based on the device's orientation.

Processing orientation events

Introduced in Gecko 6.0

(Firefox 6.0 / Thunderbird 6.0 / SeaMonkey 2.3)

Firefox 6 removed the MozOrientation event when adding support for the DeviceOrientationEvent , which you can listen for in order to receive updates as the user adjusts the orientation of the device.

All you need to do in order to begin receiving orientation events is this:

window.addEventListener("deviceorientation", handleOrientation, true);

After registering your event listener (in this case, a JavaScript function called handleOrientation()), your listener function periodically gets called with updated orientation data.

The orientation event contains four values:





The event handler function can look something like this:

function handleOrientation(orientData) {
  var absolute = orientData.absolute;
  var alpha = orientData.alpha;
  var beta = orientData.beta;
  var gamma = orientData.gamma;

  // Do stuff with the new orientation data

Accelerometer values explained

The value reported for each axis indicates the amount of rotation around a given axis in reference to a standard coordinate frame.  These are described in greater detail in the Orientation and motion data explained article which is summarized below. 

The DeviceOrientationEvent.alpha value represents the motion of the device around the z axis, represented in degrees with values ranging from 0 to 360.

The DeviceOrientationEvent.beta value represents the motion of the device around the x axis, represented in degrees with values ranging from -180 to 180.  This represents a front to back motion of the device.

The DeviceOrientationEvent.gamma value represents the motion of the device around the y axis, represented in degrees with values ranging from -90 to 90. This represents a left to right motion of the device.

Browser Compatibility

Compatibility for DeviceOrientationEvent:

  • Desktop
  • Mobile

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)

Basic support



? ? ?
Feature Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support



-- --


Firefox 3.6, 4, and 5 supported mozOrientation versus the specified DeviceOrientation event

Tags (5)

Edit tags

Attachments (0)


Attach file