JavaScript cannot reliably lock a browser tab or ordinary web page into portrait orientation. The web platform does provide a screen-orientation lock API, but it only works in limited situations, such as an installed app or a document already in fullscreen mode, and support varies by browser and operating system. If you need a page to stay portrait on phones, the dependable approaches are CSS and layout design that tolerate rotation, plus native app configuration when you control the app shell. The JavaScript lock is a best-effort enhancement, not a guarantee.

The distinction matters because “disable landscape mode in JavaScript” can mean several different things. You might be building a hybrid app with a WebView, a Progressive Web App, a fullscreen kiosk, or just a mobile website. Each has different capabilities. The practical method is to detect the platform, request a lock in the narrow window where browsers allow it, handle the promise rejection gracefully, and design the interface so landscape is not a broken state.

What the Screen Orientation API actually allows

The relevant interface is screen.orientation, part of the Screen Orientation API. Its lock() method accepts an orientation string and returns a promise. A call looks like this:

or

The "portrait" value permits either portrait-primary or portrait-secondary, depending on the device. The "portrait-primary" value is stricter and asks for the default upright portrait orientation. There are also landscape values and "natural".

Two conditions dominate whether this works. First, the browser must support the API. Chromium-based browsers on Android have historically supported orientation locking; Safari on iOS has not exposed screen.orientation.lock() to ordinary web content. Second, many browsers require the document to be in fullscreen mode before granting a lock. That fullscreen requirement is the big practical hurdle on a normal page: you cannot silently force fullscreen without a user gesture, and even then the browser may refuse.

The lock returned by lock() is a promise that can reject. Rejection is normal, not exceptional. Your code must catch it and fall back rather than throwing an unhandled error. The promise can also reject later if the document exits fullscreen or the browser revokes the lock, so in real code you treat the lock as temporary.

There's also screen.orientation.unlock(), which releases a lock, and screen.orientation.type plus a change event, which let you read the current orientation and react to rotation. Those reads work far more broadly than the lock, so they're useful for layout adjustments even where locking fails.

Prerequisites before you write any orientation code

Before attempting a lock, confirm four things, because each one changes the correct approach:

1: Delivery target. Is this an installed app, a fullscreen experience, or a normal website? A normal mobile website is the most restricted case. 2: Platform and browser. Android Chromium and iOS Safari behave differently. Test on the actual target, not just a desktop emulator. 3: Fullscreen availability. Browsers that require fullscreen need an element and a user gesture. Confirm the Fullscreen API is available and that your UI has a natural trigger, such as a “Start” or “Enter fullscreen” button. 4: Fallback design. Decide what the page should look like in landscape before you try to prevent it, because on many devices you can't prevent it at all.

If the answer to the first item is “a normal website,” set expectations: the lock is optional, and the layout must stand on its own.

Step-by-step: attempt a portrait lock with a safe fallback

This sequence works for the cases where a lock is possible, and fails gracefully everywhere else. The actions are ordered so that each step only runs when the previous one succeeded.

1: Feature-detect. Check typeof screen !== "undefined", that screen.orientation exists, and that typeof screen.orientation.lock === "function". Skip the attempt if any check fails, and go straight to your layout fallback.

2: Put the page in fullscreen if the browser requires it. Wrap a container element, call element.requestFullscreen() (or the prefixed variant if necessary) inside a click handler, and await it. Fullscreen requests must originate from a user gesture, so tie this to a button the user taps. If fullscreen is refused, the lock attempt is very likely to be refused too; stop and use the fallback.

3: Request the lock. Call screen.orientation.lock("portrait") and await the returned promise. Use the strict "portrait-primary" only if you truly need upright-only and can accept more refusals.

4: Handle success. On success, the device is now locked while the document stays fullscreen. Update your UI to reflect that the orientation is controlled, and optionally disable rotation-dependent controls.

5: Handle rejection. Catch the error, check the error name if it's informative (browsers may use distinct names for unsupported versus refused requests), and switch to the orientation-agnostic layout. Log the reason without treating it as a crash.

6: Listen for later changes. Subscribe to screen.orientation.change and to fullscreenchange. If the document leaves fullscreen, the lock is typically released; re-evaluate and fall back cleanly.

7: Release on teardown. When the user exits or you destroy the view, call screen.orientation.unlock() to avoid leaving a stale lock behind.

To check success, inspect whether screen.orientation.type reports a portrait value after the attempt, and confirm the promise resolved rather than threw. Because support changes across versions, the accurate check is a live test in your target browser rather than documentation, and behavior can shift with any update. Treat the lock as confirmed only when the promise resolves in that exact environment.

A specific stop condition: if your target is iOS Safari web content, lock() is generally unavailable, so stop chasing a JavaScript solution there. Move to CSS fallback and, if you ship an app, native configuration.

Reacting to rotation instead of preventing it

When locking isn't possible, the honest approach is to adapt. Read screen.orientation.type on load, and listen for the change event to update state:

Then apply a class to the document root, such as orientation-landscape, and let CSS do the heavy lifting. This pattern works across browsers much more reliably than lock(), because reading orientation is a widely supported feature even where locking is not.

A useful mental model is that the lock is an aspiration and the class-based adaptation is the actual guarantee. Build the guarantee first. Users who rotate their phone and get a sensible, usable layout will rarely notice the absence of a lock; users who rotate and see a broken or clipped interface will remember it.

A CSS-based portrait guard for web pages

If the goal is “please hold the phone upright,” CSS can encourage that without preventing rotation. The most common pattern is a media query that detects landscape by orientation and shows a friendly overlay:

The max-height condition narrows the rule to phones and small tablets rather than desktop monitors, which are almost always landscape and should not see a “please rotate” prompt. The overlay can darken the page and show a message and an icon. This does not disable landscape; it turns landscape into a deliberate, clearly communicated state. That's a defensible design when the app is genuinely portrait-oriented, such as a simple form, a reading view, or a scanner-style interface.

A stricter variation uses @media screen and (orientation: landscape) combined with a check on pointer type, such as (pointer: coarse), to target touch devices. Pointer queries don't map perfectly to the intent, but they reduce false positives on desktops.

There's a limit to this approach: overlays don't stop rotation, they just mask it. If the user needs to see content, they must rotate back. That's a trade-off you should acknowledge in the interface, not hide.

The WebView and hybrid-app case

If you're writing JavaScript inside a WebView that you control, the lock situation changes significantly, because the native app can set orientation at the operating-system level. Android apps can declare android:screenOrientation="portrait" on the activity or set it programmatically with setRequestedOrientation(). iOS apps can constrain orientation in the target's supported-interface-orientations settings. In both cases, the WebView inherits the app's behavior, and your JavaScript doesn't need to do the locking at all.

The practical rule: don't fight the platform in JavaScript when you own the shell. Set orientation natively, then let your web content assume portrait. This is the only approach that produces a true, reliable portrait-only experience on iOS. It also avoids the fullscreen dance, because the native layer handles it.

For cross-platform frameworks that wrap a WebView, the framework usually exposes an orientation or screen-orientation configuration; that setting, not your page script, is the correct place to control this. If the configuration isn't exposed, that's a sign the lock shouldn't be attempted from page JavaScript.

Progressive Web Apps and installed experiences

Installing a PWA from the browser gives you more control than a normal tab, and the manifest's orientation member lets you declare a preferred orientation, such as "portrait", "portrait-primary", or "any". When the app is launched as the installed experience, the browser or OS can honor that declaration. You can't rely on this in a regular browser tab, but the manifest is the declarative, non-script way to express intent. That's preferable to a runtime lock because it's declared up front rather than fighting the browser after load.

Note that manifest support and enforcement vary by platform, and installed PWAs still have platform-specific quirks. The manifest is the right signal, but you still need a layout that behaves when the declaration isn't honored.

So how should you actually decide?

Think in terms of three tiers of control, from most to least reliable.

Full control: you ship a native or hybrid app and set orientation at the OS level. This is the only tier that truly “disables” landscape, and JavaScript is not the tool that does it.

Declarative preference: an installed PWA with a manifest orientation, potentially combined with a lock attempt in environments that support it. Reasonably reliable, but not universal.

Best-effort enhancement: a normal website that attempts screen.orientation.lock(), catches failure, and adapts with CSS. Unreliable by design, and honest about it.

Match your choice to what you're actually building. The most common mistake is assuming the web can do what native apps do. The second most common is treating a rejected lock as a bug rather than the expected outcome.

Common failure modes and how to avoid them

A few specific problems explain most reports of “I tried to disable landscape and it didn't work.”

Attempting the lock without fullscreen. The promise rejects because the browser requires fullscreen. The fix is the fullscreen step, or acceptance of the fallback. Requesting fullscreen outside a user gesture. The browser refuses. Tie the request to a click handler. Ignoring promise rejection. An unhandled rejection produces console noise and can skip your fallback. Always catch. Assuming the lock survives navigation or exit from fullscreen. It usually doesn't; re-evaluate on fullscreenchange. Testing only on desktop emulation. Desktop “device mode” doesn't reproduce the real orientation and fullscreen policies of a phone. Test on the device. Forgetting the CSS fallback. A page that expects the lock will look broken when the lock fails, so build the layout first.

Each of these is reversible and cheap to check, which is why you start with them before concluding that the browser is misbehaving.

What about rotation and layout beyond locking

Even in a portrait-locked experience, keep the layout resilient. Safe-area insets, viewport units, and flexible containers matter whether the lock works or not. The moment a device reports landscape, whether because the lock failed or because a desktop browser can't lock, your content should remain readable.

A useful calculation illustrates the stakes. A mid-range phone at 412 × 915 CSS pixels portrait becomes approximately 915 × 412 in landscape. The vertical space available for a fixed header, content, and footer drops from 915 to 412—less than half. A layout with three stacked 200-pixel panels can't fit and will clip unless it can scroll or rearrange. That arithmetic is the real reason a lock fails to solve the problem: even where locking is unavailable, the layout has to function in the smaller dimension.

Use min-height: 100dvh and flexible stacks rather than assuming a tall viewport. Build with the narrower dimension in mind. Then landscape becomes a tolerable variant rather than an error.

A practical implementation plan

Putting it together, the plan for a page that “disables” landscape in JavaScript is:

1: Build the responsive layout first, including an orientation-aware overlay for small landscape screens. 2: Read orientation with screen.orientation and apply a class, so the layout adapts reliably. 3: Feature-detect lock(), attempt it only when fullscreen is available and the user has initiated it, and await the promise. 4: Catch and log rejections; never treat them as fatal. 5: Listen for fullscreenchange and orientationchange to re-evaluate. 6: If you control a native shell, set orientation there and let the page assume portrait. 7: If you ship a PWA, declare the orientation in the manifest.

That's the whole reliable pattern. There's no hidden trick beyond it, because the browser deliberately doesn't let arbitrary pages seize the device's orientation. That restriction exists for good reasons—accessibility, multi-window use, and user control—and the honest answer respects it.

The short version: JavaScript can request a portrait lock, but only in the narrow conditions browsers allow, and can never guarantee it on an ordinary web page. Treat the lock as a bonus. The portrait requirement itself is best enforced with CSS layout and, where you own the app shell, native settings.

FAQ

Does screen.orientation.lock() work on iOS Safari?

For normal web content it generally does not, which is why iOS requires an app-shell configuration or a layout fallback.

Can I lock orientation without fullscreen?

Some browsers may permit it in specific contexts, but fullscreen is a common precondition. Feature-detect and catch rejection rather than relying on either behavior.

Will a manifest orientation setting stop rotation in a browser tab?

It's a signal for installed experiences, not a hard rule for ordinary tabs. Support and enforcement vary by platform.

Is there a CSS way to disable landscape?

Not truly. CSS can detect orientation and display a rotate prompt, but it cannot prevent the device from rotating.