html - CSS 媒体查询中的严格不等式

标签 html css media-queries

假设我有这个:

@media only screen and (max-width: 600px) {
    nav {
        display: none;
    }
}
@media only screen and (min-width: 601px) {
    nav {
        display: block;
    }
}

这意味着 600.5 等宽度的未定义行为,这是 HiDPI 显示器上可能的逻辑宽度。 min-widthmax-width 等的问题在于它们是弱不等式(分别为 >= 和 <=)。如何在 CSS 媒体查询(> 和 <)中实现严格不等式?

最佳答案

文档的宽度将始终四舍五入为 px 中的整数。如果您对细节感兴趣,可以阅读this article .

让我们做一个简单的测试:

// set width to 600.5px:
document.querySelector('body').style.width = '600.5px';

// get actual width:
console.log('600.5px gets parsed to ' + document.querySelector('body').offsetWidth);

// set width to 600.4999px:
document.querySelector('body').style.width = '600.4999px';

// get actual width
console.log('600.4999px gets parsed to ' + document.querySelector('body').offsetWidth);

这在技术上意味着 @media (max-width:600px) 适用于文档宽度等于或小于 600.49994(9)px 时。实际上,它从来没有小数宽度。

关于html - CSS 媒体查询中的严格不等式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48601618/

相关文章:

html - CSS 过渡在 IE 中不起作用

html - CSS: anchor 不接受高度

jquery - 使用 Href Id 添加图像,使用 CSS

css - 媒体查询不响应在桌面上更改浏览器大小

另一个脚本启动后 Javascript 失败

javascript - 如何在滚动网站时显示我的文字

html - anchor 标记内的文本未显示

jquery - CSS - 创建与 chrome 和 ie10 兼容的 div 网格

html - Bootstrap - 为什么最小媒体查询 768px?

css - 如何修复使用 CSS3 媒体查询时不正常的菜单?