Skip to content

Commit 0f17873

Browse files
jdvivaryangshun
authored andcommitted
Improve question about "retina graphics" (#112)
* Improve question about "retina graphics" * Update css-questions.md
1 parent 01b98e5 commit 0f17873

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

questions/css-questions.md

+26-4
Original file line numberDiff line numberDiff line change
@@ -450,15 +450,37 @@ Adaptive design is more like the modern definition of progressive enhancement. I
450450

451451
### Have you ever worked with retina graphics? If so, when and what techniques did you use?
452452

453-
I tend to use higher resolution graphics (twice the display size) to handle retina display. The better way would be to use a media query like `@media only screen and (min-device-pixel-ratio: 2) { ... }` and change the `background-image`.
453+
_Retina_ is just a marketing term to refer to high resolution screens with a pixel ratio bigger than 1. The key thing to know is that using a pixel ratio means these displays are emulating a lower resolution screen in order to show elements with the same size. Nowadays we consider all mobile devices _retina_ defacto displays.
454+
455+
Browsers by default render DOM elements according to the device resolution, except for images.
456+
457+
In order to have crisp, good-looking graphics that make the best of retina displays we need to use high resolution images whenever possible. However using always the highest resolution images will have an impact on performance as more bytes will need to be sent over the wire.
458+
459+
To overcome this problem, we can use responsive images, as specified in HTML5. It requires making available different resolution files of the same image to the browser and let it decide which image is best, using the html attribute `srcset` and optionally `sizes`, for instance:
460+
461+
```html
462+
<div responsive-background-image>
463+
<img src="/images/test-1600.jpg"
464+
sizes="
465+
(min-width: 768px) 50vw,
466+
(min-width: 1024px) 66vw,
467+
100vw"
468+
srcset="
469+
/images/test-400.jpg 400w,
470+
/images/test-800.jpg 800w,
471+
/images/test-1200.jpg 1200w">
472+
</div>
473+
```
454474

455-
For icons, I would also opt to use SVGs and icon fonts where possible, as they render very crisply regardless of resolution.
475+
It is important to note that browsers which don't support HTML5's `srcset` (i.e. IE11) will ignore it and use `src` instead. If we really need to support IE11 and we want to provide this feature for performance reasons, we can use a JavaScript polyfill, e.g. Picturefill (link in the references).
456476

457-
Another method would be to use JavaScript to replace the `<img>` `src` attribute with higher resolution versions after checking the `window.devicePixelRatio` value.
477+
For icons, I would also opt to use SVGs and icon fonts where possible, as they render very crisply regardless of resolution.
458478

459479
###### References
460480

461-
* https://www.sitepoint.com/css-techniques-for-retina-displays/
481+
* https://css-tricks.com/responsive-images-youre-just-changing-resolutions-use-srcset/
482+
* http://scottjehl.github.io/picturefill/
483+
* https://aclaes.com/responsive-background-images-with-srcset-and-sizes/
462484

463485
[[] Back to top](#css-questions)
464486

0 commit comments

Comments
 (0)