You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: questions/css-questions.md
+26-4
Original file line number
Diff line number
Diff line change
@@ -450,15 +450,37 @@ Adaptive design is more like the modern definition of progressive enhancement. I
450
450
451
451
### Have you ever worked with retina graphics? If so, when and what techniques did you use?
452
452
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
+
<divresponsive-background-image>
463
+
<imgsrc="/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
+
```
454
474
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).
456
476
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.
0 commit comments