
See the video on YouTube at goo.gl/JeV8J
Good
Bad
(px)
viewport
& zoomingcm
, in
and even em
px
)
Nexus 7
216dpi
1.3x
Nexus S
235dpi
1.5x
Chromebook Pixel
239dpi
2x
Nexus 4
316dpi
2x
Galaxy S IV
441dpi
3x
17"
(28 ÷ 17) × 96 ≈ 160 dpi
316 dpi ÷ 160 dpi ≈ 2x
window.devicePixelRatio
Nexus 4 is 768×1280 @ 316ppi.
<meta name="viewport" content="width=320">
Allows zoom (recommended):
<meta name="viewport" content="width=device-width, initial-scale=1">
Disallows zoom (use sparingly):
<meta name="viewport" content="width=device-width, maximum-scale=1">
Modern browsers are starting to do sub-pixel precise layout.
So does this work?
.thinnest-border { border-width: 1px; } @media (min-resolution: 2dppx) { .thinnest-border { border-width: 0.5px; } }
No, stick to 1 CSS pixel as minimum width (or test exhaustively).
Mobile optimized site
width=device-width
Desktop site
No viewport set
A phone user has zoomed in on the left-hand side of a wide column of text. The rest of the column is off-screen to the right. The user will have to pan from side-to-side for every line of text they read.
Hashtag tousled DIY shoreditch. Marfa kale chips echo park raw denim. Banksy gastropub lo-fi, tattooed jean shorts ennui pickled deep we farm-to-table gentrify bespoke biodiesel mixtape master cleanse you probably haven't heard of them. Fab plaid bespoke gentrify put a bird on it wes anderson, 3 wolf moon vinyl salute actually tofu messenger bag keffiyeh. Bespoke fingerstache bushwick, odd future pitchfork echo park banh mi pork belly. Etsy put a bird on it vegan keytar, high life organic fingerstache bespoke art party mumblecore. Church-key lomo 90's, before they sold out odd future master cleanse vegan locavore.
Last Resort, opt out of autosizing:
.dont-autosize { -webkit-text-size-adjust: 100%; /* Mobile Safari */ -moz-text-size-adjust: none; /* Firefox for Android */ -ms-text-size-adjust: none; /* IE Mobile */ } .dont-autosize, .dont-autosize * { max-height: 1000000px; /* Chrome for Android */ }
Üñíçø∂ê Characters
Icon Fonts
.logo { width: 100px; height: 100px; background: url(low.png); } @media only screen and (-webkit-min-device-pixel-ratio: 1.5), only screen and (-o-min-device-pixel-ratio: 3/2), only screen and (min-resolution: 144dpi), only screen and (min-resolution: 1.5dppx) { .logo { background: url(high.png); background-size: 100px 100px; } }
Highly compressed 2x images look better and
are smaller in file size.
image-set
.logo { background-image: url(fallback.png); background-image: -webkit-image-set( url(low.png) 1x, url(medium.png) 1.5x, url(high.png) 2x); background-image: image-set( url(low.png) 1x, url(medium.png) 1.5x, url(high.png) 2x); }
img srcset
<img alt="my awesome image" src="banner.jpg" srcset="banner2x.jpg 2x, banner-phone.jpg 640w, banner-phone2x.jpg 640w 2x">
canvas
canvas
backing storecanvas
300px
canvas backing store
300px × 2 (device pixel ratio) = 600px
canvas
elementvar ctx = canvas.getContext('2d'); var ratio = window.devicePixelRatio if (ratio > 1) { canvas.style.width = canvas.width + "px"; canvas.style.height = canvas.height + "px"; canvas.width = canvas.width * ratio; canvas.height = canvas.height * ratio; // Now scale the context to undo our changes to the canvas coordinate system. ctx.scale(ratio, ratio); }
/ (ctx.webkitBackingStorePixelRatio || 1);
webkitBackingStorePixelRatio
will always return 1
except on desktop Safari on a Retina MacBook, which returns 2
. For now.
<link rel="shortcut icon" href="path/to/favicon.ico"> <link rel="icon" href="path/to/favicon.png">
width=device-width
means you only have to care about device independent pixelswidth=device-width
, or if you use a fixed width, you're in a world of hurt.@media
queries to specify appropriate background imagescanvas
images, beware of webkitBackingStorePixelRatio
Video: http://goo.gl/JeV8J
Slides: http://goo.gl/j5Z5W