2012-09-22 76 views
117

iPhone 5有一个更长的屏幕,它不捕捉我的网站的移动视图。什么是新的响应式设计查询的iPhone 5,我可以结合现有的iPhone查询?iPhone 5的CSS媒体查询

我现在的媒体查询是这样的:

@media only screen and (max-device-width: 480px) {} 
+3

我们可以看到您现有的媒体查询吗? – BoltClock

+0

试试这个吗? http://halgatewood.com/iphone-5-media-query/ – sachleen

回答

248

另一个有用的媒体功能是device-aspect-ratio

请注意,iPhone 5没有16:9长宽比。实际上是40:71。

iPhone < 5:
@media screen and (device-aspect-ratio: 2/3) {}

iPhone 5:
@media screen and (device-aspect-ratio: 40/71) {}

iPhone 6:
@media screen and (device-aspect-ratio: 375/667) {}

iPhone 6加:
@media screen and (device-aspect-ratio: 16/9) {}

的iPad:
@media screen and (device-aspect-ratio: 3/4) {}

参考:
Media Queries @ W3C
iPhone Model Comparison
Aspect Ratio Calculator

+2

我喜欢这种方法,但无法在PhoneGap应用程序webview中工作,直到像Stephen Sprawl所解释的那样添加-webkit-min-device-pixel-ratio http://zsprawl.com/iOS/2012/09/css-媒体查询,这换了,iphone-5。(device-aspect-ratio:40/71)和(-webkit-min-device-pixel-ratio:2){iphone5 for phonehaps } – TaeKwonJoe

+0

我必须添加'和( -webkit-min-device-pixel-ratio:2)'在iPhone 5媒体查询中使其在PhoneGap webview中工作,如@TaeKwonJ​​oe所述 –

+1

感谢您对此进行更新以包含iPhone 6! – Maverick

63

有了这个,我信用this blog

@media only screen and (min-device-width: 560px) and (max-device-width: 1136px) and (-webkit-min-device-pixel-ratio: 2) { 
    /* iPhone 5 only */ 
} 

记住它反应了iPhone 5,而不是特定的iOS版本安装在所述设备上。

要使用现有版本进行合并,你应该能够逗号分隔它们:

@media only screen and (max-device-width: 480px), only screen and (min-device-width: 560px) and (max-device-width: 1136px) and (-webkit-min-device-pixel-ratio: 2) { 
    /* iPhone only */ 
} 

注:我没有测试上面的代码,但我测试过用逗号分隔@media查询之前,他们工作得很好。

请注意,上述可能会遇到一些其他共享相似比率的设备,如Galaxy Nexus。这是一个额外的方法,将只针对具有640px(560px由于一些奇怪的显示像素异常)和960px(iPhone < 5)和1136px(iPhone 5)之一的尺寸的设备。

@media 
    only screen and (max-device-width: 1136px) and (min-device-width: 960px) and (max-device-height: 640px) and (min-device-height: 560px), 
    only screen and (max-device-height: 1136px) and (min-device-height: 960px) and (max-device-width: 640px) and (min-device-width: 560px) { 
    /* iPhone only */ 
} 
+0

这很完美。我无法相信今天早上我在搜索中找不到这个!谢谢!! – Maverick

+1

你能建议我如何将这个查询与我的旧查询结合起来以同时捕获iphone 4和5? – Maverick

+0

@Stylez Yup,我已经完成了上述工作。 – Eric

-2

你应该把“-webkit-min-device-pixel-ratio”设置为1.5来捕捉所有的iPhone?

@media only screen and (max-device-width: 480px), only screen and (min-device-width: 640px) and (max-device-width: 1136px) and (-webkit-min-device-pixel-ratio: 1.5) { 
/* iPhone only */ 
} 
+1

哪个iPhone使用的设备像素比率为1.5? – BoltClock

+1

我强烈建议根据设备宽度进行媒体查询,而不是设备名称(iPhone)。所以如果iPhone 5的宽度更大,只需要使用它即可。 –

5

无响应的工作对我来说靶向phonegapp应用。

如下面的link分,下面的解决方案工作。

@media screen and (device-height: 568px) and (orientation: portrait) and (-webkit-min-device-pixel-ratio: 2) { 
    // css here 
} 
7

对我来说,是做了工作的查询是:

only screen and (device-width: 320px) and (device-height: 568px) and (-webkit-device-pixel-ratio: 2) 
31

所有这些问题的答案上面列出,使用max-device-widthmax-device-height媒体查询,遭受很强的错误:他们也瞄准很多其他受欢迎的移动设备(可能是不需要的,从未测试过,或将在未来上市)。

此查询将适用于任何具有较小屏幕的设备,并且可能会损坏您的设计。

结合类似的设备特定媒体查询(HTC,三星,iPod,Nexus ...),这种做法将启动定时炸弹。对于debigging,这个想法可以让你的CSS成为一个不受控制的spagetti。你永远不能测试所有可能的设备。

请注意,只有媒体查询始终瞄准的iPhone5并没有别的是:

/* iPhone 5 Retina regardless of IOS version */ 
@media (device-height : 568px) 
    and (device-width : 320px) 
    and (-webkit-min-device-pixel-ratio: 2) 
/* and (orientation : todo: you can add orientation or delete this comment)*/ { 
       /*IPhone 5 only CSS here*/ 
} 

注意确切的宽度和高度,而不是最大宽度在这里检查。


现在有什么解决方案?如果你想要写一个网页,将寻找所有可能的设备好,最好的做法是使用降解

/*退化模式 我们正在检查屏幕宽度只有 肯定的是,这将改变从纵向转向景观*/

/*css for desktops here*/ 

@media (max-device-width: 1024px) { 
    /*IPad portrait AND netbooks, AND anything with smaller screen*/ 
    /*make the design flexible if possible */ 
    /*Structure your code thinking about all devices that go below*/ 
} 
@media (max-device-width: 640px) { 
/*Iphone portrait and smaller*/ 
} 
@media (max-device-width: 540px) { 
/*Smaller and smaller...*/ 
} 
@media (max-device-width: 320px) { 
/*IPhone portrait and smaller. You can probably stop on 320px*/ 
} 

如果您的网站访问者的30%以上来自手机,把这个方案颠倒,提供移动优先的方法。在这种情况下使用min-device-width。这将加快移动浏览器的网页渲染速度。

+9

我会先进行增强和移动。这是从移动和开始使用最小设备宽度。 – primavera133

+2

另请注意,仅匹配iPhone 5的高度/宽度/像素比例解决方案也会匹配出现的具有相同特征的下一个手机。您的答案的整体主题是正确的:根据*特定设备思考*是错误的方式。 – Pointy

+1

对于清晰明确的评论+1 +1 – Philip007

0

AFAIK没有iPhone使用1.5

iPhone 3G/3GS的像素比:(-webkit装置像素比:1) iPhone 4G/4GS/5G:(-webkit设备像素2)

5

只是一个非常快速的补充,因为我一直在测试一些选项,并一路错过了这个。确保你的页面有:

<meta name="viewport" content="initial-scale=1.0"> 
0
/* iPad */ 
@media screen and (min-device-width: 768px) { 
    /* ipad-portrait */ 
    @media screen and (max-width: 896px) { 
     .logo{ 
      display: none !important; 
     } 
    } 
    /* ipad-landscape */ 
    @media screen and (min-width: 897px) { 

    } 
} 
/* iPhone */ 
@media screen and (max-device-width: 480px) { 
    /* iphone-portrait */ 
    @media screen and (max-width: 400px) { 

    } 
    /* iphone-landscape */ 
    @media screen and (min-width: 401px) { 

    } 
} 
5

iPhone 5在纵向&景观

@media only screen 
and (min-device-width : 320px) 
and (max-device-width : 568px) { 

/* styles*/ 
} 

iPhone 5在景观

@media only screen 
and (min-device-width : 320px) 
and (max-device-width : 568px) 
and (orientation : landscape) { 
/* styles*/ 

} 

iPhone 5在纵向

@media only screen 
and (min-device-width : 320px) 
and (max-device-width : 568px) 
and (orientation : portrait) { 
/* styles*/ 

} 
+0

为什么你需要纵向上的'max-device-width:568px'? – adi518

+1

@ adi518如果你不使用最大宽度,那么css规则适用于大于320px的任何设备,就像字面上的大部分设备 - 平板电脑桌面等 – Sudheer

+1

为什么不使用'设备宽度:320px和设备高度:而不是568px'? – adi518