I have the following media query for a mobile app,
only screen and (max-height: 630px) and (orientation: portrait) {}
What I want is for the styles inside this media query to apply to all screens which has a height of 630px or less. Basically it should apply for smaller screens. On the browser this works great. But on actual devices, it doesn't work properly.
Devices which are larger than 630px still applies the styles inside that media query. I managed to fix it for some screens by adding the pixel ratio -
only screen and (max-height: 630px) and (orientation: portrait) and (-webkit-device-pixel-ratio: 2) {}
Still on some larger screens (even new phones) the styles are getting applied. What am I doing wrong here.
Try using device-height
#media only screen
and (max-device-height: 630px)
and (orientation: portrait) {
/* Code */
}
A good article about media query https://blog.box.com/blog/media-queries-things-i-wish-id-known/
Try this.
#media screen and (max-height: 630px){
//Write your mobile styles here
}
Related
I've seen the CSS media query below recommended to target phones. Yes, it works for my phone. However, my phone, and many others, have resolution width 1080px. How does it work...?
#media screen and (max-width: 1024px) { }
Every devise has physical pixel size and a ratio for browsers. For instance iPhoneX has with 1125px and a ratio 3. So the CSS width will be 375px.
So for it's screen with physical resolution 1125px your media will be
#media screen and (max-width: 375px) { }
Very good table with devises resolutions, ratios and CSS scale here:
https://www.mydevice.io/#compare-devices
Although you can determine in media the -webkit-device-pixel-ratio and orientation, like this
/* iPhone X in landscape */
#media only screen
and (min-device-width : 375px)
and (max-device-width : 812px)
and (-webkit-device-pixel-ratio : 3)
and (orientation : landscape) { /* STYLES GO HERE */}
More about it here http://stephen.io/mediaqueries/
Is there a way to media-query if the window is taller than it is wide in CSS?
A sorta portrait vs landscape for any devices.
I know this is easy enough with javascript but I'm searching for a CSS only solution.
This will select the devices which has width more than 320 and less than 767 and orientation will be landscape only.
#media screen and (min-width: 320px)and (max-width: 767px)and (orientation: landscape) {}
another example of you want to select by height and width combination:
#media (max-width: 320px) and (min-height: 400px) and (max-height: 812px) {}
althoug better use is using orientation in your case, as per the question
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.
I am using bootstrap to build a client's site and I have come unstuck when trying to target landscape and portrait orientation on mobile in order to add some specific styles for both viewports. How do I target portrait and landscape orientation for mobile styles? I need to add specific styles at 320px breakpoint and certain styles at 480px breakpoint. With my current media queries this is not working Currently in my stylesheet I have the following:
/* portrait phones */
#media only screen and (max-width: 320px) and (orientation:portrait) {
/* Styles */
}
/* landscape phones */
#media only screen and (min-width: 321px) and (orientation:landscape) {
/* Styles */
}
If I put styles in for landscape however I don't think they are being picked up. Every time I make a change and then refresh my Iphone I don't see any difference. Im thinking maybe my media queries are wrong? If there is a better way to target mobile states I would greatly appreciate any help.
Try to use:
#media (orientation: portrait) and (max-width: 400px) {Fooobar}
#media (orientation: landscape) and (max-width: 400px) {foobar}
I managed to resolve this issue in the end by adding a max-width to my 321px media query and was able to target both landscape and portrait mobile orientation. I also found in my header I had: initial-scale=1 which seemed to be causing the problem and after removing it I was able to target the mobile breakpoints I needed.
/*Portrait phones */
#media (max-width:320px){}
/* Landscape phones and down */
#media (min-width: 321px) and (max-width: 480px) {}
I have the following rules:
#media only screen and (min-device-width : 320px) and (max-device-width : 480px)
#media only screen and (min-width : 321px) /* Smartphones (landscape) */
#media only screen and (max-width : 320px) /* Smartphones (portrait) */
#media only screen and (min-width: 768px) /* tablets and desktops */
How to catch tablet portrait without affect the other rules?
There is no standard for "tablet portrait" in terms of device pixel width.
The #media orientation query is not very reliable at all and not widely supported. See here. You are best off just using min-width and max-width media queries and trying to get it to work at ALL possible widths than targeting a specific orientation. That's pretty much how responsive design is supposed to work anyway.
Portrait mode tablets will generally be between 768px and ~960px wide.
#media only screen and (min-width: 768px) and (orientation:portrait) and (min-height:1024px)
You should also beside specifying min-width and min-height specify max width and height combining it with orientation, then you really catch up mobiles without affecting others like tablets or pc, right now only with min-width for mobiles will also affect all devices accomplying that min-width
Pure CSS has helper classes for hiding stuff in tablets.
The media query it uses for tablets is as follows:
#media (min-width: 768px) and (max-width: 979px)
You could also try adding (orientation:portrait) to that.
(As seen in http://yui.yahooapis.com/pure/0.3.0/pure.css)