Monaen's WikiFull Responsiveness 2017-04-16
Tips
Check the
device pixel ratio
of your device by dev tools1
2
3// open dev tool and change to terminal
window.devicePixelRatio
//1.75How to use
srcset
- A fun article on srcset (Warning: A little bit of NSFW language)
- Examples: srcset with w values and srcset with w values, 50vw, srcset quiz solution
Pixel Densities
How to use
sizes
- srcset with w values
General advice about alt attributes
alt
attributes should be descriptive for important images, like this body surfer. Because body surfing is important, I guess.alt
attributes should be empty for images that are just decorations, like this boiler image. Do you get the joke? It’s a boiler to represent boiler plate code, which is sometimes empty of content.alt
attributes should be set on every image, just like this pig is set on being so darn cute.
Resources
Responding To Screen Capability & View
- Jason Grigsby’s article Responsive Image Audits.
Srcset
Examples:
- srcset with w values
- srcset with w values, 50vw
- A fun article on srcset (Warning: A little bit of NSFW language)
- Device pixel density list
- More information about working with pixel density
- Working with h units
- Wikipedia wallabies
Syntax
- There are two flavors of
srcset
, one usingx
to differentiate between device pixel ratios (DPR), and the other usingw
to describe the image’s width.
Reacting to Device Pixel Ratio
1 | <img src="image_2x.jpg" srcset="image_2x.jpg 2x, image_1x.jpg 1x" alt="a cool image"> |
Set srcset
equal to a comma separated string of filename multiplier
pairs, where each multiplier
must be an integer followed by an x
.
For example, 1x
represents 1x
displays and 2x
represents displays with twice the pixel density, like Apple’s Retina displays.
The browser will download the image that best corresponds to its DPR .
Also, note that there’s a src
attribute as a fallback.
Reacting to Image Width
1 | <img src="image_200.jpg" srcset="image_200.jpg 200w, image_100.jpg 100w" alt="a cool image"> |
Set srcset
equal to a comma separated string of filename widthDescriptor
pairs, where each widthDescriptor
is measured in pixels and must be an integer followed by a w
. Here, the widthDescriptor
gives the natural width of each image file, which enables the browser to choose the most appropriate image to request, depending on viewport size and DPR. (Without the widthDescriptor
, the browser cannot know the width of an image without downloading it!)
Also, note that there’s a src
attribute as a fallback.
srcset with sizes Syntax
Here’s an example:1
2
3
4<img src="images/great_pic_800.jpg"
sizes="(max-width: 400px) 100vw, (min-width: 401px) 50vw"
srcset="images/great_pic_400.jpg 400w, images/great_pic_800.jpg 800w"
alt="great picture">
sizes
consists of comma separated mediaQuery width
pairs. sizes
tells the browser early in the load process that the image will be displayed at some width
when the mediaQuery
is hit.
In fact, if sizes is missing, the browser defaults sizes to 100vw, meaning that it expects the image will display at the full viewport width.
sizes gives the browser one more piece of information to ensure that it downloads the right image file based on the eventual display width of the image. Just to be clear, it does not actually resize the image - that’s what CSS does.
Sizes
Examples:
- In JavaScript you can get the source of an
img
element withcurrentSrc
. - The
sizes
attribute gives the browser information about the display size of an image element – it does not actually cause the image to be resized. That’s done in CSS!
The Picture Element
Example:
Accessibility
The Full Monty
Examples:
Others
- Built-in Browser Support for Responsive Images: Useful
- Download Picturefill here
- Element Queries will work like Media Queries, but with reference to the size of an element’s parent container rather than viewport size. Browser implementation is in progress and in the meantime there’s a polyfill here.
Lots of other <picture>
use cases, examples and code snippets are available here.
Find out more about responsive images from the Responsive Images Community Group.