html - CSS max-width 的像素精度不准确

标签 html css google-chrome responsive-design media-queries

我对 max-width 有疑问媒体规则。我测试隐藏不同 max-width 上的元素值(value)观和行为不一致。我测试了 4 种屏幕宽度不同的场景。

  • 场景 1:屏幕宽度为 638px:这符合我的预期。
  • 场景 2:屏幕宽度为 639px:这是错误的!我预计 column639仍然要隐藏!
  • 场景 3:屏幕宽度为 640px:符合我的预期。
  • 场景 4:屏幕宽度为 641px:符合我的预期。

问题:

场景 2 中像素不准确的原因是什么,为什么是 639px值(value)与其他值(value)区别对待?

可重复性:

奇怪的是,它无法在任何地方重现(将窗口大小调整为693px的宽度:

代码:

<!DOCTYPE html>
<html>
    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1">
    </head>
    <body>
        <h1>max-width issue</h1>
        <div class="column column639">
            <h2>Column 1</h2>
        </div>
          
        <div class="column column640">
            <h2>Column 2</h2>
        </div>
    </body>
</html>
.column {
    width: 100%;
    background-color: cyan;
}

@media screen and (max-width: 639px) {
    .column639 {
        visibility: hidden;
    }
}

@media screen and (max-width: 640px) {
    .column640 {
        visibility: hidden;
    }
}

场景:

场景 1:屏幕宽度为 638px : enter image description here

场景 2:屏幕宽度为 639px : enter image description here

场景 3:屏幕宽度为 640px : enter image description here

场景 4:屏幕宽度为 641px : enter image description here

最佳答案

max-width 包括您提供的宽度限制:等于或更窄

For example, this CSS will apply styles only if your browser's viewport width is equal to or narrower than 12450px:

@media (max-width: 12450px) { ... }

Documentation


我做了一个codepen测试一下。试试吧。

结果如我所料...

enter image description here

关于html - CSS max-width 的像素精度不准确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65470317/

相关文章:

html - 如何管理 Css Floats

html - 使用单独的 CSS 文件与 HTML 样式标签时的网站下载速度

javascript - 显示基于 2 个日历日期之间的 div

html - 背面可见性禁用表单控件

jquery - 将 console.log 转换为输出到 div

html - 为什么这些 img 不计入父 div?

javascript - 在网络 worker 中解码图像

javascript - 如何在 Google Chrome 中使用工具栏打开 window.open?

html - 我可以在 Twitter 的官方小部件上强制使用窄布局吗?

javascript - 如何使用 JQuery 修复窗口高度以调整带有 overflow-y 滚动的面板