【发布时间】:2015-11-20 13:47:08
【问题描述】:
我有两个 div,我将它们称为 A 和 B。期望的行为是:
- 如果屏幕宽度小于 400 像素,则应该只有 A 可见。
- 如果屏幕是横向的,宽度大于 400 像素,纵向小于 400 像素,则应该只有 B 可见。
- 否则(屏幕宽度在纵向和横向均大于 400 像素),A 和 B 都应该可见。
第二个项目被证明是有问题的。
我目前的方法适用于 iOS 设备,which always report min-device-width & max-device-width as the portrait values,无论当前屏幕方向如何,但不适用于 android 和其他设备,device-width 值随方向变化:
/* item 3 */
.a, .b {
display: block;
}
/* item 2 */
@media only screen and (min-width: 400px) and (max-device-width: 400px) and (orientation: landscape) {
.a { display: none; }
}
/* item 1 */
@media only screen and (max-width: 400px) {
.b { display: none }
}
是否可以仅在媒体查询中实现此行为,使其适用于所有设备?
如果可能,我希望避免使用 JavaScript、UA 嗅探以及使用 deprecated media types(例如手持设备)。
【问题讨论】:
标签: css media-queries orientation device-width