如何编码针对所有移动设备和平板电脑的CSS媒体查询? [英] How to code CSS media queries targeting ALL mobile devices and tablets?

查看:121
本文介绍了如何编码针对所有移动设备和平板电脑的CSS媒体查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@media only screen and (max-device-width : 640px) {
/* Styles */
}

@media only screen and (max-device-width: 768px) {
/* Styles */
}

这是我到目前为止。我正在开发的移动网站的PSD样机,宽640像素。另一个,平板电脑版的网站,是768像素。我只能使用max-width来测试网站在我的网络浏览器,但现在是时候让这个网站在设备上工作,它仍然渲染常规的全尺寸网页。上面的两个查询是我最好的猜测。

This is what I have so far. The PSD mockup for the mobile site I'm working on, is 640px wide. The other one, the tablet version of the site, is 768px. I was able to test the site in my web browser by only using max-width, but now it's time to get this site working on the devices, and it's still rendering the regular full size web page. The two queries above are my best guess. Where am I going wrong?

推荐答案

不是最初使用特定宽度,而是使用方向或任何其他废话弄乱建议使用以下媒体标签...

Instead of using specific widths initially, or messing around with orientations, or any other nonsense, I suggest using the following media tag...

@media only screen and (min-resolution: 117dpi) and (max-resolution: 119dpi), only screen and (min-resolution: 131dpi) and (max-resolution: 133dpi), only screen and (min-resolution: 145dpi) and (max-resolution: 154dpi), only screen and (min-resolution: 162dpi) and (max-resolution: 164dpi), only screen and (min-resolution: 169dpi) {
  /* Your touch-specific css goes here */
}
@media only screen and (min-resolution: 165dpi) and (max-resolution: 168dpi), only screen and (min-resolution: 155dpi) and (max-resolution: 160dpi), only screen and (min-resolution: 134dpi) and (max-resolution: 144dpi), only screen and (min-resolution: 120dpi) and (max-resolution: 130dpi), only screen and (max-resolution: 116dpi) {
  /* Your click-specific css goes here */
}

你使用这些标签是什么?设置悬停和悬停的内容点击vs触摸事件。

And what do you use these tags for? To set stuff for hover & click vs touch events.

触摸设备(除了上面灰色区域中的几个设备)的分辨率与桌面设备非常不同。不要 - 这是设置设计元素,但导航元素。有些pudent可能会说,最大宽度的一些疯狂可能会更好,但有这么多的高清手机,这是可笑的设备最大宽度很快变得无用。

Touch devices, other than a few devices in grey areas above addressed, have a very different resolution than desktop devices. Do -not- this to set design elements, but navigation elements. Some pudents may cry that some insanity with max-width may be better, but there's so many HD phones it's ridiculous that device-max-width quickly becomes useless.

应该使用宽度媒体宽度查询。然而,不要打扰max-device-width,只是max-width& min-width。让上述标记可以触摸而不触摸用户,让min-width和max-width地址基于窗口大小并调整网站视觉

However, you should use width media width queries. However, don't bother with max-device-width, just max-width & min-width. Let the above tags address your touch vs not touch users, let min-width vs max-width address based on window size and adjust site visuals.

此外,使用方向来确定它是否是移动的只是愚蠢,因为甚至监视器可以放置在各种方向(一个常见的设置,我见过3-监视器是纵向中心监视器和风景侧监视器。)

Further, using orientation to determine if it's mobile or not is just silly, as even monitors can be placed in various orientations (a common setup I've seen for 3-monitors is a portrait center monitor and landscape side monitors.)

对于宽度视图,专注于使您的网站在各种宽度上清洁,忽略什么类型的设备实际访问它,只需确保您的屏幕以各种尺寸干净地显示。这是适用于桌面和移动的好设计。如果他们将您的网站放在屏幕左上角的小窗口中,以便在其他地方使用其大部分屏幕媒体资源(或快速分心),而且应该为他们以及移动用户提供建立更小的宽度。尝试任何其他东西快速走下一个非常痛苦和自我毁灭的路径为web开发。所以对于这些较小的宽度,你可以设置你的宽度任何你想要的,但我会包括一些我个人喜欢。

For width views, focus on making your site clean on various widths, ignoring what kind of device is actually accessing it, just make sure your screen displays cleanly at various sizes. That's good design that applies to both desktop and mobile. If they have your site in a small window at the upper left corner of their screen for reference (or quick distraction) while using the majority of their screen real estate elsewhere, and it should be for them, as well as mobile users, that your smaller widths are built for. Trying anything else is quickly going down a very painful and self-defeating path for web development. So for those smaller widths, you can set your widths to whatever you want, but I'll include a few I personally like.

总之,你应该有类似This ...

So altogether, you should have something like this...

@media only screen and (min-resolution: 117dpi) and (max-resolution: 119dpi), only screen and (min-resolution: 131dpi) and (max-resolution: 133dpi), only screen and (min-resolution: 145dpi) and (max-resolution: 154dpi), only screen and (min-resolution: 162dpi) and (max-resolution: 164dpi), only screen and (min-resolution: 169dpi) {
    #touch_logo {
       display: inherit;
    }
    #mouse_logo {
       display: none;
    }
  /* Your touch-specific css goes here */
}
@media only screen and (min-resolution: 165dpi) and (max-resolution: 168dpi), only screen and (min-resolution: 155dpi) and (max-resolution: 160dpi), only screen and (min-resolution: 134dpi) and (max-resolution: 144dpi), only screen and (min-resolution: 120dpi) and (max-resolution: 130dpi), only screen and (max-resolution: 116dpi) {
    #touch_logo {
       display: none;
    }
    #mouse_logo {
       display: inherit;
    }
  /* Your click-specific css goes here */
}
@media (min-width: 541px){
  /* Big view stuff goes here. */
}
@media (max-width: 540px) and (min-width: 400px){
  /* Smaller view stuff goes here. */
}
@media (max-width: 399px){
  /* Stuff for really small views goes here. */
}

虽然不要忘记在页面的头部加入以下内容:

Although, don't forget to include the following in your page's head:

<meta name='viewport' content="width=device-width, initial-scale=1" />

在某些情况下,它可能会中断,但这应该比其他解决方案更简洁和更完整

It may still break on some cases, but this should be more concise and more complete than many other solutions.

这篇关于如何编码针对所有移动设备和平板电脑的CSS媒体查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆