Screen orientation
The ScreenOrientation interface of the Screen Orientation API provides information about the current orientation of the document.
A ScreenOrientation instance object can be retrieved using the screen.orientation property.
EventTarget ScreenOrientation
Browser support
| Feature | Desktop | Mobile | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
| 38 | 79 | 43 | 16.4 | 38 | 16.4 | |
| The orientation read-only property of the Screen interface returns the current orientation of the screen. | 38 | 79 | 43 | 16.4 | 39 | 16.4 |
| The angle read-only property of the ScreenOrientation interface returns the document's current orientation angle. | 38 | 79 | 43 | 16.4 | 38 | 16.4 |
| The change event of the ScreenOrientation interface fires when the orientation of the screen has changed, for example when a user rotates their mobile phone. | 38 | 79 | 43 | 16.4 | 38 | 16.4 |
| The type read-only property of the ScreenOrientation interface returns the document's current orientation type, one of portrait-primary, portrait-secondary, landscape-primary, or landscape-secondary. | 38 | 79 | 43 | 16.4 | 38 | 16.4 |
- Available with a vendor prefix: ms (12)
- Edge does not return an `Orientation` object; instead, it returns the orientation type as a string.
- Available with a vendor prefix: moz (14)
Syntax
screen.orientation.addEventListener("change", (event) => {
const type = event.target.type;
const angle = event.target.angle;
console.log(`ScreenOrientation change: ${type}, ${angle} degrees.`);
}); Use cases
-
Using Screen orientation
The ScreenOrientation interface of the Screen Orientation API provides information about the current orientation of the document.
Cautions
- May not be supported in older browsers.
Implementation notes
- Some APIs require a secure context (HTTPS) or user activation. Check the requirements before use.