简体   繁体   中英

CSS Media Queries targeted for mobile applying to desktop

Webpage: www.mathias-syversen.net

Hello, this is my first post, so be kind! I am trying to make a mobile friendly web page using @media queries to detect screen size and device.

Problem is, if I get the query to work with the mobile, it also applies to the desktop version. Probably because the max / min width is too large, to compensate for the new smartphones with high resolution.

@media only screen and (min-device-width: 420px) and (max-device-width : 768px) Only works for iPad, but not for desktop or android mobile.

Have tried uncountable combinations of max and min screen width, and each time it works on the mobile (android) it also applies to the desktop.

If I try to detect pixel ratio, it works on the smartphone, but also applies to desktop (at least on OS X, probably because it has a retina display)

What I want is an easy way to just determine if the device is NOT a desktop, and apply the proper css, regardless if its android, iOS, phone or tablet.


@media screen and (max-width: 800px), (max-device-width: 480px), (max-device-width: 768px) {}

Now works on desktop > 800px, Tablet and mobile. As far as I have tested, this seams to dove my problem.

I should point out I'm learning reponsive too, so I may not be 100% right.

Viewport and pixel resolution (on mobile devices) are not the same. Consider loading a standard 960px web page on your mobile phone. You can see it all, but if you consider the iPhone does not have 960 pixels in width (portrait); it's resolution is either 320 (iPhone 3, 3G, 3GS) or 640 (4 and up). So why do you see the whole page? It scales, or zooms out of the page to fit it in the viewport. On the iPhone its default width is 980px , hence why pages based on the 960px grid system look fine, you don't need to scroll horizontally, you've even got 10px either side of margin.

So, the default viewport width size is 980px, but the native resolution width is either 320px or 640px depending on the phone model. To add further complexity all iPhones use the same viewport width of 320px. When a page loads and it doesn't cater for mobile devices you're essentially viewing 3 x zoomed out (980 / 320).

Note, by default a mobile device will scale out to its maximum - you can't scale or zoom out anymore. Using the iPhone example you couldn't zoom out beyond 980px. If the page extends beyond 980px you would need to scroll the page horizontally.

If you're looking at a page in default size (980px) and you zoom in on a portion of that page (you could be zooming in to scale at 100%) you're only going to see a section of that page.

Considering mobile devices, unless the webpage you're viewing contains the meta tag below in the head section, it will zoom / scale out of it's default viewport size.

<meta name="viewport" content="width=device-width, initial-scale=1.0">

This tells the browser that the width of its window should be equal to the device's native viewport width (note, this is not necessarily the same as the native resolution width) and it should scale at 1, or 100%.

I suggest you have a look at Viewport Sizes to reference the device you want to target.

According to the website the Galaxy S4's viewport's dimensions are 360 x 640.

Proof of concept

Create a div, set up 2 styles:

  1. Background colour blue
  2. Background colour red with a max width of 360px

View the page on the S4 in portrait and landscape. The div should change colour; in portrait it should be red, landscape it should be blue.

Make sure you include the meta tag above in the head of the document.

From the research around I've done, it's far easier to find native viewport sizes on devices - ie when you're looking at the browser window at a scale of 1 / 100%. Finding the default viewport size on devices is harder, but thankfully when you're designing for mobile, it's the native viewport size you'll be most concerned with.

@media only screen and (min-device-width: 420px) and (max-device-width : 768px) Only works for iPad, but not for desktop or android mobile.

This means that it's work on devices which got their resolution beween 420 and 768px, so it wont apply to devices which got lower or higher resolutions than this. If you have web developer tools installed on mozilla press Ctrl+Shift+M and try to resize the window, there you can see the actual resolution on it, if it doesn't apply there in this scale of resolution (420 -> 768), it should be a CSS error, try to validate your code.

PS:Hope i get your question correct.

You have to really identify which breakpoints you want to use. I would suggest if you use (max-width:959px) this will include from mobile to tablet landscaped and very small res little computers. Then (min-width:960px) this is you breakpoint from laptop to desktop. Identifying which breakpoints in the beginning is very important in media query because this can get overwhelming in the end.

 /** this is for mobile to little laptops res**/
  @media only screen and (max-width:959px) {
   }  

 /** this is for little laptops res to desktop**/
 @media only screen and (min-width:960px) {
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM