Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to lock viewport to Portrait Orientation in HTML5/CSS3

Is it possible to lock the view port's orientation to portrait on a mobile device?

I Googled it but could not find out exactly how to do it.

like image 628
H. Ferrence Avatar asked Jul 13 '16 19:07

H. Ferrence


People also ask

How do I test the orientation of the viewport in CSS?

The orientation CSS media feature can be used to test the orientation of the viewport (or the page box, for paged media ). Note: This feature does not correspond to device orientation.

What is the difference between viewport and viewport portrait?

portrait The viewport is in a portrait orientation, i.e., the height is greater than or equal to the width. landscape The viewport is in a landscape orientation, i.e., the width is greater than the height.

What is a viewport tag in CSS?

Definition of CSS Viewport CSS Viewport is defined as the visible area on a window screen which refers to the displays of the mobile devices. Adding CSS <meta> tag with viewport is an efficient way to improve the web pages to look on smaller screens. The ViewPort is not the same size as the original Webpage.

How do I set the viewport in HTML5?

Setting The Viewport. HTML5 introduced a method to let web designers take control over the viewport, through the <meta> tag. You should include the following <meta> viewport element in all your web pages: A <meta> viewport element gives the browser instructions on how to control the page's dimensions and scaling.


2 Answers

This trick should work:

    @media screen and (orientation: landscape) {
      html {
        /* Rotate the content container */
        transform: rotate(-90deg);
        transform-origin: left top;
        /* Set content width to viewport height */
        width: 100vh;
        /* Set content height to viewport width */
        height: 100vw;
        overflow-x: hidden;
        position: absolute;
        top: 100%;
        left: 0;
      }
    }
<h1>Test</h1>

If you need this to only be applied on mobile devices, you can do some javascript tricks to identify the device type, and if a mobile device, you add a class is-mobile-device or something, and specify html.is-mobile-device as the selector in the style definition. You may already use a framework that adds classes for different device types already, so you can specify those.

Note that this will not actually lock the orientation, just make the page look like it is locked.

like image 185
awe Avatar answered Nov 15 '22 18:11

awe


As my experience, it cannot be done as you are access the website from the browser. Which should have the lock orientation capability is the browser itself - e.g Safari, Chrome. Your HTML CSS code will not be feasible to control it.

For example when you are building a hybrid mobile app - meaning an application with html css and js then convert it to mobile app with wrapper as a web view inside. Then you are capable to lock the screen orientation. There are some configuration need to be done and later on these will be converted to Objective C or Java.

like image 43
trungk18 Avatar answered Nov 15 '22 19:11

trungk18