CSS Media Query Screen orientation issue - html

I am using this media query for target viewport max-width of 800px devices mininmum with of 400px
#media screen and (max-width: 800px) and (min-device-width: 400px)
{
body {background:#fff;}
}
And i need to change the color in the landscape orientation for this i use this code
#media screen and (orientation:landscape)
{
body {background:red;}
}
its working good in the device but the background red applies for pc browsers also how to apply background red in devices landscape orientation only?
Thank you.

You aren't choosing an element to apply the background to
#media screen and (orientation:landscape)
{background:red;}
Should be something like:
#media screen and (max-device-width: 1000px)
and (orientation:landscape){
body {
background: red;
}
}
The max-device-width should make it ignore desktops, if you don't put device in there, it will still affect desktops that have made their browser smaller.

try in this way
#media screen and (max-width: 800px)
and (min-width: 400px) {
body {
background: white;
}
}
#media screen and (max-width: 800px)
and (min-width: 400px)
and (orientation:landscape) {
body {
background: red;
}
}
or try to detect handheld-only devices looking at the min-resolution value as in http://jsbin.com/irozuf/1/edit - e.g.
#media screen and (max-width: 800px)
and (min-width: 400px)
and (min-resolution: 97dpi) /* a typical screen has 96dpi */
and (orientation:landscape) {
body {
background: red;
}
}
Looking at this list of displays by pixel density it seems that it could work fine on modern mobile devices

if you add these meta tags to head of page:
<meta name="viewport" content="width=device-width, initial-scale=1.0">
so the key: "width" means same "device-width".so,
#media screen and (max-device-width: 1000px){}
is equal to
#media screen and (max-width: 1000px){}
use each one you like:
[meta tags with key "width"]
OR
[no meta tags but "device-width"]

Related

#media function not working in this CSS code

I have been trying to make the #media work with this webpage, but have been unable to do so. I have the follow CSS and HTML code plugged in. What am I missing? When I try to resize the page the new style for the appropriate pixel does not take effect. However, the min-width: 1281px style does indeed work. The ones below it does not work
Tried on multiple different browsers, and did research online. Seems like the code is correct, not sure what am I missing.
#media (min-width: 1281px){
{CSS STYLES-1}
}
#media (min-width: 1025px) and (max-width: 1280px){
{CSS STYLES-2}
}
#media (min-width: 768px) and (max-width: 1024px) {
{CSS STYLES-3}
}
#media (min-width: 768px) and (max-width: 1024px) and (orientation: landscape) {
{CSS STYLES-4}
}
#media (min-width: 481px) and (max-width: 767px) {
{CSS STYLES-5}
}
#media (min-width: 320px) and (max-width: 480px) {
{CSS STYLES-6}
}
<meta charset="utf-8" content="width=device-width, initial-scale=1" name="viewport">
{HTML CODE}
when the window size is decreased I would like the CSS style e to change to match accordingly. I am mainly concern about mobile devices, tablets, laptop screens, and desktop, etc

Media Query Not setting for right screen resolution

My screen resolution is: 1280 x 768
I have two queries but whatever one is last to activate gets used which is not what i want. The one that gets activated is: 1280 x 720. What i dont get is my screen is 1280 x 768? Why does it not work?
Screen 1280 x 768:
#media only screen
and (min-device-height: 768px)
and (max-device-width: 1280px)
and (orientation: landscape)
{
body {
background-color: lightgreen;
}
}
Screen 1280 x 720:
#media only screen
and (min-device-height: 720px)
and (max-device-width: 1280px)
and (orientation: landscape)
{
body {
background-color: blue;
}
}
device-width/device-height has been deprecated
This feature has been removed from the Web standards. Though some
browsers may still support it, it is in the process of being dropped.
Avoid using it and update existing code if possible; see the
compatibility table. Be aware that this feature may cease to work
at any time.
In this case, when using min-*, the query with the highest height value needs to be last or else it does not work
And if you use min-height, I guess you'll get the expected result
Note, it's not the computer's screen width that counts, it's the browsers viewport, so if your computer screen is 1280x768 you need to run the browser in full screen for it to work
#media only screen and (min-height: 720px) {
body {
background-color: blue;
}
}
#media only screen and (min-height: 768px) {
body {
background-color: lightgreen;
}
}
If you also want this to only work on screen with a max-width, do like this
#media only screen and (min-height: 720px) and (max-width: 1280px) {
body {
background-color: blue;
}
}
#media only screen and (min-height: 768px) and (max-width: 1280px) {
body {
background-color: lightgreen;
}
}
And of course, limited for landscape will look like this
#media only screen and (min-height: 720px) and (max-width: 1280px) and (orientation: landscape) {
body {
background-color: blue;
}
}
#media only screen and (min-height: 768px) and (max-width: 1280px) and (orientation: landscape) {
body {
background-color: lightgreen;
}
}

Media query settings

I created a media query for phones
#media screen and (max-device-width: 480px), screen and (max-width: 480px) { ...css here... body {width:50%;}
but i need this query for desktop
#media screen and (max-device-width: 750px), screen and (max-width: 750px)
if i add like to #media desktop query .body {width:100%;} the settings is changed in 480px also.
My question is: How i separate 480px css settings, and 750px css settings to be different
method A:
write larger width queries before smaller so they can overwrite them
#media screen and (max-width: 750px){
body{
width:100%;
}
}
#media screen and (max-width: 480px){
body{
width:50%;
}
}
method B:
define min-width as well:
#media screen and (max-width: 750px) and (min-width: 481px){
body{
width:100%;
}
}
#media screen and (max-width: 480px){
body{
width:50%;
}
}
NOTE:i think that you dont need both max-device-width and max-width

Custom CSS for iPad Mini 2, retina display and #media screen/viewport settings

I'm trying to specify different colors for selected range of screen sizes, however I just can't seem to figure out the iPad Mini 2 with Retina Display. It does simply not follow the rules of it's pixel resolution and I wonder why.
Here is my code:
/** Retina iPad **/
#media
screen and (-webkit-min-device-pixel-ratio: 1.5),
screen and (-moz-min-device-pixel-ratio: 1.5),
screen and (-o-min-device-pixel-ratio: 1.5),
screen and (min-device-pixel-ratio: 1.5){
body {
background-color: #486ffd;
}
}
/** 1600px non-retina screen **/
#media screen and (max-width: 1600px){
body {
background-color: #770029;
}
}
/** 1000px non-retina screen **/
#media screen and (max-width: 1000px){
body {
background-color: #117700;
}
}
/** 500px non-retina screen **/
#media screen and (max-width: 500px){
body {
background-color: #ffce00;
}
}
/** 300px non-retina screen **/
#media screen and (max-width: 300px){
body {
background-color: #770200;
}
}
Now when my iPad Mini 2 is in portrait mode it shows the background color #117700, and when I have it in landscape it shows the color #770029. How come it does not follow the rules of its resolution on: 2048x1536?
I also have this in my HTML:
<meta name="viewport" content="width=device-width; initial-scale=1; maximum-scale=3;" />
I've tried using both a pixel ratio on 1.5 and 2 which has been suggested by others in previous questions. Any help?
The website I am using is here if you wish to see for yourself.
The problem was solved by putting the CSS specification in the correct order.
When selectors have an equal specificity value, the latest rule is the
one that counts.
I also added:
only screen and (min-resolution: 192dpi)
only screen and (min-resolution: 2dppx)
In order for it to be even more fit and specified.
/** 1600px non-retina screen **/
#media screen and (max-width: 1600px){
body {
background-color: #770029;
}
}
/** Retina iPad **/
#media
only screen and (-webkit-min-device-pixel-ratio: 2),
only screen and (-moz-min-device-pixel-ratio: 2),
only screen and (-o-min-device-pixel-ratio: 2/1),
only screen and (min-device-pixel-ratio: 2),
only screen and (min-resolution: 192dpi),
only screen and (min-resolution: 2dppx){
body {
background-color: #486ffd;
}
}
So correct order is: LAST overrules FIRST, which means device-width needs to be overruled by the retina specifics. More about this can be read here CSS Specificity

Media Queries CSS

For some reason my media queries do not resize my site correctly when viewing from an iphone or mobile device, i get the tablet version of the site on an iphone. The media queries work fine for tablet and perfectly when shrinking a browser window.
Have I missed anything?
#media only screen and (min-width: 1024px) {
/*styling here*/
}
#media only screen and (max-width: 1023px) and (min-width: 740px) {
/*Tablet styling here*/
}
#media only screen and (max-width: 739px) and (min-width: 0px) {
/*Mobile styling here*/
}
Check Out this,
and let me know whether it is working or not
You need to specify device width.
I think you may need to specify it as 'min-device-width'.
#media only screen and (max-device-width: 739px) and (min-device-width: 0px) {
/*Mobile styling here*/
}