html - Understanding responsive images and device pixel ratio -


i'm trying implement responsive images on website using srcset , sizes. when testing on computer looks , works great. image downloaded next-largest image when compared current page width (page width of 500px retrieve 720px wide image). here's i'm using:

<img src="image-240w.jpg"     srcset="image-1140w.jpg 1140w,         image-940w.jpg 940w,         image-720w.jpg 720w,         image-480w.jpg 480w,         image-240w.jpg 240w"     sizes="(max-width: 1140px) 100vw, 1140px"> 

on phone confused. have nexus 5x 1920x1080 resolution screen device pixel ratio of 2.5. viewport meta added page (<meta name="viewport" content="width=device-width, initial-scale=1">) media queries respond expected--as if device on 400px wide (1080px/4 = 432px). support example below. 480px wide image which, expected, hangs past edge of page 48px. example b image 1140px wide larger 432px wide viewport. example c result of img tag above (and i'm aiming for).

comparison

at first, pleased worked. however, when viewing developer tools can see img tag srcset downloaded 1140px wide image. seems me 480px wide image (which barely larger 432px wide viewport) should have been selected. instead, image downloaded of 1080px wide screen.

why media queries respond expect them (acting if screen 432px wide) responsive images care image size matches real screen resolution? must have 2.5 device pixel ratio don't how.

my goal on device 480px image, not largest one. how accomplish this?

edit

or perhaps desires incorrect , browser doing should doing. explain why, when 480px wide image hanging off edge of screen, should still pick 1140px wide image.

answer/proof

as pointed out in answer below, the device doing should doing. prove point, here device (432 css pixels 1080 device pixels) displaying 480px , 1140px image. although 480px image matches css pixels, terribly blurry when compared 1140px image matches device pixels. so, device knows best , although expected pick smaller image, knew needed larger image (from responsive srcset , sizes) present image correctly.

480vs1140

the answer is: yes has device pixel ratio. , yes, device doing right.

the image shown correctly because if don't override image dimension css sizes attribute used (in case "100vw", ~"100%") (if change sizes "50vw" around "50%" width).

the key part there different concepts of pixels.

normal media queries (min-width, max-width...) measured in called layout pixels (also css pixels). these pixels abstraction of real physical pixels (device pixels) have satisfying , reliable pixel unit layout stuff (css) retina screens (and low resolution screens). using physical pixels layout pixels mean on small high resolution devices very, small layout objects. in css world pixel means layout pixel. calculated device pixel / device pixel ration.

however, in case of images how many pixels device has. if has 1000px pixels screen width , image displayed @ 100vw, makes sense load 1000 pixel width image satisfy full quality of screen. (= each physical screen pixel gets real image pixel assigned.)

this makes sense if these 1000 physical pixels measured in 360 layout pixels.

see also: a pixel not pixel.


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -