HTML itself has no command that forces a phone or tablet to rotate, but it does provide a way to detect the device's current orientation and to declare the orientation a web app should use when it is installed and launched. In a normal browser tab, the page can detect orientation through CSS media queries and JavaScript and then rearrange itself accordingly, but the visitor retains control of the physical rotation. In an installed web app with a web app manifest, the developer can request a specific orientation such as portrait or landscape. Full lockdown of the screen—the kind a native game or video player imposes—is only available to fullscreen or installed contexts through the Screen Orientation API, and even then the browser and operating system can refuse.

Understanding which of those three layers you actually need is the key to the question. "Controlling" orientation can mean three quite different things: responding to it, requesting it for an installed app, or locking it at runtime. They use different tools and have different reliability.

The three layers of orientation control

Layer What it does Where it works Main tools
Detection and layout Reacts to the current orientation Every browser tab CSS media queries, window.matchMedia, screen.orientation
Manifest declaration Requests a preferred orientation for an installed web app Installed PWAs and some app-like contexts Web app manifest orientation member
Runtime lock Attempts to freeze the screen to a chosen orientation Fullscreen or installed contexts, with permission and support screen.orientation.lock()

Most developers searching for this topic need the first layer, sometimes the second, and rarely the third. The rest of this article takes them in order, with the caveats that make each one accurate.

Detecting orientation with CSS

CSS is the cleanest way to respond to orientation when the goal is purely presentational. The orientation media feature matches portrait or landscape:

The media feature is based on the viewport's width-to-height ratio, not on the physical device. A desktop browser window that is taller than it is wide reports portrait, even on a laptop. That behavior is usually desirable for responsive layout, but it means the term does not always mean "the user is holding a phone upright." For a wide desktop monitor, orientation: landscape matches simply because the viewport is wider than tall.

Two common mistakes are worth avoiding. First, don't use orientation as a proxy for screen size. A small landscape phone and a large landscape television both match the same query; pair orientation queries with width breakpoints when the layout depends on both. Second, don't target specific devices through orientation alone. The query tells you the shape of the viewport, nothing about hardware, input method, or platform.

CSS also has aspect-ratio media queries and container queries, which are often more precise than orientation for layout decisions, because they let you pick an actual ratio rather than a binary label.

Detecting orientation in JavaScript

When behavior—not just styling—must change, JavaScript offers two related interfaces.

The older approach is window.matchMedia('(orientation: portrait)'), which returns a MediaQueryList whose matches property you can read and whose change event you can listen to:

The newer approach is the Screen Orientation API, accessed as screen.orientation. Its type property returns values such as "portrait-primary", "portrait-secondary", "landscape-primary", and "landscape-secondary", and it fires a change event:

The distinction between primary and secondary matters. portrait-primary and landscape-primary refer to the device's default upright or sideways position; the secondary variants describe the 180-degree flipped positions. On phones that never flip upside down, secondary orientations may never occur. The angle value tells you the rotation in degrees and is useful for compensating image or canvas rendering.

The Screen Orientation API is widely supported in modern browsers, but screen.orientation may be undefined in very old environments. Feature-detect before using it, and fall back to matchMedia when it is missing. Note also that the change event fires on rotation and on viewport resize in ways that vary by browser, so code should read the current state rather than assume what triggered the event.

Declaring orientation in a web app manifest

If your goal is an installed web app—typically a Progressive Web App launched from the home screen—the web app manifest lets you declare a preferred orientation. The standard orientation member accepts values drawn from the Screen Orientation API, plus a few conveniences:

Common values include "portrait", "landscape", "portrait-primary", "landscape-primary", "any", and "natural". The portrait and landscape shorthands permit either the primary or secondary variant of that orientation, while the -primary forms are stricter.

This is a request, not a guarantee. The browser or operating system decides whether to honor it, and behavior differs across platforms, launchers, and whether the app is displayed in a standalone window. A manifest orientation is generally respected when the app is launched in its own window on mobile, and it is typically ignored in an ordinary browser tab. Desktop platforms often disregard orientation entirely because the concept does not map cleanly to a resizable window. Also remember that the manifest only takes effect after the app has been installed or added to the home screen; changing it does not affect an already-open tab.

The manifest is the right tool when you are building something like a kiosk-style form, a camera app, or a game whose design assumes one orientation. It is the wrong tool if you only want a page to look good in both orientations—use CSS for that.

Locking orientation at runtime with the Screen Orientation API

The Screen Orientation API includes a lock() method:

This attempts to freeze the screen in the requested orientation. It is the closest web equivalent to what a native app does, but its restrictions are substantial:

It generally requires fullscreen. Most browsers only permit locking while the document is in fullscreen mode, entered through a user gesture. You typically call element.requestFullscreen() first and then screen.orientation.lock(). It may require an installed app context. Some browsers allow lock only for installed PWAs, not ordinary tabs. The user and OS can override. If the device's rotation lock is on, or the platform does not support the requested orientation, the lock can fail or be silently ignored. Desktop support is limited. Many desktop browsers do not support orientation lock because desktop screens do not rotate. The promise rejects on failure. Wrap the call in try/catch and design the page so it still works if the lock never succeeds.

screen.orientation.unlock() releases a previous lock. There is also a screen.orientation.lock() companion event, change, which still fires when the orientation actually changes—for example, when the user exits fullscreen and the lock is released.

A realistic pattern looks like this:

The important design principle is that orientation lock should be an enhancement, never a hard dependency. If the lock fails, the page must remain usable.

Why browsers do not let a normal page force rotation

The restriction is deliberate. A page that could rotate a visitor's screen without permission would be disorienting and hostile, and it would break accessibility tools and multi-window workflows. Orientation is a device-level setting the user controls, much like volume or brightness. The web platform therefore grants orientation influence only in contexts where the user has signaled intent: installing an app, entering fullscreen, or both. Understanding this explains why tutorials that promise to "lock orientation in HTML" often fail—they are describing APIs that only work in narrow conditions.

A practical decision guide

Choose based on what you are actually building:

A responsive website. Use CSS @media (orientation: ...) and, where useful, matchMedia for behavior changes. Do not attempt to lock anything. A responsive website that needs a specific orientation for one feature, such as a camera capture screen or a wide data table. Detect orientation, show a gentle prompt suggesting the user rotate the device, and provide a usable fallback. Avoid forcing rotation; a full-screen prompt that blocks the page until rotation is a poor experience and can trap users whose device cannot rotate. An installed web app. Add orientation to the manifest and test on the target platforms, since support varies. A fullscreen game, media player, or kiosk experience. Request fullscreen from a user gesture, then attempt screen.orientation.lock(), with a graceful fallback when it is refused.

A compact comparison makes the tradeoffs clear:

Goal Best mechanism Reliability
Reflow layout for the current shape CSS orientation media query High; works everywhere
Run logic when orientation changes matchMedia or screen.orientation events High in modern browsers
Prefer an orientation for an installed app Manifest orientation member Medium; platform-dependent
Force the screen to rotate screen.orientation.lock() in fullscreen Low to medium; permission- and context-dependent

Common pitfalls and misconceptions

"I can set orientation with a meta tag." There is no HTML meta tag that locks orientation in a browser. Meta tags exist for viewport scaling, and some old platform-specific conventions existed for hybrid apps, but they are not a general web solution. The manifest is the standards-based way to express an orientation preference for an installed app.

"CSS orientation is about the device." It is about the viewport, as noted. On desktop, resizing a window across the square point flips the media query.

"Locking is permanent." A lock ends when the document leaves fullscreen, when the user rotates after a lock fails, or when the page navigates away. It is also not a security boundary; it is a best-effort request.

"Orientation lock works in an iframe freely." Embedded frames have additional restrictions, and fullscreen and orientation requests from cross-origin iframes are typically blocked unless explicitly permitted.

"I should block portrait users until they rotate." This pattern is tempting for landscape-only content, but it fails for users on devices that cannot rotate, users with accessibility needs, and desktop users with tall windows. Always provide a fallback layout, even if it is a simplified one.

"The screen.orientation.type values are all I need." On some platforms, particularly desktop, the type may remain landscape-primary regardless of window shape, because there is no physical rotation. Combine orientation events with resize and media queries rather than relying on a single signal.

Testing orientation behavior

Orientation bugs are easy to miss because developing on a desktop rarely reproduces them. Practical checks include resizing the browser window across the square ratio, using the device emulation tools in browser developer tools to simulate rotation, and testing on at least one physical phone and tablet. For manifest and lock behavior, test the installed app rather than the tab, because the two contexts behave differently. Also test with the device's rotation lock enabled, since that is a common real-world state that can defeat a lock request.

Bottom line

You control portrait and landscape in HTML in layers. Use CSS media queries and JavaScript detection to respond to whatever orientation the visitor has. Use the web app manifest to request a preferred orientation for an installed app. Use the Screen Orientation API's lock() method, in fullscreen or installed contexts, only when forcing a rotation is genuinely necessary—and always build a fallback for when the request is denied. The web intentionally reserves forced rotation for user-initiated, app-like contexts, so treating orientation as something to adapt to, rather than something to command, produces more reliable pages.