CSS Media Queries Overriding Each other - html

Thanks Stack Overflow for helping. I've got some custom css I'm using to tighten up a design. I keep running into this issue where if I change something in one media query say for the iphone 6, that change then affects another device say the iphone 5. Its becoming this issue were I'm constantly adjusting with no end in sight. Here are my #media break points I'm using.
/* IPHONE 6 PLUS */
#media only screen
and (min-device-width : 414px)
and (max-device-width : 736px)
and (orientation : portrait) {
}
/* IPHONE 6 */
#media only screen
and (min-device-width : 375px)
and (max-device-width : 667px)
and (orientation : portrait) {
}
/* IPHONE 5s */
#media only screen
and (min-device-width : 320px)
and (max-device-width : 568px)
and (orientation : portrait) {
}
/* IPAD LAYOUTS */
#media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px) {
}
/* IPAD LANDSCAPE */
#media only screen
and (min-device-width: 768px)
and (max-device-width: 1024px)
and (orientation: landscape)
and (-webkit-min-device-pixel-ratio: 1) {
}
Any help would be greatly appricated.

I agree with Birdman, and you should consider a mobile first approach. Mobile first, however, means that the smallest device size is completely outside of any media query. The next size up will start the first media query. You only ever need a min-width, as these new styles will be in addition to the base styles, not overwriting them. Each media query created will continue to combine with those already called.
And instead of worrying about iPad this, or tablet that... worry about when your design elements start to look bad. All of the major browsers have intelligent enough emulators to test in different device sizes.
Here is a good article on the pros and cons. I always code mobile first, and never worry about styles colliding, unless I do it on purpose :)
https://codemyviews.com/blog/mobilefirst

When a device width falls between the media query's range, the styling will be applied. So if a device's width is 500px, it will first have the 6plus styling, which will then be overridden by the 6's styling, then the 5s'. Normally, it's not recommended to try to tailor your CSS for a specific device, but if you do want to, you'll need to make sure none of the ranges overlap, or they will just be overridden by whichever styling comes last.

Related

All iPhone 5 and 6 (with/without fullscreen portrait and landscape) get each others' media query, and get the computer's media query

I know that, in one of questions, somebody said it is not wise to specify display/screen size, but the problem is that, the iOS app developer messed and mixed the website with Bootstrap and third-party codes, and since they knew me, they delivered me the website in my hands and I found a lot and a lot of mistakes and of problems he made in the website. I worked a lot to fix everything. But as he did not write in pure Bootstrap because of third-party codes, I had to specify display/screen size of mobile, tablet and computer. If he wrote in pure Bootstrap without third-party codes, I would not need to specify.
I know I should rewrite the site in pure Bootstrap or without Bootstrap (because I am a purist), but the CEO says they need to introduce their own app to the public next 2 weeks and next month.
I tried My iPhone 6 gets the iPhone 5 media query and #media iphone 5 and 6 queries and it almost worked.
I divided 6 specific media queries – iPhone 5 and up – portrait with or without full screen (because of Safari menu bar) and landscape, and iPhone 6 and up – portrait with or without full screen (because of Safari menu bar) and landscape.
If I added aspect-ratio only to iPhone 5 with full screen and
also to iPhone 6 with and without fullscreen and landscape,
everything worked, until...
I redesigned for iPhone 5 without full
screen and it got the media query from iPhone 6, and added
aspect-ratio to iPhone 5 without full screen, and all iPhone 5 and 6
ended up got each other's media-queries and also got the standard
computer's media-query.
If this is impossible, I'll try to convince the CEO to let me rewrite the site, while I will keep only the bottom sheets, which are totally good for every device. Or I think of using JavaScript to detect device agent and switch the CSS media-query file.
Here is my small code snippet of 6 media queries:
/* iPhone 5, 5C, 5E and 5S */
/* Portrait with fullscreen */
#media only screen
and (max-width: 320px)
and (max-height: 568px)
and (aspect-ratio: 40/71)
and (orientation: portrait) {}
/* Portait without fullscreen */
#media only screen
and (max-width: 320px)
and (max-height: 460px)
and (aspect-ratio: 40/71)
and (resolution: 192dpi)
and (orientation: portrait) {}
/* Landscape */
#media only screen
and (max-width: 568px)
and (max-height: 320px)
and (aspect-ratio: 40/71)
and (orientation: landscape) {}
/* iPhone 6 and 6S */
/* Portait with fullscreen */
#media only screen
and (max-width: 375px)
and (max-height: 667px)
and (aspect-ratio: 667/375)
and (orientation: portrait) {}
/* Portrait without fullscreen */
#media only screen
and (max-width: 375px)
and (max-height: 559px)
and (aspect-ratio: 667/375)
and (orientation: portrait) {}
/* Landscape */
#media only screen
and (max-width: 667px)
and (max-height: 375px)
and (aspect-ratio: 667/375)
and (orientation: landscape) {}
Update
I investigated the cause which required me to create several specific display sizes. I found some third-party codes came from Firebase, as unit-wrapper, box-image-about, show-r and hide-r. I found bad idea to mix Firebase and Bootstrap. These codes not part of Bootstrap, third-part codes and ones from Firebase are isolated do not allow the automatic and unspecific responsive, and require me to create many specific media queries.

CSS Media Query - target tablets more carefully

I've been using media queries to target tablets and down just by writing:
#media only screen and (max-width: 1199px) { ... }
However, I came across a tablet this weekend whose resolution were even bigger than that, triggering a menu it wasn't supposed to see. Normally, I don't care since I use these queries to trigger design changes that normally looks good in that resolution but in this particular case, it's not good since a hover based menu showed up that's obviously really hard to use on a tablet.
So I've been tinkering a bit with writing a media query that triggers on EITHER max-width or both of the orientations, like this:
#media handheld, (max-device-width: 1300px), (orientation: landscape), (orientation: portrait) { ... }
However, this seems to trigger on my computer as well and I don't understand why. Anyone know why, or has a better solution to my problem?
It's because you are using , instead of and in you media query condition .
For example:
#media handheld and (max-device-width: 1300px) and (orientation : landscape) {
.div {....}
}
#media screen (max-width: 449px), handheld and (orientation:landscape) { ... }
Try this one.

How to perform media queries if either statements are correct?

I'm looking to find out how to perform css based on whether either one of two statements is true. For example:
#media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px)
and (orientation : landscape)
OR
and (max-device-width : 1024px)
and (orientation : portrait){
}
I'm making a multi-platform website and have finished the mobile version, however I want iPads to have the laptop/desktop version, all was working good with this:
#media only screen and (min-device-width: 0px){Mobile Version CSS}
#media only screen and (min-device-width: 500px){Other Version CSS}
But then I noticed after changing a mobile to landscape, it would switch back to the desktop mode, due to the width of the media query being less than the screens landscape width. What's the best set of media queries, that I can perform in two different queries, just for mobile and other platform, that take in account a phone being landscape or portrait? I don't want to have to repeat my CSS code through multiple media queries because of phones going landscape and such. I just want the mobile version to be active whether a mobile is landscape or portrait.
Thanks
That's nothing a quick google search couldn't have revealed to you: (first try) https://css-tricks.com/logic-in-media-queries/
media-query logic:
And: and
Or: ,
Not: not
 
In your case, your CSS would look like:
#media
only screen and (min-device-width: 768px) and (max-device-width: 1024px) and (orientation: landscape),
only screen and (max-device-width: 1024px) and (orientation: portrait) {
/* your css here */
}
 
Just some reminders:
Your media-query do not serve the situations "portrait and device-width < 768px" and "device-width > 1024px". You need to address those cases too somehow.
While an iPad (in ladnscape) has a screen-width of 1024px, you would present your websites desktop view to any other phones/tablets having a width of at least 768px, too. I don't think that's a good idea. But I don't know what your website looks like, so I assume you know what you're doing.
BTW: Wouldn't a grid-system like bootstrap do a lot for you?

Media Queries not working on desktop

I am having a strange problem with my media queries. They seem to be working just fine on my mobile devices, but when I resize my desktop browser the changes are not applied. This makes using web inspector/firebug impossible to inspect my mobile styles. Here is my media query:
#media only screen
and (min-device-width : 320px)
and (max-device-width : 480px) {}
You are calling device specific queries. If you want to test on browser window resize, you need to remove -device- from both min and max calls.
#media only screen
and (min-width: 320px)
and (max-width: 480px) {}

Responsive web design and media queries for iPad Portrait not showing any changes when re-sizing window

I am looking to add some responsive web design to my site for iPad Portrait. I have my main css file (style.css) and then i have added the line below:
<link rel="stylesheet" href="ipad-portrait.css" media="only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : portrait)" />
However when i try to set a div's background colour to red or something then resize the window i cannot see any change when it gets to the iPad Portrait size.
The stylesheet im linking to looks like:
/* iPads (portrait) ----------- */
#media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px)
and (orientation : portrait) {
/* Styles */
.myTile{background:red;}
}
I have tried it with and without this and no difference when i resize.
What am I doing wrong?
Any help would be great, I am just picking up responsive design.
Thanks
I would use:
#media screen and (max-width: 768px) {
/*styles*/
}
I also use max-width. What's the difference between max-width vs max-device-width? The difference, max-device-width only affects those devices that have a max width of XYZpx, so re-sizing a browser window on your desktop would yield no styling changes in relation to that particular media query. max-width, will yield styling changes on any device/browser that fits the media query. So re-sizing your your desktop browser and having the media query use max-width, you'd see the website as someone using an iPad would.
this is a great reference: Responsive Web Design