我知道如何为平板电脑肖像和手机做另一个媒体查询。但是,是否有可能只有一个唯一的媒体查询:平板电脑纵向和手机?
/* 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?
*/
最佳答案
没有。
为什么?
iPad 和 iPhone 是不同的设备,具有不同的屏幕尺寸、分辨率和不同的像素密度(基于不同的版本/发布 - 请参阅 here)。
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
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 */
}
但关于这三种类型之间的最佳断点的观点差异很大,我知道关于此类断点的研究/数据将受到开发者社区的广泛欢迎。
如果我想专门针对 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 */
}
其他人针对 iOS 的进一步努力:
- CSS Media Queries for iPads & iPhones
- Basic iPhone And iPad Screen Component Pixel Dimensions
- ios-test.css - media queries for iOS devices
- SO question: iPhone 5 CSS media query
一般引用:
警告
我在上面使用了一个示例方法,但可以通过多种方式应用媒体查询:
@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" />
关于css - 媒体查询平板纵向及以下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16122767/