🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEcss

css Documentation

LOADING ENGINE...

Resolution

AI & DATA SCIENCE // resolution

The resolution media feature applies styles conditionally based on a device's pixel density, commonly used to serve higher-resolution images or assets specifically to high-DPI (Retina-style) displays.

Syntax

@media (min-resolution: 2dppx) { ... }

Deep Dive Course

resolution is typically expressed in dppx, dots per pixel unit, where 1dppx represents a standard-density display and 2dppx represents a high-density display with twice as many physical pixels per CSS pixel in each dimension, like Apple's Retina displays — this is commonly used with min-resolution to conditionally load a higher-resolution background image specifically for high-density screens, where a standard-resolution image would otherwise appear visibly blurry or soft when displayed at that higher physical pixel density. For actual <img> elements, the srcset attribute is generally the more modern, preferred, and more efficient technique for serving resolution-appropriate images, since the browser can choose and download only the one appropriate image variant, rather than a CSS media query approach potentially still downloading an image that ultimately goes unused.

1Understanding Resolution

resolution is typically expressed in dppx, dots per pixel unit, where 1dppx represents a standard-density display and 2dppx represents a high-density display with twice as many physical pixels per CSS pixel in each dimension, like Apple's Retina displays — this is commonly used with min-resolution to conditionally load a higher-resolution background image specifically for high-density screens, where a standard-resolution image would otherwise appear visibly blurry or soft when displayed at that higher physical pixel density. For actual <img> elements, the srcset attribute is generally the more modern, preferred, and more efficient technique for serving resolution-appropriate images, since the browser can choose and download only the one appropriate image variant, rather than a CSS media query approach potentially still downloading an image that ultimately goes unused.

💡

For actual <img> elements specifically, prefer the srcset attribute over a resolution media query for serving different image resolutions — srcset lets the browser choose and download only the one genuinely appropriate image variant, while a CSS-based media query approach can end up downloading an image that ultimately goes unused.

editor.html
.logo {
  background-image: url("logo.png");
}

@media (min-resolution: 2dppx) {
  .logo {
    background-image: url("logo@2x.png");
  }
}
localhost:3000

2Practical Example

Here is a real-world application of Resolution showing how it is used in production CSS code.

editor.html
<img src="photo.jpg" srcset="photo.jpg 1x, photo@2x.jpg 2x" alt="A photo">
localhost:3000

3Best Practices

Follow these guidelines when working with Resolution:

1. Use a resolution media query specifically for background images set via CSS, where srcset, an HTML attribute, isn't applicable

2. Prefer the srcset attribute over a resolution media query for actual <img> elements, since it lets the browser download only the one genuinely needed image variant

3. Provide at minimum a 1x and 2x resolution image variant for background images intended to look crisp on both standard and high-density, Retina-style, displays

⚠️

Tip: For actual <img> elements specifically, prefer the srcset attribute over a resolution media query for serving different image resolutions — srcset lets the browser choose and download only the one genuinely appropriate image variant, while a CSS-based media query approach can end up downloading an image that ultimately goes unused.

editor.html
.logo {
  background-image: url("logo.png");
}

@media (min-resolution: 2dppx) {
  .logo {
    background-image: url("logo@2x.png");
  }
}
localhost:3000

Examples

Example 01Basic Usage
.logo {
  background-image: url("logo.png");
}

@media (min-resolution: 2dppx) {
  .logo {
    background-image: url("logo@2x.png");
  }
}
Example 02Advanced Example
<img src="photo.jpg" srcset="photo.jpg 1x, photo@2x.jpg 2x" alt="A photo">

Best Practices

  • Use a resolution media query specifically for background images set via CSS, where srcset, an HTML attribute, isn't applicable
  • Prefer the srcset attribute over a resolution media query for actual elements, since it lets the browser download only the one genuinely needed image variant
  • Provide at minimum a 1x and 2x resolution image variant for background images intended to look crisp on both standard and high-density, Retina-style, displays

Interview Question

Why is srcset generally considered more efficient than a resolution media query specifically for serving different image resolutions on an <img> element?

Hint: Think about which approach lets the browser make its resolution-appropriate choice before actually downloading any image data, versus after.

A CSS-based resolution media query approach, applied to a background-image property, requires the browser to evaluate CSS rules that reference potentially multiple different image URLs across different media query blocks, and depending on how the CSS is structured, this can sometimes result in the browser downloading an image that a matching media query condition then simply doesn't end up using, wasting that bandwidth entirely. srcset, by contrast, is specifically designed as an HTML-level mechanism that lets the browser evaluate all the offered resolution variants and the current display's actual pixel density together upfront, before initiating any image download at all, then deliberately choose and download only the one single variant that's genuinely appropriate for that specific display, with the browser's own built-in image-loading logic handling this selection natively and efficiently. This upfront-selection-then-single-download approach is exactly why srcset is considered the more modern, purpose-built, and generally more bandwidth-efficient technique specifically for serving resolution-appropriate images on actual <img> elements, reserving the CSS media query approach mainly for background images where srcset, being an HTML attribute, simply doesn't apply.

Exercises

MediumPractice using Resolution in a real scenario.
View Solution
.logo {
  background-image: url("logo.png");
}

@media (min-resolution: 2dppx) {
  .logo {
    background-image: url("logo@2x.png");
  }
}

Frequently Asked Questions

Why is srcset generally considered more efficient than a resolution media query specifically for serving different image resolutions on an <img> element?

A CSS-based resolution media query approach, applied to a background-image property, requires the browser to evaluate CSS rules that reference potentially multiple different image URLs across different media query blocks, and depending on how the CSS is structured, this can sometimes result in the browser downloading an image that a matching media query condition then simply doesn't end up using, wasting that bandwidth entirely. srcset, by contrast, is specifically designed as an HTML-level mechanism that lets the browser evaluate all the offered resolution variants and the current display's actual pixel density together upfront, before initiating any image download at all, then deliberately choose and download only the one single variant that's genuinely appropriate for that specific display, with the browser's own built-in image-loading logic handling this selection natively and efficiently. This upfront-selection-then-single-download approach is exactly why srcset is considered the more modern, purpose-built, and generally more bandwidth-efficient technique specifically for serving resolution-appropriate images on actual elements, reserving the CSS media query approach mainly for background images where srcset, being an HTML attribute, simply doesn't apply.

Related Functions

Media-queriesRefScreenDimensionsBackground-image