|
@@ -36,35 +36,65 @@ Note that a space separates `[image]` and `[resize-rule]`, but the resize rule m
|
|
|
To render an image in multiple formats, you can use the `picture` tag:
|
|
|
|
|
|
```html+django
|
|
|
-{% picture page.photo width-400 format-{avif,jpeg} %}
|
|
|
+{% picture page.photo format-{avif,webp,jpeg} width-400 %}
|
|
|
```
|
|
|
|
|
|
-Compared to `image`, this will render a `<picture>` element with one `<source>` element per format. The browser [picks the first format it supports](https://web.dev/learn/design/picture-element/#source), or defaults to a fallback `<img>` element.
|
|
|
+Compared to `image`, this will render a `<picture>` element with a fallback `<img>` within and one `<source>` element per extra format. The browser [picks the first format it supports](https://web.dev/learn/design/picture-element/#source), or defaults to the fallback `<img>` element. For example, the above will render HTML similar to:
|
|
|
|
|
|
-`picture` can also be used with responsive image resize rules.
|
|
|
+```html
|
|
|
+<picture>
|
|
|
+ <source srcset="/media/images/pied-wagtail.width-400.avif" type="image/avif">
|
|
|
+ <source srcset="/media/images/pied-wagtail.width-400.webp" type="image/webp">
|
|
|
+ <img src="/media/images/pied-wagtail.width-400.jpg" alt="A pied Wagtail" width="400" height="300">
|
|
|
+</picture>
|
|
|
+```
|
|
|
+
|
|
|
+In this case, if the browser supports the [AVIF](https://en.wikipedia.org/wiki/AVIF) format it will load the AVIF file. Otherwise, if the browser supports the [WebP](https://en.wikipedia.org/wiki/WebP) format, it will try to load the WebP file. If none of those formats are supported, the browser will load the JPEG image. The order of the provided formats isn’t configurable – Wagtail will always output source elements in the following order: AVIF, WebP, JPEG, PNG, GIF. This ensures the most optimized format is provided whenever possible.
|
|
|
+
|
|
|
+The `picture` tag can also be used with multiple image resize rules to generate responsive images.
|
|
|
|
|
|
(responsive_images)=
|
|
|
|
|
|
## Responsive images
|
|
|
|
|
|
-Wagtail provides `picture` and `srcset_image` template tags which can generate an `<img>` tag with a `srcset` attribute. This allows browsers to select the most appropriate image file to load based on [responsive image rules](https://developer.mozilla.org/en-US/docs/Learn/HTML/Multimedia_and_embedding/Responsive_images).
|
|
|
+Wagtail provides `picture` and `srcset_image` template tags which can generate image elements with `srcset` attributes. This allows browsers to select the most appropriate image file to load based on [responsive image rules](https://developer.mozilla.org/en-US/docs/Learn/HTML/Multimedia_and_embedding/Responsive_images).
|
|
|
|
|
|
-The syntax for `srcset_image` is the same as `image, with two exceptions:
|
|
|
+The syntax for `srcset_image` is the same as `image`, with two exceptions:
|
|
|
|
|
|
```html+django
|
|
|
-{% srcset_image [image] [resize-rule-with-brace-expansion] sizes="100vw" %}
|
|
|
+{% srcset_image [image] [resize-rule-with-brace-expansion] sizes="[my source sizes]" %}
|
|
|
```
|
|
|
|
|
|
-- The resize rule should be provided with multiple sizes in a brace-expansion pattern, like `width-{200,400}`. This will generate the `srcset` attribute, with as many URLs as there are sizes defined in the resize rule.
|
|
|
+- The resize rule should be provided with multiple sizes in a brace-expansion pattern, like `width-{200,400}`. This will generate the `srcset` attribute, with as many URLs as there are sizes defined in the resize rule, and one width descriptor per URL. The first provided size will always be used as the `src` attribute, and define the image’s width and height attributes, as a fallback.
|
|
|
- The [`sizes`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#sizes) attribute is essential. This tells the browser how large the image will be displayed on the page, so that it can select the most appropriate image to load.
|
|
|
|
|
|
-Here is an example with the `picture` tag:
|
|
|
+Here is an example of `srcset_image` in action, generating an `srcset` attribute:
|
|
|
+
|
|
|
+```html+django
|
|
|
+{% srcset_image page.photo width-{400,800} sizes="(max-width: 600px) 400px, 80vw" %}
|
|
|
+```
|
|
|
+
|
|
|
+This outputs:
|
|
|
+
|
|
|
+```html
|
|
|
+<img srcset="/media/images/pied-wagtail.width-400.jpg 400w, /media/images/pied-wagtail.width-800.jpg 800w" src="/media/images/pied-wagtail.width-400.jpg" alt="A pied Wagtail" sizes="(max-width: 600px) 400px, 80vw" width="400" height="300">
|
|
|
+```
|
|
|
+
|
|
|
+And here is an example with the `picture` tag:
|
|
|
|
|
|
```html+django
|
|
|
-{% picture page.photo fill-{512x100,1024x200} format-{avif,jpeg} sizes="100vw" %}
|
|
|
+{% picture page.photo format-{avif,webp,jpeg} width-{400,800} sizes="80vw" %}
|
|
|
```
|
|
|
|
|
|
-This will generate a `<picture>` element with one `<source>` for AVIF and one fallback `<img>` with JPEG. Both the source and fallback image will have the `sizes` attribute, and a `srcset` with two URLs each.
|
|
|
+This outputs:
|
|
|
+
|
|
|
+```html
|
|
|
+<picture>
|
|
|
+ <source sizes="80vw" srcset="/media/images/pied-wagtail.width-400.avif 400w, /media/images/pied-wagtail.width-800.avif 800w" type="image/avif">
|
|
|
+ <source sizes="80vw" srcset="/media/images/pied-wagtail.width-400.webp 400w, /media/images/pied-wagtail.width-800.webp 800w" type="image/webp">
|
|
|
+ <img sizes="80vw" srcset="/media/images/pied-wagtail.width-400.jpg 400w, /media/images/pied-wagtail.width-800.jpg 800w" src="/media/images/pied-wagtail.width-400.jpg" alt="A pied Wagtail" width="400" height="300">
|
|
|
+</picture>
|
|
|
+```
|
|
|
|
|
|
(available_resizing_methods)=
|
|
|
|