I know it's with pure CSS possible to adapt the stylesheet according to screen dimensions, like this:
#media (max-width: 959px) {
/* styles for smallest viewport widths */
}
#media (min-width: 600px) and (max-width: 959px) {
/* styles for mid-tier viewport widths */
}
#media (min-width: 960px) {
/* original CSS styles */
}
(source)
Is it with pure css possible to check on a landscape or portrait display?
Yes, using the following syntax:
#media all and (orientation: landscape) {}
See the w3 specs for more information.
You can use orientation:
#media all and (max-width: 959px) and (orientation : portrait) {
/* Styles */
}
From w3:
#media all and (orientation:portrait) { … }
#media all and (orientation:landscape) { … }
All answers are incorrect. Android will swap from portrait to landscape when the keyboard is shown.
Different keyboards also need testing as they can take up more vertical space. Swift keyboard takes up more vertical space so you cannot use solutions like #media screen and (min-aspect-ratio: 13/9) { /* landscape styles here */} as this will fail on lots of phones.
The only solution is to use javascript.
On newer Android devices you can test and use the new window.screen.orientation api.
On iOS you can use window.orientation which works fine. ie Math.abs( window.orientation ) === 90 is landscape
And as a fallback you can use window.screen.width > window.screen.height which will cover really old Android devices which don't support the new window.screen.orientation api
Then all you need to do is add / remove a class on resize / orientationchange events.
/* Android Orientation */
var orientation = window.screen.orientation || window.screen.mozOrientation || window.screen.msOrientation || null;
/* Check */
if ( orientation && orientation.type ) {
/* Landscape */
if ( orientation.type === 'landscape' || orientation.type === 'landscape-primary' || orientation.type === 'landscape-secondary' ) {
return 'landscape';
/* Portrait */
} else {
return 'portrait';
}
}
Related
I used media queries in making my page responsive on mobile but notice that when the device rotates it doesn't effect the media query. How can I use it in a way that it still holds even when the screen rotates(landscape).
You should use Device orientation
Device Orientation
#media all and (orientation:portrait) {
/* Styles for Portrait screen */
}
#media all and (orientation:landscape) {
/* Styles for Landscape screen */
}
for reference : 1) click me 2) me too
I have past experience of working with foundation. So I started using foundation media queries rules for creating responsive site.
// Small screens
#media only screen { } /* Define mobile styles */
#media only screen and (max-width: 40em) { } /* max-width 640px, mobile-only styles, use when QAing mobile issues */
// Medium screens
#media only screen and (min-width: 40.063em) { } /* min-width 641px, medium screens */
#media only screen and (min-width: 40.063em) and (max-width: 64em) { } /* min-width 641px and max-width 1024px, use when QAing tablet-only issues */
// Large screens
#media only screen and (min-width: 64.063em) { } /* min-width 1025px, large screens */
#media only screen and (min-width: 64.063em) and (max-width: 90em) { } /* min-width 1025px and max-width 1440px, use when QAing large screen-only issues */
// XLarge screens
#media only screen and (min-width: 90.063em) { } /* min-width 1441px, xlarge screens */
#media only screen and (min-width: 90.063em) and (max-width: 120em) { } /* min-width 1441px and max-width 1920px, use when QAing xlarge screen-only issues */
// XXLarge screens
#media only screen and (min-width: 120.063em) { } /* min-width 1921px, xxlarge screens */
Here is my problem:
I am trying to hide a menubar in mobiles that is shown in desktop version (like how show-for-small in foundation). So I have defined stylings for mobile inside the media query #media only screen and (max-width: 40em). Surprisingly it is not working . So I have added the following rule before the above mentioned rule, #media only screen { }, then it worked.
I also tried the combination of #media only screen and (min-width:5 em) and (max-width: 40em). It also did not work.
I also have viewport meta tag defined in my page. Can anyone explain me why this is happening so ???
i think the problem maybe related to css specificity ( one command has higher priority)
so what about trying to put
#media only screen and (max-width: 40em)
in the last of your commands
and also inspect element in browser to know if this command is overwrited by other command
this is the most things happen with me when works with bootstrap ,wish this help you
You have to give your element the hide-for-small class so that it won't show up on mobile screens: Know as visibility classes
I just did a speed test on desktop & mobile on gtmetrix and have the error code: Serve Scaled Images - But only on the mobile speed test. The error reads as follows:
**.jpg is resized in HTML or CSS from 1200x431 to 318x114. Serving a scaled image could save 520.9KiB (92% reduction).
Is there a specific code I can put with the image to have it one size when on mobile and leave the desktop one at the original/other size. Or is there another way such as serve a particular image for mobile (same image smaller size) then another image for serving desktops?
Thanks.
You can do like this and define different background url on different media queries for same class.
/* Smartphones (portrait and landscape) ----------- */
#media only screen
and (min-device-width : 320px)
and (max-device-width : 480px) {
/* Styles */
}
/* Smartphones (landscape) ----------- */
#media only screen
and (min-width : 321px) {
/* Styles */
}
/* Smartphones (portrait) ----------- */
#media only screen
and (max-width : 320px) {
/* Styles */
}
/* iPads (portrait and landscape) ----------- */
#media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px) {
/* Styles */
}
/* iPads (landscape) ----------- */
#media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px)
and (orientation : landscape) {
/* Styles */
}
I think you might want something like this:
var isMobile = {
Android: function() {
return navigator.userAgent.match(/Android/i);
},
BlackBerry: function() {
return navigator.userAgent.match(/BlackBerry/i);
},
iOS: function() {
return navigator.userAgent.match(/iPhone|iPad|iPod/i);
},
Opera: function() {
return navigator.userAgent.match(/Opera Mini/i);
},
Windows: function() {
return navigator.userAgent.match(/IEMobile/i);
},
any: function() {
return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
}
};
if(isMobile.any()){
// Mobile!
} else {
// Not mobile
}
You can then do something like: (untested below(needs tweaking))
if(isMobile.any()){
img { height:140%;width:140%
You can alter the size of images through the style tag. img { should alter all <img srctags on the page as I've done it before. And a revision of the above code should alter it only for mobile or desktop computers.
I have different views on portrait and landscape
/* portrait ----------- */
#media only screen
and (max-width : 320px) {
body{
padding:20px;
}
}
/* landscape----------- */
#media only screen
and (min-width : 321px) {
body
{
padding:60px;
}
}
/* webpage----------- */
body
{
padding:0px;
}
however, landscape css effects on webpage view. how do I spilt webpage up?
I tried to make another media query on webpage, but it didnt work.
also I tried (min-device-width : 321px) for devices only, but it doesnt work
As explained in this article, the media query spec includes orientation detection. It should look something like this:
#media screen and (orientation:landscape) and (min-width:321px) {
foo {
padding:60px;
}
}
#media screen and (orientation:portrait) and (max-width:320px) {
foo {
padding:20px;
}
}
And so on.
I need to findout and apply tablet width from css with css expression.
I have a div content. it should apply width 100% in portrait mode and When i turn to landscape the div should change the width to half of the tablet device width(tablet width/2). How to apply this expression method in css?
I'd try to steer clear of expressions, as they are limited to Internet Explorer 5,6 and 7 (what tablets runs this??), and they slow things down considerably (performance wise). Anyway, try this:
#media screen and (orientation:portrait) {
/* Portrait styles */
}
#media screen and (orientation:landscape) {
.element {
width:expression(document.body.clientWidth / 2);
}
}
You can also try more specifics - these would be considered as hacks (thanks to TheBlackBenzKid for suggesting it):
/* Windows 7 Phone - WP7 */
#media only screen and (max-device-width: 480px) and (orientation:portrait) {
}
#media only screen and (max-device-width: 480px) and (orientation:landscape) {
}
/* Apple iPhone */
#media only screen and (max-device-width: 320px) and (orientation:portrait) {
}
#media only screen and (max-device-width: 320px) and (orientation:landscape) {
}
If not using expressions, (e.g. to target other browsers and .. well, tablets) you can use a small javascript to detect the orientation, and then add a class to the element:
html:
<body onorientationchange="updateOrientation();">
Javascript:
function updateOrientation() {
if(window.innerWidth> window.innerHeight){
//we are in landscape mode, add the 'LandscapeClass' class to the element
document.getElementById("element").className += " LandscapeClass";
} else {
//we are in portrait mode, remove the class
document.getElementById("element").className =
document.getElementById("element").className.replace
( /(?:^|\s)LandscapeClass(?!\S)/ , '' );
}
If using jQuery, you could try this, to modify the element's (inline) CSS directly:
function updateOrientation() {
var $width;
if(window.innerWidth> window.innerHeight){
$width = window.innerWidth/2;
} else {
$width = '100%';
}
$(".element").css({width:$width});
}
I have not tested any of this, but I think it will work