CSS Reference ยท Units and Values
The resolution units: dpi, dpcm, dppx and x
Reference for CSS resolution units dpi, dpcm, dppx and x: how they relate, where they are allowed, and how to use them in media queries and image-set().
Resolution units describe pixel density: how many device pixels are used to draw a given amount of space. CSS has four of them: dpi (dots per inch), dpcm (dots per centimeter), dppx (dots per CSS pixel) and x, which is simply another spelling of dppx.
A value with a resolution unit is a <resolution>. It is not a length, so you cannot use it for width or margin. It appears in just two places: the resolution media feature, and image-set() for choosing between image files.
@media (min-resolution: 2dppx) { .logo { background-image: url("[email protected]"); } }
Try it
Syntax
<resolution> = <number> [ dpi | dpcm | dppx | x ]
Conversion factors:
| Unit | Equals |
|---|---|
1dppx |
1 device pixel per CSS pixel |
1x |
1dppx |
1dpi |
1/96 dppx |
1dpcm |
about 2.54 dpi (1 inch is 2.54 cm) |
96dpi |
1dppx |
Values
| Value | Description |
|---|---|
dpi |
Dots per inch. 96dpi is the same as 1dppx. |
dpcm |
Dots per centimeter. 1dpcm is about 2.54dpi. |
dppx |
Dots per CSS pixel. A "2x" display has 2dppx. |
x |
Alias for dppx. Common in image-set(), for example url([email protected]) 2x. |
Usage notes
- Prefer
dppxorx. They match what JavaScript reports aswindow.devicePixelRatio, so the numbers in your CSS and script agree. dpiis not physical. Browsers define96dpias one CSS pixel per device pixel. The unit does not measure real printed dots unless you are printing.- Use
min-resolutionfor high-density rules. Amin-query with2dppxmatches every display at 2x or above, so one rule covers 2x, 3x and beyond. - Often unnecessary. Modern pages can use
srcsetin HTML, orimage-set()in CSS, and let the browser pick the file. Write your own resolution queries only when you need different layout or styles per density. - Not valid elsewhere.
width: 2dppxis invalid and ignored.
Browser support
dppx, dpi and dpcm have long been supported; the x spelling is newer, so check a compatibility table before using it in a media query without a fallback.
Related
resolutionโ the media featureimage-set()โ choose images by density@mediaโ the at-rule that hosts media features- Lesson: Units: px, em, rem and More