Skip to content

Instantly share code, notes, and snippets.

@debborafernandess
Last active February 14, 2025 15:23
Show Gist options
  • Save debborafernandess/949c957c628883ef56d56bb011c6595c to your computer and use it in GitHub Desktop.
Save debborafernandess/949c957c628883ef56d56bb011c6595c to your computer and use it in GitHub Desktop.
Lock Orientation to Portrait Mode

Improve UX by locking orientation to Portrait Mode

The page should be locked to Portrait mode regardless of the device orientation, so users know this page is meant to be used in portrait mode. Actually, the payment informations, who can be read, edited or deleted, are hidden on small/medium mobile devices when its orientation is landscape, once that the footer is sticked and is displaied in front of it.

How-to

We had two options to try to achieve this goal:

  1. Actually lock screen orientation (Javascript) Via Javascript, get the screen orientation and call the lock() api.

  2. Stylize screen on landscape mode as portrait mode (CSS) With CSS, rotate the page on -90 degree, so the style that we have for portait will be applied to an rotated device.

Results

PR-46056 contains more information about the code who lead to this results.

#1. Actually lock screen orientation (Javascript)

Some devices or browsers, like chrome, don't allow this change due to user preference; so this solution won't solve our problem.

if (screen.orientation && screen.orientation.lock) {
    this.lockOrientation()
  } else {
    console.warn("Debug: Screen orientation lock is not supported by this browser.");
}
2. Stylize screen on landscape mode as portrait mode (CSS)

Here’s a refined version of your explanation that makes it clearer for both developers and the product owner:


Handling Landscape Mode with CSS Rotation

To ensure a consistent portrait experience, even when the device is in landscape mode, we are applying:

  • Using CSS rotation transform: rotate(-90deg) , we will be able to make the landscape mode looks like portrait mode. Besides, we will need some effort to adjust: to centering for different devices
  • Adjustments for different screen sizes to ensure proper centering across devices.
  • Sticky footer behavior to keep it anchored at the bottom, even after rotation.
  • Rotating modals and flyouts so they remain aligned with the transformed layout instead of following the default unrotated behavior. Without these adjustments, UI elements like modals and side menus would appear misaligned, impacting usability.

trim CAF7A933-29FB-46D9-B46B-B8AF929418EC

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment