Screen Orientation API

On this page 7

The Screen Orientation API is a browser interface that lets a web page read the current orientation of the screen, listen for rotation changes and, under specific conditions, lock the display to portrait or landscape. In the Mobile web, it gives browser-based games, video players, camera tools and installed progressive web apps a level of control over rotation that was once reserved for native applications on Mobile platforms.

The interface is defined by the W3C Screen Orientation specification and is exposed through the screen.orientation object. It replaces the older, nonstandard window.orientation property and the orientationchange event, which still exist in some engines but are deprecated and should not be used in new code.

How the Screen Orientation API works

The API works by exposing a single ScreenOrientation object that describes how the rendered screen is currently positioned relative to its natural orientation. The natural orientation is the one the manufacturer designed as the default: portrait for almost every phone, landscape for most tablets in keyboard docks and for laptops.

When the user rotates the device, the operating system rotates the rendered screen, the browser updates the object and fires a change event. Scripts read two properties to learn the new state: type, which names one of four orientation types, and angle, which gives the rotation in degrees from the natural orientation. Two methods, lock() and unlock(), request or release a fixed orientation.

The following table summarizes the members of the interface and what each one returns or does.

MemberKindWhat it provides
typeRead-only propertyportrait-primary, portrait-secondary, landscape-primary or landscape-secondary
angleRead-only propertyRotation in degrees: 0, 90, 180 or 270
lock()MethodPromise that resolves when the requested orientation is applied
unlock()MethodReleases any lock and restores the default behavior
change eventEventFires on the object after each orientation change
Members of the ScreenOrientation interface

A key detail is that angle is not a sensor reading. It reflects how the screen content is rotated, which changes in steps of 90 degrees, and it stays fixed when the user turns the phone while system rotation is disabled. Continuous physical tilt belongs to Device orientation events, a separate API based on the gyroscope and accelerometer.

Orientation types and lock values

Orientation types describe the four possible screen positions, and lock values describe which of those positions a page is willing to accept. A primary type is the main variant of that shape, while a secondary type is the same shape turned 180 degrees, such as a phone held upside down.

The mapping between type and angle depends on the device, because a tablet whose natural orientation is landscape reports landscape-primary at 0 degrees, while a typical phone reports portrait-primary at 0 degrees. Code should therefore test the type string rather than infer the shape from the angle alone.

The table below lists the values accepted by lock() and the orientations each one allows.

Lock valueAllowed orientationsTypical use
portraitBoth portrait typesReading apps, vertical video
landscapeBoth landscape typesGames, full-screen video players
portrait-primaryUpright portrait onlyBarcode or document scanners
landscape-primaryOne landscape direction onlyRacing or tilt-controlled games
naturalNatural orientation of the deviceKiosk and signage displays
anyAll four typesReleasing a strict lock while staying in control
Values accepted by the lock method

Conditions for locking the orientation

Locking is heavily restricted, because an arbitrary page that froze the display would harm usability. The specification lets each browser decide the preconditions, and in practice the most common rule is that the document must be in fullscreen mode or running as an installed web app.

Chromium-based browsers on Android accept a lock after the page has called the Fullscreen API, or when the app was launched from the home screen with a web app manifest whose display mode is fullscreen or standalone. Safari on iOS and iPadOS supports reading the orientation and the change event, but it does not implement lock(), and desktop browsers generally reject the call.

When a lock cannot be applied, the promise rejects with a named exception. The next table maps the usual errors to their causes and the recommended response.

Error nameUsual causeRecommended response
NotSupportedErrorBrowser or device does not allow lockingFall back to a rotate-device hint
SecurityErrorDocument not fully active or sandboxedAdd allow-orientation-lock to the iframe sandbox
AbortErrorA newer lock call replaced the pending oneIgnore it or await only the latest call
InvalidStateErrorPage not in fullscreen where it is requiredRequest fullscreen first, then lock
Common lock failures and how to handle them

Embedded content needs the allow-orientation-lock sandbox token, and every lock is released automatically when the page leaves fullscreen or the document is unloaded. Robust code always treats a lock as a best-effort request, never as a guarantee.

The Screen Orientation API is one of several tools that respond to rotation, and choosing the right one avoids unnecessary JavaScript. Layout changes are usually better handled in CSS, while the API is suited to behavior that cannot be expressed declaratively.

The following comparison shows where each related mechanism fits.

MechanismWhat it measures or controlsBest suited for
Screen Orientation APIRendered screen type, angle and lockGame logic, video players, lock requests
CSS orientation media queryViewport aspect ratioResponsive layout changes
Device orientation eventsPhysical tilt in three axesCompass, augmented reality, tilt controls
Manifest orientation memberDefault orientation of an installed appPWA launch behavior
window.orientationLegacy angle valueNothing new; deprecated
Rotation-related mechanisms compared

One common misconception is that the CSS orientation query reports device orientation. It compares viewport width and height, so a desktop window narrower than it is tall matches portrait, and an on-screen keyboard can flip the result on some phones.

Practical implementation and common mistakes

A sound implementation follows a progressive enhancement pattern: detect support, request fullscreen in response to a user gesture, attempt the lock, and keep a usable layout if any step fails. Because fullscreen requires transient user activation, the whole sequence normally starts inside a click or tap handler.

  • Check that screen.orientation and its lock method exist before calling them.
  • Call the lock only after the fullscreen promise has resolved.
  • Wrap the call in error handling and show a rotate-device message as the fallback.
  • Listen to the change event on screen.orientation, not to window resize alone.
  • Call unlock() when the user exits a game or video so that normal rotation returns.
  • Test on real devices, because emulators often misreport angle values.

Frequent mistakes include calling lock() on page load, which fails without fullscreen, assuming iOS will honor the lock, and hard-coding angle 90 as landscape, which breaks on tablets with a landscape natural orientation. Accessibility also matters: guidance such as WCAG 2.1 success criterion 1.3.4 asks that content not be restricted to one orientation unless that orientation is essential.

Stripped to the essentials

Phones turn their screens sideways when you rotate them. The Screen Orientation API is the way a website asks the browser which way the screen is facing right now and gets told whenever that changes.

It can also ask the phone to stay in one position, the way a game might insist on landscape. Browsers only allow this in limited situations, usually when the page fills the whole screen, and some phones refuse entirely. The key idea is simple: a website can always ask which way the screen faces, but it can only sometimes keep it from turning.

Where it is applied

A small studio builds a browser-based racing game that players open from a link on their Android phones. The track is drawn for a 16:9 landscape view, so the start screen shows a large Play button. When the player taps it, the game requests fullscreen and then calls screen.orientation.lock("landscape"), which keeps the view steady even when the player tilts the phone to steer.

On iPhones the lock call is not available, so the game listens to the change event instead. If the player holds the phone upright, a centered message asks them to rotate it, and the race pauses until screen.orientation.type reports a landscape value. When the player exits the race, the game calls unlock(). The result is one codebase that behaves well on both platforms, with fewer accidental rotations during play and no broken layouts for players whose browsers cannot lock.