【问题标题】:combining two media queries结合两个媒体查询
【发布时间】:2023-04-07 11:41:02
【问题描述】:

我正在尝试测试媒体查询。当宽度小于

我尝试过使用:

  1. @media only screen and (max-width: 600px) and (max-height: 400px),
  2. @media only screen and (max-width: 600px) and only screen (max-height: 400px),
  3. @media only screen and (max-width: 600px and max-height: 400px),
  4. @media only screen and (max-width: 600px), (max-height: 400px)

但是 1-3 没有做任何事情,如果任一陈述为真,则 4 将背景渲染为蓝色。每当我用谷歌搜索我想做的事情时,我都会看到像#4 这样的解决方案的答案。 (例如this SO answer.)这不是我想要的。

我通过使用下面的嵌套查询使其工作。 这是做我想做的最好/唯一的方法还是有更清洁的解决方案?

@media only screen and (max-width: 600px) {
    @media only screen and (max-height: 400px) {
        body {
            background-color: blue;
        }
    }
}

【问题讨论】:

  • 你确定#1 不起作用吗?它直接等同于您拥有的嵌套规则。
  • @BoltClock 这很尴尬。我不知道我是怎么搞砸的。

标签: css media-queries


【解决方案1】:

最干净的方法是您提出的第一个解决方案1,它实际上工作正常 - 请参阅下面的 sn-p(展开为全尺寸):

div {
  background-color: blue;
  height: 300px;
}

@media only screen and (max-width: 600px) and (max-height: 400px) {

  div {
    background-color: red;
  }


}
<div></div>

1@media only screen and (max-width: 600px) and (max-height: 400px)

【讨论】:

猜你喜欢
  • 2021-12-25
  • 2012-02-18
  • 2014-07-16
  • 2013-12-22
  • 2014-03-28
  • 2012-08-28
  • 2015-11-11
  • 1970-01-01
  • 2018-02-15
相关资源
最近更新 更多