Mobile orientation change not applying CSS - css

I'm testing a website I'm developing and am using media queries.
When I test and resize the page in a browser, everything is good.
But when I test on my mobile device, I encounter a problem when I change the orientation of the phone.
If I load the page in landscape mode, the correct CSS are applied.
When I change to portrait, the CSS are also correct.
But if I go back to landscape, the portrait css classes are still being applied.
I'm using these metatags
<meta name="MobileOptimized" content="320">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
And in my media queries I have
#media
only screen and (max-width: 610px),
only screen and (max-width: 610px) and (orientation:landscape) { ... }
#media
only screen and (min-device-width: 240px) and (max-device-width: 520px),
only screen and (min-width: 240px) and (max-width: 520px) { ... }
I've alerted the device width to make sure it's ok and in landscape mode it's 598px wide and portrait is 384px
I'm using a Nexus 4 (Android 4.3)
How come the CSS aren't applied once I change back the orientation?
EDIT:
If I load the site in portrait and then change to landscape, the CSS aren't applied.
It's as if once it goes to the smallest resolution, it can't go back.

On my Nexus 4, I have something that looks like this and seems to work for your test cases:
<meta http-equiv='X-UA-Compatible' content='IE=edge,chrome=1'>
<meta name='viewport' content='width=device-width'>
And I make no reference to orientation in the media query, for example:
#media only screen and (max-width: 610px) { /* Some CSS here */ }
EDIT: Looks like you have to put max-device-width after the other max-width stuff in terms of the media queries. To quote vyx.ca in the comments below...
Just found my problem. Notice how I define 'max-device-width' before the rest. If I put that condition last, it works. 'max-device-width' is used for retina display.

My problem is related to the order of my CSS requests.
I used to define 'min-device-width' before the rest.
#media
only screen and (min-device-width: 240px) and (max-device-width: 520px),
only screen and (min-width: 240px) and (max-width: 520px) { ... }
But if I define it last, it works.
#media
only screen and (min-width: 240px) and (max-width: 520px),
only screen and (min-device-width: 240px) and (max-device-width: 520px) { ... }
For more information about device-width:- check out this question

Well-known quiksmode site defines device-width / device-height media features as static.
These media queries are static once determined; i.e. they do not update the value they’re checked against when the device orientation is changed. (So if you start in portrait and then switch to landscape, the portrait device-width still counts. Reloading the page solves this.)
That's why using max-device-width is still applied after changing orientation.

this code worked for me:-
#media only screen and (min-width: 240px) and (max-width: 520px), only screen and (min-device-width: 240px) and (max-device-width: 520px) and (orientation:portrait)
{
body
{
background:#009;
}
}
#media only screen and and (max-width: 610px), only screen and (max-device-width: 610px) and (orientation:landscape)
{
body
{
background:#993;
}
}

Your media queries have overlap. Max-width of 520 will also be true of max-width 610. Also you have an OR in the landscape so it's possible to be true in Portrait if max-width 610 is true. Overlapping media queries will work like CSS in that it cascades so you'll get odd behavior.
I don't see any reference to orientation:portrait in your queries so how do you know portrait classes are being applied. You typically want to make it an either or when doing orientation queries.
Also try adding min-width to remove any width overlap
#media only screen and (orientation:landscape) { ... }
#media only screen and (orientation:portrait) { ... }

Related

CSS - Is it safe only to use one #media query when designing for mobile?

I'm doing a website that is supposed to work on mobile devices. I have researched about the subject and every website recommends that I use a different media query for each device I intend the website to work on, for example:
/* ----------- iPhone 4 and 4S ----------- */
/* Portrait */
#media only screen
and (min-device-width: 320px)
and (max-device-width: 480px)
and (-webkit-min-device-pixel-ratio: 2)
and (orientation: portrait) {
}
/* Landscape */
#media only screen
and (min-device-width: 320px)
and (max-device-width: 480px)
and (-webkit-min-device-pixel-ratio: 2)
and (orientation: landscape) {
}
/* ----------- iPhone 5 and 5S ----------- */
/* Portrait */
#media only screen
and (min-device-width: 320px)
and (max-device-width: 568px)
and (-webkit-min-device-pixel-ratio: 2)
and (orientation: portrait) {
}
/* Landscape */
#media only screen
and (min-device-width: 320px)
and (max-device-width: 568px)
and (-webkit-min-device-pixel-ratio: 2)
and (orientation: landscape) {
}
/* etc... */
However I feel it would be much much simpler to just use one media query for landscape and another for portrait orientation, but I haven't found anyone recommending that.
I imagine you may want to design something more specific for tablet. But speaking only about mobile phones, I can only thing of a reason to have different media queries for each device if you want something CRAZY specific.
Is there any reason for it?
Should I add a media query for each device or is it "safe" to continue with only two media queries?
That's absolutely fine. Don't forget to add the responsive meta tag to every page:
<meta name="viewport" content="width=device-width, initial-scale=1.0">
and then use media queries as you resize your preview window width and height. That'll make the website same as the desktop version.
You can totally pull it off with just one or two media queries. I do it all the time for tablet and then for mobile in fully responsive sites that work on all devices. Those type of fleshed out media queries are for very specific sizes when the developer wants to have a set version of the site for this size and that size.
#media screen and (max-width: 1024px) {
/* Landscape style changes */
}
#media screen and (max-width: 768px) {
/* Portrait to mobile style changes */
}
#media screen and (max-width: 450px) {
/* Maybe one more because that header text doesn't fit anymore on smaller screen */
}
It will be as good the rest of your code, but if you have clean css this should not be a problem.
There is NO problem with it, as far as I see it.
You make your site Responsive for not only the browser window (resizing) but also Adaptive on specific devices. Adding and on those media queries is good too if you see how it behaves (target) on Android phones since your breakpoint basis are iPhone.
You may consider creating another .css file for phone/mobile, the same goes for others (Tablet, TV, etc).
w3schools - media queries
Put all your mobile queries on separate .css. Facebook did the same m.facebook.com.

Media queries not working on mobile phone

I am working on my own website with self-made media-queries.
DEMO: JSFIDDLE
IMAGE EXAMPLE (mobile screen)
As you can see it works fine on deskopt, but I dont know why on mobile displays high resolution .
Any clue?
MEDIA QUERIES USED:
#media only screen and (max-height: 700px) {}
#media only screen and (max-width: 1023px) {}
#media only screen and (max-width: 880px) {}
#media only screen and (max-width: 700px) {}
Viewport:
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
Note: Yes, I know I am not following the standars, but shouldn't be the problem (sure?)
It's a shame to me say this, but the problem was on the mediaqueries sintaxis.
I just forgot the ending tag "}", so modern browsers understands where it ends, but not the mobile ones.

Media query min-width:1000px including iPad landscape

How can I target devices which are wider than 1000 pixels? That is, including iPad in landscape mode and most desktops but excluding iPad in portrait mode.
I have found how to target iPad specifically:
/* iPads (landscape) ----------- */
#media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px)
and (orientation : landscape) {
/* Styles */
}
/* iPads (portrait) ----------- */
#media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px)
and (orientation : portrait) {
/* Styles */
}
However, I would prefer if my media queries could be shaped by at what widths the content prompted layout changes rather than by specific devices.
If I understand your question correctly, you don't need to try and target specific devices at all.
In your main CSS just include:
#media (min-width: 1000px) {
/* CSS styles for viewports 1000px and up */
}
and in the head of your page include
<meta name="viewport" content="width=device-width, initial-scale=1.0">
EDIT
Here is a working example which you can check in an iPad:
Live: http://fiddle.jshell.net/panchroma/Bn4ah/show/
Code view: http://fiddle.jshell.net/panchroma/Bn4ah
Good luck!
While that article you referenced from CSS-Tricks is a very handy reference (and Chris Coyier is my nerd-idol) it may be a bit misleading. Media queries at their core are just fancy if statements, and if the entire statement evaluates as true, then the corresponding style sheet or style rules are applied, following the normal cascading rules.
Let's look at a common media queries:
#media screen and (min-width: 768px) { ... }
Notice the screen parameter. It says nothing about the device width nor browser chrome. Read in plain English, it says "if the media we're working on is the screen, and if that screen is a minimum of 768px or bigger, use these styles.
So, to answer your question: to target screens that are larger than 1000px wide, try this:
#media screen and (min-width: 1000px) { ... }
Just remember that you're targeting all screens at least 1000 pixels wide. If you need to target specific devices, I recommend using JavaScript to handle specific UserAgent targeting.

media queries for desktop 1024 vs ipad

I need to set my site on desktop (1024 * 768) and ipad, but I can not separate his Vizualization.
I'm using in Ipad:
# media only screen and (min-device-width: 768px) and (max-device-width : 1010px)
and desktop:
# media (min-width: 1020px)
its Works in Firefox, but not Chrome.
Rather than trying to target specific devices, it's arguably better to set appropriate breakpoints specific to your layout. That is, gradually narrow your browser and observe the points at which this particular design needs to reflow. Then set styles that apply to those points, and let each device receive whatever layout works best within its dimensions.
So, in the head of your page, I recommend you place this:
<meta name="viewport" content="width=device-width">
and then in your style sheet, use something like this (the numbers are arbitrary):
/* default styles, perhaps for basic mobiles and older browsers */
/* end default styles */
#media only screen and (min-width: 1025px) { }
#media only screen and (min-width: 701px) and (max-width: 1024px) { }
#media only screen and (max-width: 700px) { }
#media only screen and (min-width: 320px) and (max-width: 480px) { }
There are so many combinations of this that the above is just a rough example. You don't always need max and/or min. It depends on the layout.
There's also an argument for using ems instead of px, but I won't go there for now. :)
Why not just cascade down?
#media screen and (max-width : 1024px){ /*Styles*/ }
#media screen and (max-device-width : 768px){ /*Styles*/ }
1024x768 is iPad size anyway.

Only landscape mode works in iPad portrait orientation (CSS)

If you go here on an iPad, and click through to Chapter1 > Chapter 1 > Get started... you will see after a loading screen the Chapter 1 page.
Basically what this is, is html embedded into an iframe being pulled together by HTML5 and JavaScript. the html in this iframe calls its own css sheet called other.css. The html file that pulls this all together is calling a stylesheet called styles.css.
Obviously I want in portrait view the content area of this iframe to be smaller than in landscape. I am using the css in other.css :
#media only screen and (device-width: 768px) and (orientation:landscape) {
#content {background:green;}
}
#media only screen and (device-width: 768px) and (orientation:portrait) {
#content {background:blue;}
}
The problem is that its like it doesn't even see the portrait css. I have tried a dozen different ways ( this is supposed to be the correct way and works for the styles.css adjustments to the whole page) but it will not recognize it. It will only use the landscape. Its not as though it wont see the media queries, it pulls the landscape CSS. But WILL NOT use the portrait. Really weird. If you see green for the bg in portrait and landscape its ignoring the portrait. If you see blue it's working. How can I achieve this?
If I get rid of landscape CSS, it prefers the default to the portrait. makes no sense. Could the iframe be hindering its pulling in new CSS upon orientation change?
You should target min or max device widths or you will miss out devices.
/* iPads (landscape) ----------- */
#media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px)
and (orientation : landscape) {
/* Styles */
}
/* iPads (portrait) ----------- */
#media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px)
and (orientation : portrait) {
/* Styles */
}
from http://css-tricks.com/snippets/css/media-queries-for-standard-devices/
Here's an explanation why you probably shouldn't even be that specific http://catharsis.tumblr.com/post/501657271/ipad-orientation-css-revised
what you could try doing is creating two different stylesheets specifically for desktop & Tablet so;
<link rel="stylesheet" media="screen" href="css/stylesheet1.css"> <!--Desktop-->
<link rel="stylesheet" href="css/tablet-nav.css" media="screen and (min-width: 800px) and (max-width: 1024px)"> <!--tablet-->
and dont forget to add;
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
http://webdesign.tutsplus.com/tutorials/htmlcss-tutorials/quick-tip-dont-forget-the-viewport-meta-tag/
http://webdesignerwall.com/tutorials/css3-media-queries

Resources