媒体查询平板电脑画像及以下

Nrc*_*Nrc 7 css media-queries

我知道如何对平板电脑肖像进行媒体查询,为手机查看另一个媒体查询.但是,是否有可能只为所有人提供一个媒体查询:平板电脑肖像和手机

/* tablets portrait */
@media only screen 
and (min-device-width : 768px) 
and (max-device-width : 1024px)
and (orientation : portrait) { 

}

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

}

/* tablets portrait and phones 
Is it possible to have only one media query for this that do the same than the other media queries together?
*/
Run Code Online (Sandbox Code Playgroud)

nic*_*har 14

没有.

为什么?

iPad和iPhone是不同的设备,具有不同的屏幕尺寸,分辨率和不同的像素密度(基于不同的版本/版本 - 见这里).

Device                     Screen(res)   CSS pixel ratio
iPad   (generation 1,2)    1024 × 768    1
iPad   (generation 3,4)    2048 × 1536   2
iPhone (gen 1,3G,3GS)      480 × 320     1
iPhone (gen 4,4S)          960 × 640     2
iPhone (gen 5)             1136 x 640    2
Run Code Online (Sandbox Code Playgroud)

iPad被归类为平板电脑,iPhone是移动设备,因此您永远无法使用一个媒体查询来完全满足这两种设备或为这两种设备创造理想体验.


我能做什么?

一种解决方案是提供一种相当常见的方法,并为3级设备类型和/或屏幕宽度/方向提供媒体查询:

  • 手机/智能手机
  • 片剂
  • 桌面

基本理论是您需要在尽可能多的设备上使用您的站点或应用程序,从而使您的方法标准化,而不是错过过多的单个设备和/或制造商.

一个粗略的例子可能是:

@media screen and (max-width:500px) {
   /* Mobile styles */
}
@media screen and (min-width:501px) and (max-width:999px) {
   /* Tablet styles */
}
@media screen and (min-width:1000px) {
   /* Desktop styles */
}
Run Code Online (Sandbox Code Playgroud)

但是对于这三种类型之间的最佳断点,观点差异很大,我知道这些断点的研究/数据会受到开发者社区的广泛欢迎.


如果我想专门针对iOS设备怎么办?

你可以这样做.有些人提出了满足特定iOS设备特定要求的媒体查询(未经过测试,但我经常看到我下面列出的链接):

/* iPads (portrait and landscape) ----------- */
   @media only screen 
   and (min-device-width : 768px) 
   and (max-device-width : 1024px) {
   /* Styles */
}

/* iPads (landscape) ----------- */
   @media only screen 
   and (min-device-width : 768px) 
   and (max-device-width : 1024px) 
   and (orientation : landscape) {
   /* Styles */
}

/* iPads (portrait) ----------- */
   @media only screen 
   and (min-device-width : 768px) 
   and (max-device-width : 1024px) 
   and (orientation : portrait) {
   /* Styles */
}

/* iPhone 4 ----------- */
   @media
   only screen and (-webkit-min-device-pixel-ratio : 1.5),
   only screen and (min-device-pixel-ratio : 1.5) {
   /* Styles */
}
Run Code Online (Sandbox Code Playgroud)

其他人针对iOS的其他努力:

一般参考:


警告

我上面使用了一个示例方法,但可以通过多种方式应用媒体查询:

@media screen and (max-width:500px) { ... }
@import url(mobile.css) (max-width:500px);
<link rel="stylesheet" type="text/css" media="screen and (max-width: 500px)" href="mobile.css" />
Run Code Online (Sandbox Code Playgroud)