Is there a way to use DPI in css media queries instead of px
What you are looking for is the Device Pixel ratio. Because things like the iPhone display like a 320px screen but with a 640px layout (Pixel ratio of 2). In media queries, use "device-pixel-ratio". Though I would make sure to still use the vendor prefixes.
A good post on it: http://menacingcloud.com/?c=highPixelDensityDisplays
In 2021 you can use a media query for resolution, min-resolution, max-resolution in all modern browsers except safari.
header {
background-image: url(low-res-background.jpg);
}
@media (resolution >= 2dppx) {
header {
background-image: url(hi-res-background.jpg);
}
}
But CSS is not the only anwer. You can also just use HTML. Have a look at Resposive Images withe <img srcset=...>
.
p.s.
You could also use dpi to specify resolution, but this only makes sense for print:
@media print and (min-resolution: 72dpi) {
p {
text-decoration: underline;
}
}
See https://developer.mozilla.org/en-US/docs/Web/CSS/@media/resolution
You can do either:
<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)"
/>
or
@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*/
}