picture
element, designed to solve a number of problems arising in the development of responsive designs. Let's take a closer look at it.picture
element solves the following tasks facing the developer of responsive web applications (I will use the classification proposed by pepelsbey at a recent frontend conference): <picture> <source srcset="< URL c >" [sizes="< >"] [media="<>"] [type="<mime/type>"] > <source ...> ... <img src="image.jpg" alt="My image" [srcset="< URL >"] [sizes="< >"]> </picture>
picture
element does not render any content, but is merely a reference container for the img
tag attached to it. <img src="image.jpg" alt="My image" [srcset="< URL >"] [sizes="< >"]>
picture
support is enabled by setting dom.image.picture.enable
in about: config), or Chrome Canary , or Opera Developer . On a mobile device, you can test a new element in Chrome Beta. <img src="images/400.jpg" srcset="images/800.jpg 2x, images/1200.jpg 3x" width="400" height="300" >
2x
and 3x
are pixel density descriptors , they tell the browser that these pictures were prepared for this density, you can use if you want. Please note that they do not force the browser to use these pictures, but only prompt it. The final decision remains with him depending on other conditions, for example, the current connection.src
attribute in this case serves as the source of the image for a density of <2 and a foldback in case the browser does not support the new element. <img src="images/400.jpg" srcset=" 400w, images/800.jpg 800w, images/1200.jpg 1200w" sizes="(min-width: 700px) 75vw, 100vw" >
400w, 800w, 1200w
are width descriptors , they prompt the browser how wide the image is at a given URL and based on this information the browser decides which image is best for the current situation. As in the case of retina information is advisory in nature and the final decision what image to load remains for the browser.sizes
attribute lists image sizes for all control points in our design. Control points are set in the form of ordinary media expressions, the browser takes the first one, which returns True and does not consider the chain any further. For the width, a new unit of length vw is used , which returns a value in percent of the viewport width.sizes="100vw"
. And for more complex designs, you can use the calc()
CSS function, for examplesizes="(max-width: 30em) 100vw, (max-width: 50em) 50vw, calc(33vw - 100px)"
sizes
, the browser always chooses the largest image. In combination with the width
does not work, although it would seem a logical combination and a number of other glitches. Perhaps this is a feature of early implementation.picture
, it is time for him to join the game too.video
or audio
tags. <picture> <source srcset="images/400.webp" type="image/webp"> <img src="images/400.jpg" width="400" height="300" > </picture>
src
attribute.media
attribute will help us with this task. <picture> <source media="(min-width: 900px)" srcset="images/original.jpg 1200w" sizes="100vw"> <source media="(min-width: 700px)" srcset="images/800crop.jpg 800w" sizes="100vw"> <img srcset="images/400crop.jpg 400w" sizes="100vw" > </picture>
media
attribute, we specify a media
expression, in which we will change the original image and, unlike the previous examples, the browser will be obliged to follow it. Also note how framing here is combined with adaptability to stretch the image to its full width. <picture> <source media="(min-width: 1280px)" sizes="50vw" srcset="opera-fullshot-200.webp 200w, opera-fullshot-400.webp 400w, opera-fullshot-800.webp 800w, opera-fullshot-1200.webp 1200w, opera-fullshot-1600.webp 1600w, opera-fullshot-2000.webp 2000w" type="image/webp"> <source sizes="(min-width: 640px) 60vw, 100vw" srcset="opera-closeup-200.webp 200w, opera-closeup-400.webp 400w, opera-closeup-800.webp 800w, opera-closeup-1200.webp 1200w, opera-closeup-1600.webp 1600w, opera-closeup-2000.webp 2000w" type="image/webp"> <source media="(min-width: 1280px)" sizes="50vw" srcset="opera-fullshot-200.jpg 200w, opera-fullshot-400.jpg 400w, opera-fullshot-800.jpg 800w, opera-fullshot-1200.jpg 1200w, opera-fullshot-1600.jpg 1800w, opera-fullshot-2000.jpg 2000w"> <img src="opera-closeup-400.jpg" alt="The Oslo Opera House" sizes="(min-width: 640px) 60vw, 100vw" srcset="opera-closeup-200.jpg 200w, 400w, opera-closeup-800.jpg 800w, opera-closeup-1200.jpg 1200w, opera-closeup-1600.jpg 1600w, opera-closeup-2000.jpg 2000w"> </picture>
Source: https://habr.com/ru/post/237991/
All Articles