Resolution and Pixel Density Queries
You'll learn how to target high-resolution displays with media queries so your images and designs look crisp on devices like Retina screens.
What Are Resolution Queries?
Remember learning about device pixels vs CSS pixels? High-DPI displays (like Apple's Retina displays or modern Android phones) pack more physical pixels into the same CSS pixel. A standard screen might have a 1:1 ratio, but a Retina display has 2× or even 3× as many device pixels per CSS pixel.
Resolution media queries let you detect these high-density screens and serve sharper images or adjust styles accordingly.
The Core Syntax
You can target pixel density using three approaches:
1. Standard resolution feature:
@media (min-resolution: 2dppx) {
/* Styles for 2x displays and higher */
}
2. Older min-resolution with dpi:
@media (min-resolution: 192dpi) {
/* 192dpi = 2x density (96dpi × 2) */
}
3. Webkit-prefixed version (for older Safari):
@media (-webkit-min-device-pixel-ratio: 2) {
/* Styles for 2x displays */
}
Real-World Use Case
The most common use is swapping standard images for high-resolution versions:
.logo {
background-image: url('logo.png');
}
@media (min-resolution: 2dppx) {
.logo {
background-image: url('logo@2x.png');
}
}
This ensures users with Retina displays see sharp images while avoiding unnecessarily large files for standard screens.
Key Takeaway: Resolution and pixel density queries let you detect high-DPI displays using min-resolution, resolution, or -webkit-device-pixel-ratio, enabling you to serve appropriately sized assets for crisp visuals on all devices.