简体   繁体   English

针对移动应用到桌面的 CSS 媒体查询

[英]CSS Media Queries targeted for mobile applying to desktop

Webpage: www.mathias-syversen.net网页: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.我正在尝试使用@media 查询来检测屏幕尺寸和设备来制作一个移动友好的网页。

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. @media only screen and (min-device-width: 420px) and (max-device-width : 768px) 仅适用于 iPad,不适用于台式机或安卓手机。

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.尝试过无数种最大和最小屏幕宽度的组合,每次它在移动设备(android)上运行时,它也适用于桌面。

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)如果我尝试检测像素比,它适用于智能手机,但也适用于桌面(至少在 OS X 上,可能是因为它有视网膜显示器)

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.我想要的是一种简单的方法来确定设备是否不是桌面,并应用正确的 css,无论它是 android、iOS、手机还是平板电脑。


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

Now works on desktop > 800px, Tablet and mobile.现在适用于桌面 > 800px、平板电脑和移动设备。 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. 我应该指出,我也在学习反应式,所以我可能不是100%正确。

Viewport and pixel resolution (on mobile devices) are not the same. 视口和像素分辨率(在移动设备上)不同。 Consider loading a standard 960px web page on your mobile phone. 考虑在您的手机上加载标准的960px网页。 You can see it all, but if you consider the iPhone does not have 960 pixels in width (portrait); 您可以看到所有内容,但是如果您认为iPhone的宽度不为960像素(人像),则可以看到所有内容。 it's resolution is either 320 (iPhone 3, 3G, 3GS) or 640 (4 and up). 它的分辨率为320(iPhone 3、3G,3GS)或640(4及更高版本)。 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. 在iPhone上,其默认宽度为980px ,因此,为什么基于960px网格系统的页面看起来不错,您无需水平滚动,甚至在空白边距处都可以看到10px。

So, the default viewport width size is 980px, but the native resolution width is either 320px or 640px depending on the phone model. 因此,默认视口宽度大小为980px,但本机分辨率宽度为320px或640px,具体取决于手机型号。 To add further complexity all iPhones use the same viewport width of 320px. 为了进一步提高复杂性,所有iPhone都使用相同的320px视口宽度。 When a page loads and it doesn't cater for mobile devices you're essentially viewing 3 x zoomed out (980 / 320). 当页面加载且无法满足移动设备的需求时,您实际上是在查看3倍缩小(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. 以iPhone为例,您无法缩小到980像素以上。 If the page extends beyond 980px you would need to scroll the page horizontally. 如果页面超出980像素,则需要水平滚动页面。

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. 如果您正在查看默认大小(980px)的页面,并且放大了该页面的一部分(您可能会放大以100%缩放),那么您只会看到该页面的一部分。

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标记,否则它将缩放/缩小其默认视口大小。

<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%. 这告诉浏览器其窗口的宽度应等于设备的本机视口宽度(请注意,此宽度不必与本机分辨率宽度相同) 并且应缩放为1或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. 根据该网站的数据,Galaxy S4的视口尺寸为360 x 640。

Proof of concept 概念证明

Create a div, set up 2 styles: 创建一个div,设置2种样式:

  1. Background colour blue 背景色蓝色
  2. Background colour red with a max width of 360px 背景颜色为红色,最大宽度为360px

View the page on the S4 in portrait and landscape. 以纵向和横向查看S4上的页面。 The div should change colour; div应该改变颜色; 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. 确保在文档的头部包含上面的meta标签。

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%. 根据我已经完成的研究,在设备上查找本机视口大小要容易得多-例如,当您以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. @media only屏幕和(最小设备宽度:420像素)和(最大设备宽度:768像素)仅适用于iPad,但不适用于台式机或android移动版。

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. 这意味着它可以在分辨率介于420和768像素之间的设备上工作,因此不适用于分辨率低于或高于此分辨率的设备。 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. 如果您在mozilla上安装了Web开发人员工具,请按Ctrl + Shift + M并尝试调整窗口的大小,如果在该分辨率范围内它不适用,则可以在其中看到实际的分辨率(420-> 768) ,应该是CSS错误,请尝试验证您的代码。

PS:Hope i get your question correct. PS:希望我的问题正确。

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.我建议如果您使用 (max-width:959px) 这将包括从移动设备到平板电脑的景观和非常小的 res 小型计算机。 Then (min-width:960px) this is you breakpoint from laptop to desktop.然后 (min-width:960px) 这是从笔记本电脑到台式机的断点。 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) {
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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