css - 媒体查询通过最大宽度隐藏 DIV

标签 css

http://jsbin.com/regovusu/1/edit

我一直在构建一个 WYSIWYG 网站设计器,今天我有一些时间来合并媒体查询。

我有 3 个 div 设置,.mobile.tablet.desktop

计划是如果浏览器400px 或更低 显示.mobile div,
400px600px 显示 .tablet,
600px + 显示 .desktop

我不确定为什么没有响应。

非常感谢任何帮助。

CSS:

body {
  margin: 0;
  padding: 0;
}

.mobile, .tablet {
  display: none;
}

@media all and (max-width:400px) { 
  .tablet, .desktop {
    display: none;
  }
}

@media all and (max-width:600px) { 
  .mobile, .desktop {
    display: none;
  }
}

最佳答案

当您使用具有最大宽度的媒体查询时,您应该始终将较大的值包含在较小的值之上,因为您希望较小的宽度覆盖较大的宽度。

/* common rules always active */

@media all and (max-width:400px) { 
    /* this will only shows when the max width is 400 */
}

@media all and (max-width:600px) {
    /* this will only show when the max width is 600,
    but it will override the 400 view  because when the
    max width is 600, it is always greater than 400 */
}

为避免此问题,您需要将 600 block 移动到 400 block 上方。

/* common rules always active */

@media all and (max-width:600px) {

}

@media all and (max-width:400px) { 
    /* now the 400 block will override the 600 view
    when the max width is less than 400 */
}

第二部分是您需要在移动设备和平板电脑 View 中重置显示属性。例如显示: block

这是一个简单的例子 http://jsfiddle.net/4xaFe/

关于css - 媒体查询通过最大宽度隐藏 DIV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23190237/

相关文章:

javascript - 如何在没有getElementByID的情况下从CSS获取ID到Javascript?

css - 我不明白为什么这个 CSS 不起作用

html - 如何阻止 2 个 div 重叠?

html - 如何以正确的方式使用 [attribute ="value"]?

css - 调整屏幕大小时如何防止带有 "overflow:hidden"的div换行

html - css 没有动画计时功能

CSS Wrap 给页面白色边框

javascript - 如何淡化div中背景颜色之间的过渡?

javascript - CSS - 按钮和文本输入在包含日语文本时不对齐

css - 如何在HTML中嵌入RTSP实时流?