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.
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With