How to Lock Viewport to Portrait Orientation in Html5/Css3

How to lock viewport to Portrait Orientation in HTML5/CSS3

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.

How do I lock the orientation to portrait mode in a iPhone Web Application?

You can specify CSS styles based on viewport orientation:
Target the browser with body[orient="landscape"] or body[orient="portrait"]

http://www.evotech.net/blog/2007/07/web-development-for-the-iphone/

However...

Apple's approach to this issue is to allow the developer to change the CSS based on the orientation change but not to prevent re-orientation completely. I found a similar question elsewhere:

http://ask.metafilter.com/99784/How-can-I-lock-iPhone-orientation-in-Mobile-Safari

Force “landscape” orientation mode

It is now possible with the HTML5 webapp manifest. See below.


Original answer:

You can't lock a website or a web application in a specific orientation. It goes against the natural behaviour of the device.

You can detect the device orientation with CSS3 media queries like this:

@media screen and (orientation:portrait) {
// CSS applied when the device is in portrait mode
}

@media screen and (orientation:landscape) {
// CSS applied when the device is in landscape mode
}

Or by binding a JavaScript orientation change event like this:

document.addEventListener("orientationchange", function(event){
switch(window.orientation)
{
case -90: case 90:
/* Device is in landscape mode */
break;
default:
/* Device is in portrait mode */
}
});

Update on November 12, 2014: It is now possible with the HTML5 webapp manifest.

As explained on html5rocks.com, you can now force the orientation mode using a manifest.json file.

You need to include those line into the json file:

{
"display": "standalone", /* Could be "fullscreen", "standalone", "minimal-ui", or "browser" */
"orientation": "landscape", /* Could be "landscape" or "portrait" */
...
}

And you need to include the manifest into your html file like this:

<link rel="manifest" href="manifest.json">

Not exactly sure what the support is on the webapp manifest for locking orientation mode, but Chrome is definitely there. Will update when I have the info.

Can a website force a device rotation lock?

In an update to an old ('12) question, I think this can help a lot of people!

I haven't figured out a true way of locking the device rotation, but came up with a perfect alternative, which I've seen a few people do too.

Option A. One simple alert

By use of a simple jQuery script, you can detect the orientation of your device.

if(window.innerHeight > window.innerWidth){
alert("Please use Landscape!");
}

Well, a simple alert is easy, but the notification can be quite nicer!

Option B. One nice image notification

(update as of 04-2018: (as I just saw my post again, I thought of something easier..) use media queries. Pretty much the same as below, but instead of using Javascript, use css, hide the element by default and show it when the orientation is landscape → @media (orientation: landscape) {...})

Simply add an fixed element to your page that is shown when the orientation has changed.

HTML

<div class="turnDeviceNotification"></div>

CSS

.turnDeviceNotification {
position:fixed;
top: 0;
left:0;
height:100%;
width:100%;
display: none;
}

You can update this element with text, or simply connect it to a background-image by

.turnDeviceNotification {
background-image:url('../images/turnDevice.jpg');
background-size:cover;
}

Simply add a nice background to your images folder, such as the one below.

Noticed the object has an display: none ? That's because else it'd be shown even in portrait mode. Now, all you need to do is to use the script below, so the object is shown only in landscape mode.

jQuery(window).bind('orientationchange', function(e) {
switch ( window.orientation ) {
case 0:
$('.turnDeviceNotification').css('display', 'none');
// The device is in portrait mode now
break;

case 180:
$('.turnDeviceNotification').css('display', 'none');
// The device is in portrait mode now
break;

case 90:
// The device is in landscape now
$('.turnDeviceNotification').css('display', 'block');
break;

case -90:
// The device is in landscape now
$('.turnDeviceNotification').css('display', 'block');
break;
}
});

This will show the notification only when the device orientation has changed to landscape.
Sample image notification

Prevent landscape orientation Mobile-website

You need to add the following meta tag to your page header like so:

<meta http-equiv="ScreenOrientation" content="autoRotate:disabled">

Or you can have separate CSS files for landscape and portrait mode like so:

<link rel="stylesheet" type="text/css" href="css/landscape.css" media="screen and (orientation: landscape)">
<link rel="stylesheet" type="text/css" href="css/portrait.css" media="screen and (orientation: portrait)">

Detect viewport orientation, if orientation is Portrait display alert message advising user of instructions

if(window.innerHeight > window.innerWidth){
alert("Please use Landscape!");
}

jQuery Mobile has an event that handles the change of this property... if you want to warn if someone rotates later - orientationchange

Also, after some googling, check out window.orientation (which is I believe measured in degrees...)

EDIT: On mobile devices, if you open a keyboard then the above may fail, so can use screen.availHeight and screen.availWidth, which gives proper height and width even after the keyboard is opened.

if(screen.availHeight > screen.availWidth){
alert("Please use Landscape!");
}

How can I lock auto rotate on the website for the mobile phone?

You may use media query with orientation for this:

@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;
}
}

The trick here is to detect the changed orientation and using CSS transform to rotate the content of your web page so as to mock orientation-lock.

If you are comfortable with the idea of using Javascript to accomplish this then you can try this:

screen.orientation.lock('landscape');

See:

  • https://w3c.github.io/screen-orientation/
  • https://caniuse.com/screen-orientation


Related Topics



Leave a reply



Submit