CSS Resolution Media Feature for Retina Displays
The CSS resolution media feature allows developers to
target devices based on the pixel density of their output screens,
making it an essential tool for serving high-quality graphics to Retina
and HiDPI displays. This article explains how the
resolution media query works, breaks down its standard
units of measurement, and demonstrates how to implement it to deliver
crisp visual assets without wasting bandwidth on standard-resolution
screens.
Understanding Pixel Density and HiDPI
Standard displays typically feature a 1:1 ratio between physical screen pixels and CSS reference pixels. HiDPI screens, commonly branded as Retina displays by Apple, pack significantly more physical pixels into the same physical space. A 2x Retina screen, for instance, uses four physical pixels (a 2x2 grid) to render a single CSS pixel.
If a standard-definition raster image is displayed on a high-density
screen, the browser must stretch the image to fill the required space,
resulting in blurry, pixelated visuals. The CSS resolution
media feature solves this by detecting the screen's pixel density and
enabling conditional styles, such as serving high-resolution background
images.
Supported Resolution Units
The resolution media query evaluates density using three
standardized CSS units:
dppx(dots per pixel): Represents the number of physical pixels per CSS pixel unit. A standard display has a value of1dppx, while a 2x Retina screen corresponds to2dppx. This is the modern, preferred unit for web stylesheets.dpi(dots per inch): Represents physical dots per inch. In CSS,1dppxis equivalent to96dpi. Therefore, a 2x Retina display corresponds to192dpi(96 × 2).dpcm(dots per centimeter): Represents dots per centimeter (1 inch is approximately 2.54 cm, making1dpcmroughly2.54dpi). This unit is rarely used in web development.
Implementation and Syntax
The min-resolution condition is the standard syntax used
to target displays at or above a specified density threshold.
/* Default styles for standard displays (1x) */
.hero-banner {
background-image: url('banner-1x.jpg');
background-size: cover;
}
/* Targeting Retina / HiDPI displays (2x and higher) */
@media (min-resolution: 2dppx) {
.hero-banner {
background-image: url('banner-2x.jpg');
}
}
/* Targeting Ultra-HiDPI displays (3x and higher) */
@media (min-resolution: 3dppx) {
.hero-banner {
background-image: url('banner-3x.jpg');
}
}Legacy Support:
-webkit-min-device-pixel-ratio
Older WebKit-based browsers (such as older versions of Safari)
originally implemented a non-standard vendor prefix called
-webkit-min-device-pixel-ratio before the standard
resolution query was universally adopted. To ensure
backward compatibility with older legacy devices, both declarations can
be combined using a comma-separated list, which functions as a logical
OR:
@media
(-webkit-min-device-pixel-ratio: 2),
(min-resolution: 192dpi),
(min-resolution: 2dppx) {
.logo {
background-image: url('logo@2x.png');
background-size: 150px 50px;
}
}Benefits of the Resolution Media Feature
- Bandwidth Optimization: Standard-resolution devices only download standard-size assets, saving data and improving load times for mobile or low-bandwidth users.
- Visual Fidelity: High-density devices receive appropriately scaled raster assets, preventing blurriness across hero images, iconography, and backgrounds.
- Performance Balance: By serving 2x or 3x assets only where necessary, devices avoid the overhead of decoding unnecessarily large images on displays that cannot render the extra detail.