css - 有没有办法在 css 媒体查询中使用 DPI 而不是 px

标签 css media-queries

宽度和高度的像素数并不总能说明全部情况。这对于在屏幕上添加或删除元素非常有用,但对于设置正确的图像来说不太合适。在 MBP 上使用 Retina 显示器,设置为屏幕一半的浏览器窗口将具有与当今大多数机器相同数量的像素宽度。然而,考虑到较高的 DPI,显示的图像可能会太小。

有没有一种方法可以根据 DPI 而不是像素宽度和高度来更改图像?

最佳答案

您可以执行以下任一操作:

<link
    rel="stylesheet"
    type="text/css"
    href="/css/retina.css"
    media="only screen and (-moz-min-device-pixel-ratio: 2), only screen and (-o-min-device-pixel-ratio: 2/1), only screen and (-webkit-min-device-pixel-ratio: 2), only screen and (min-device-pixel-ratio: 2)"
/>

@media only screen and (-moz-min-device-pixel-ratio: 2), 
       only screen and (-o-min-device-pixel-ratio: 2/1), 
       only screen and (-webkit-min-device-pixel-ratio: 2), 
       only screen and (min-device-pixel-ratio: 2) {
 /*use CSS to swap out your low res images with high res ones here*/
}   

关于css - 有没有办法在 css 媒体查询中使用 DPI 而不是 px,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11722442/

相关文章:

javascript - Angular : Dynamically changing image (portrait/landscape) using media queries?

html - 如何根据不同的屏幕宽度缩放我的网站

jquery - 使用 jquery 添加 css 规则

html - 在没有不良影响的情况下为倾斜的 div 设置动画

html - 检测滞后位置的媒体查询 : fixed behaviour

javascript - 如何更改调整窗口大小的 javascript 选项?

css 媒体查询 : target mobile devices without specifying width, 像素比例等

jquery - 使用 JQuery 进行内联编辑/保存/取消

html - 如何使 Logo 出现在背景图片上?

html - 如何删除链接之间的空格?