Orientation Lock

@media screen and (min-width: 320px) and (max-width: 767px) and (orientation: landscape) {
  html {
    transform: rotate(-90deg);
    transform-origin: left top;
    width: 100vh;
    overflow-x: hidden;
    position: absolute;
    top: 100%;
    left: 0;
  }
}

Sent in by Andreas Gotfredsen.

Comments

  1. User Avatar
    rgwatkins
    Permalink to comment#

    I was just playing around with this but haven’t had total success. The vw and vh units still seem to refer to the original portrait orientation, so any layout done with these units breaks. Ideally, we would be able swap vw and vh when the page is rotated, but I couldn’t find any way to do that. One workaround would be to define –vw and –vh variables and set them appropriately for each orientation, but then we are stuck with the clumsy calc()/var() syntax when using them.

  2. User Avatar
    Binyamin

    Chris, we have for it now Screen Orientation API, see https://w3c.github.io/screen-orientation/

    screen.orientation.lock('portrait')
    screen.orientation.lock('landscape')
    screen.orientation.unlock()

Submit a Comment

Posting Code

You may write comments in Markdown. This makes code easy to post, as you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences (```) with double new lines before and after.

Code of Conduct

Absolutely anyone is welcome to submit a comment here. But not all comments will be posted. Think of it like writing a letter to the editor. All submitted comments will be read, but not all published. Published comments will be on-topic, helpful, and further the discussion or debate.

Want to tell us something privately?

Feel free to use our contact form. That's a great place to let us know about typos or anything off-topic.

icon-anchoricon-closeicon-emailicon-linkicon-logo-staricon-menuicon-nav-guideicon-searchicon-staricon-tag