How can I get the current screen orientation?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
On the web, the current screen orientation is usually either portrait or landscape, and modern browsers expose that information through the Screen Orientation API. In practice, you often combine that API with CSS media queries and a fallback based on viewport dimensions so the UI stays responsive across browsers.
Reading the current orientation in JavaScript
The most direct modern approach is screen.orientation.type. It reports values such as portrait-primary or landscape-primary.
If the browser supports the API, this gives you a precise orientation string rather than a guessed value.
Responding to orientation changes
A one-time check is useful, but most apps need to react when the user rotates the device:
That pattern is helpful for media players, dashboards, or layouts that need to show different controls in landscape mode.
A practical fallback with matchMedia
Browser support is uneven enough that a fallback is still worth having. A good option is the orientation media query:
This is often enough if you only care about portrait versus landscape and do not need the exact API-specific orientation type.
Styling the page with CSS
Orientation checks are not only for JavaScript. Many layout changes are better handled directly in CSS:
That keeps presentation logic in the stylesheet and avoids unnecessary JavaScript-driven layout changes.
Last-resort fallback with viewport dimensions
If you need a simple fallback where the Orientation API is unavailable, compare viewport width and height:
This is not as precise as the dedicated APIs, but it works in many cases and is better than having no fallback at all.
For application code, a layered approach works best: try screen.orientation, fall back to matchMedia, and only then use width-versus-height guessing. That keeps the code robust without overcomplicating it.
Common Pitfalls
The first pitfall is assuming orientation and viewport size always mean the same thing. On some devices, browser chrome, split-screen mode, and virtual keyboards can change the usable viewport in ways that make simple width-versus-height checks misleading.
Another issue is relying only on JavaScript for layout. If the real goal is to rearrange UI elements, CSS @media (orientation: ...) rules are usually simpler and more robust.
Be careful with event support as well. Some browsers support the media query but not the same event model for screen.orientation, so defensive coding with optional chaining or feature checks is a good idea.
Finally, test on real devices. Orientation behavior in a desktop emulator is helpful, but mobile browsers often differ in timing and viewport behavior during rotation.
If the page must preserve application state during a rotation, make sure the orientation handler is idempotent. A layout update should be safe to run multiple times without duplicating DOM nodes or re-registering listeners.
Summary
- Use
screen.orientation.typewhen you want the browser's current orientation value. - Listen for the
changeevent if the app needs to react when the device rotates. - Use
matchMedia("(orientation: portrait)")as a practical cross-browser fallback. - Prefer CSS media queries for layout changes and JavaScript for behavior changes.
- Fall back to comparing
window.innerWidthandwindow.innerHeightonly when the dedicated APIs are unavailable.
Related reading
- How can I get the font size and font name of a UILabel?
- How can I get the height and width of an uiimage?
- How can I get the iOS 7 default blue color programmatically?
- How can I import Swift code to Objective-C?
- How can I increase the Tap Area for UIButton?
- How can I launch Safari from an iPhone app?
- How can I launch the iOS Simulator from Terminal?
- How can I load storyboard programmatically from class?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.