Skip to content

Commit 5542a28

Browse files
erralstevepiercy
andauthored
document img-srcsets (#1951)
* document img-srcsets * Update docs/classic-ui/images.md Co-authored-by: Steve Piercy <[email protected]> --------- Co-authored-by: Steve Piercy <[email protected]>
1 parent 1f32954 commit 5542a28

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

docs/classic-ui/images.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,39 @@ Please note that this example has the `resolve_uid_and_caption` filter disabled
501501
The real `src` URLs look more like `http://localhost:8080/Plone50/dsc04791.jpg/@@images/778f9c06-36b0-485d-ab80-12c623dc4bc3.jpeg`.
502502
```
503503

504+
### All image scales in the srcset
505+
506+
Modern browsers can download appropriate sized images depending on their viewport width when URLs and widths are correctly provided in an attribute called `srcset` and the viewport width is provided in the attribute `sizes`.
507+
508+
Examine the following {term}`TALES` example.
509+
510+
```html
511+
<img tal:define="images context/@@images;"
512+
tal:replace="structure python:images.srcset(sizes='90vw')" />
513+
```
514+
515+
This example will render the HTML `img` tag with the URLs of all scales configured in Plone, calculating the width of each of the scales, and will add the `sizes="90vw"` attribute.
516+
The `sizes` attribute instructs the browser to retrieve the image that best fits in 90% of the current viewport width.
517+
518+
This means that the appropriate scaled image will be downloaded and displayed.
519+
520+
This also means that the developer does not need to worry about creating a specific scale.
521+
Instead they only need to provide the correct media query to signal the required width.
522+
523+
The `scrset` method of the `@@images` view also takes all other parameters that are HTML attributes of the `img` tag, such as `title`, `alt` or `loading`.
524+
525+
```html
526+
<img tal:define="images context/@@images;"
527+
tal:replace="structure python:images.srcset(sizes='90vw',
528+
alt='This is the alternative text',
529+
loading='lazy',
530+
css_class='rounded-img')" />
531+
```
532+
533+
```{note}
534+
While using this approach may be useful for projects, using it in reusable add-ons is not recommended, because it may require overriding it to your needs in a project.
535+
Use configurable picture variants instead.
536+
```
504537
## Image scales from catalog brain
505538

506539
For all `NamedBlobImage` fields, we can get existing scale information directly from the catalog brain.

0 commit comments

Comments
 (0)