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.
| Member | Kind | What it provides |
|---|---|---|
| type | Read-only property | portrait-primary, portrait-secondary, landscape-primary or landscape-secondary |
| angle | Read-only property | Rotation in degrees: 0, 90, 180 or 270 |
| lock() | Method | Promise that resolves when the requested orientation is applied |
| unlock() | Method | Releases any lock and restores the default behavior |
| change event | Event | Fires on the object after each orientation change |
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 value | Allowed orientations | Typical use |
|---|---|---|
| portrait | Both portrait types | Reading apps, vertical video |
| landscape | Both landscape types | Games, full-screen video players |
| portrait-primary | Upright portrait only | Barcode or document scanners |
| landscape-primary | One landscape direction only | Racing or tilt-controlled games |
| natural | Natural orientation of the device | Kiosk and signage displays |
| any | All four types | Releasing a strict lock while staying in control |
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 name | Usual cause | Recommended response |
|---|---|---|
| NotSupportedError | Browser or device does not allow locking | Fall back to a rotate-device hint |
| SecurityError | Document not fully active or sandboxed | Add allow-orientation-lock to the iframe sandbox |
| AbortError | A newer lock call replaced the pending one | Ignore it or await only the latest call |
| InvalidStateError | Page not in fullscreen where it is required | Request fullscreen first, then lock |
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.
How it compares with related Mobile web features
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.
| Mechanism | What it measures or controls | Best suited for |
|---|---|---|
| Screen Orientation API | Rendered screen type, angle and lock | Game logic, video players, lock requests |
| CSS orientation media query | Viewport aspect ratio | Responsive layout changes |
| Device orientation events | Physical tilt in three axes | Compass, augmented reality, tilt controls |
| Manifest orientation member | Default orientation of an installed app | PWA launch behavior |
| window.orientation | Legacy angle value | Nothing new; deprecated |
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.orientationand itslockmethod 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.