How to detect orientation change?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Detecting orientation changes matters when a layout, media experience, or input model should react differently in portrait and landscape modes. On the web, the modern approach is to use the Screen Orientation API or responsive CSS rather than the older orientationchange event.
Modern Web Detection with screen.orientation
The current browser-facing API is screen.orientation. You can read the current orientation type and listen for its change event.
This gives you more structured information than older APIs because you can inspect both type and angle.
Use CSS When the Goal Is Only Layout
If you only need to restyle the interface, JavaScript is often unnecessary. CSS media queries are simpler and more robust for pure presentation changes.
This is usually the best choice for responsive design because it keeps layout concerns in CSS instead of mixing them into application logic.
A Useful Fallback with matchMedia
When you want JavaScript behavior tied to orientation but also want a lightweight interface, matchMedia works well:
This is especially convenient when your logic already revolves around responsive breakpoints.
Avoid the Old orientationchange Event
You will still find examples using:
That older event is deprecated and non-standard. Modern browser guidance points developers toward screen.orientation and its change event instead.
If you maintain older code, it may still work in some environments, but it is not the best foundation for new development.
Handling Real UI Updates
In practice, orientation changes usually require more than a console log. Typical actions include:
- recalculating canvas dimensions
- reflowing a gallery or chart
- re-running a measurement-based layout function
- changing media controls or navigation placement
A realistic handler often reads measurements after orientation changes:
The important point is that orientation detection should lead to idempotent layout logic, not fragile one-off DOM mutations.
Mobile App Perspective
In native mobile development, orientation changes are usually handled through platform lifecycle or configuration APIs rather than a web event. The core idea is still the same: detect the state change, then rebuild or adjust the UI in a way that preserves user state.
Even if your project is not a browser app, the design lessons carry over:
- keep orientation handling separate from business logic
- preserve important user state
- prefer declarative layout systems when possible
Common Pitfalls
The first pitfall is using JavaScript for layout changes that CSS could handle more cleanly. If the only requirement is responsive styling, use media queries.
Another pitfall is relying on the deprecated orientationchange event for new code. It may still appear in tutorials, but it is not the recommended modern API.
A third pitfall is assuming orientation alone fully describes the UI context. Screen size, safe areas, keyboard presence, and viewport changes can matter just as much.
Finally, remember to test on real devices. Browser emulation is helpful, but orientation behavior often feels different on actual hardware.
Summary
- On the web, prefer
screen.orientationand itschangeevent - Use CSS media queries when orientation only affects styling
- '
matchMedia("(orientation: portrait)")is a useful JavaScript-friendly fallback' - Avoid building new code around the deprecated
orientationchangeevent - Treat orientation changes as part of responsive UI design, not as isolated device trivia
Related reading
- How to detect tableView cell touched or clicked in swift
- How to detect that animation has ended on UITableView beginUpdates/endUpdates?
- How to detect the end of loading of UITableView
- How to detect the end of UISlider drag?
- How to detect when a TextField loses the focus in SwiftUI for iOS?
- How to detect when a UIScrollView has finished scrolling
- How to detect when an Android app goes to the background and come back to the foreground
- How to detect when AVPlayer video ends playing?
.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.