【问题标题】:@media query not working for iPad in Portrait@media 查询不适用于纵向 iPad
【发布时间】:2014-12-24 17:15:05
【问题描述】:

我有以下媒体查询...景观媒体查询始终优先。我试着改变这些想法的顺序,如果肖像是第一个,它会优先,但这不起作用。事实上,即使我完全删除了横向媒体查询并只留下纵向它也不起作用,所以看起来纵向媒体查询以某种方式被破坏了:S

这是我的两个查询:

/* iPad Retina, landscape */
@media only screen 
and (min-device-width : 768px) 
and (max-device-width : 1024px) 
and (orientation : landscape)
and (-webkit-min-device-pixel-ratio: 2) { 
#container {
 width: 995px !important;
}


#currentroom_convo {
    -webkit-overflow-scrolling: touch;
    overflow-y: auto !important;
   overflow-x: hidden !important;
}

.slimScrollBar .ui-draggable {
   opacity: 0 !important;
 }

}


/* iPad Retina, portrait*/
@media only screen 
and (min-device-width : 768px) 
and (max-device-width : 1024px) 
and (orientation : portrait)
and (-webkit-min-device-pixel-ratio: 2)  { 

#container {
 width: 300px !important;
}



#currentroom_convo {
    -webkit-overflow-scrolling: touch;
    overflow-y: auto !important;
    overflow-x: hidden !important;
}

.slimScrollBar .ui-draggable {
   opacity: 0 !important;
 }

 div .topbar {
    max-width: 300px !important;
    }

}

我的标题中有这个元标记:

<meta name="viewport" content="width=device-width, initial-scale=1.0, minimal-ui, user-scalable=no">

请注意,我正在使用 !important,因为我需要覆盖来自第三方插件的其他一些 CSS。在横向查询中删除 !important 不会导致 #container 元素发生任何变化(甚至仍然不会选择纵向查询)。

我试过在这里搜索,但似乎没有一个答案对我有用。谢谢:)

【问题讨论】:

  • 尝试删除iPad竖屏CSS下div .topbar中的空格,使其变为div.topbar
  • 我完全删除了 div .topbar 样式,但它仍然不起作用:(
  • 我还从两个查询中删除了“and (-webkit-min-device-pixel-ratio: 2)”,认为这并没有太大的区别......它没有... :(

标签: css ipad media-queries


【解决方案1】:

你的方法是错误的。请注意,当您将设备方向从横向更改为纵向时,您的宽度变为高度,高度变为宽度。在您的代码中,您没有相应地更改这些值。因此,当屏幕宽度介于 786 像素到 1024 像素之间时, 会根据您的条件应用肖像样式。我认为这个条件不满足,因为 ipad 在纵向模式下的分辨率宽度是标准的 786px。因此,您的样式永远不会被应用。

也不要使用max-device-width & min-device-width & max-device-width 而应使用min-width & max-width。这背后的原因是max-device-width 给你整个屏幕的宽度,但max-width 给你浏览器视口的实际宽度。

牢记所有这些,将您的代码更改为以下内容:

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

/* iPad Retina, portrait*/
@media only screen and (max-width:768px) and (orientation:portrait){ 
    /* Styles */
}

【讨论】:

  • 好的,我按照你说的做了,得到了以下结果 - 横向样式应用正确,纵向时不再应用于屏幕,但是纵向部分没有样式被捡起(它的所有有效的 css)......想法?
  • 我尝试过使用简单的 CSS 来更改 div 的背景颜色。风景作品,肖像没有被拾取(但没有使用很好的风景样式):
  • 'code'/* iPad Retina, 横向 / 'code'@media only screen 'code'and (min-width : 768px) 'code'and (max-width : 1024px) 'code'and (orientation : Landscape) { 'code'#container { 'code'background-color: red !important; 'code'} 'code'} 'code'/ iPad Retina, portrait*/ 'code'@media only screen 'code'and (max-width : 768px) 'code'and (orientation : portrait) { 'code'#container { 'code' 'code' 背景色:蓝色 !important; '代码'} '代码'}
猜你喜欢
  • 1970-01-01
  • 2020-07-10
  • 2013-06-16
  • 2017-07-30
  • 2012-07-10
  • 1970-01-01
  • 2012-10-24
  • 1970-01-01
相关资源
最近更新 更多