# HTML Picture element with PNG and WEBP images and lazy loading option

**URL:** <https://discourse.gohugo.io/t/html-picture-element-with-png-and-webp-images-and-lazy-loading-option/29566>\
**Category:** support\
**Created:** [November 22, 2020, 6:11am UTC](https://discourse.gohugo.io/t/html-picture-element-with-png-and-webp-images-and-lazy-loading-option/29566 "2020-11-22T06:11:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mickel1982](https://avatars.discourse-cdn.com/v4/letter/m/f14d63/32.png) [@mickel1982](https://discourse.gohugo.io/u/mickel1982)\
**Post date:** [November 22, 2020, 6:11am UTC](https://discourse.gohugo.io/t/html-picture-element-with-png-and-webp-images-and-lazy-loading-option/29566/1 "2020-11-22T06:11:01Z")

</div>

I would like an opinion about my shortcodes for the element with WEBP and PNG images.

Code on pages:

```
{{< img src="/images/image" alt="Example Image" width="100" height="100" >}}

{{< img-lazy src="/images/image" alt="Example Image" width="100" height="100" >}}

```

Shortcodes:

img.html:

```
<picture>
<source srcset="{{ .Get "src" }}.webp" type="image/webp">
<source srcset="{{ .Get "src" }}.png" type="image/png">
<img src="{{ .Get "src" }}.png" alt="{{ .Get "alt"}}" width="{{ .Get "width"}}" height="{{ .Get "height"}}">
</picture>

```

img-lazy.html:

```
<picture>
<source srcset="{{ .Get "src" }}.webp" type="image/webp">
<source srcset="{{ .Get "src" }}.png" type="image/png">
<img class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=" data-src="{{ .Get "src" }}.png" alt="{{ .Get "alt"}}" width="{{ .Get "width"}}" height="{{ .Get "height"}}">
</picture>

```

It works. But I want an opinion from someone else. Maybe someone can improve it.

---

<div class="post-metadata">

**Author:** ![ju52](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/ju52/32/6749_2.png) [@ju52](https://discourse.gohugo.io/u/ju52)\
**Post date:** [November 22, 2020, 10:00am UTC](https://discourse.gohugo.io/t/html-picture-element-with-png-and-webp-images-and-lazy-loading-option/29566/2 "2020-11-22T10:00:18Z")

</div>

I tryed to generate different image sizes for responsive layouts. Hugo hardcore 😉

[HugoSample/render-image.html at master · gj52/HugoSample (github.com)](https://github.com/gj52/HugoSample/blob/master/themes/photon/layouts/_default/_markup/render-image.html)

I use the standard image markdown format `![](images/image.gif "sample image")`

---

<div class="post-metadata">

**Author:** ![mickel1982](https://avatars.discourse-cdn.com/v4/letter/m/f14d63/32.png) [@mickel1982](https://discourse.gohugo.io/u/mickel1982)\
**Post date:** [November 22, 2020, 11:58am UTC](https://discourse.gohugo.io/t/html-picture-element-with-png-and-webp-images-and-lazy-loading-option/29566/3 "2020-11-22T11:58:56Z")

</div>

Ok. I’m using HTML. Not markdown.

My website has more than 180 pages, and a lot of images (screenshots), so I don’t want to change too much at once.

And also, my website contains screenshots for tutorials, so it’s not always a good idea to resize them, otherwise people may not see the info clearly.

All images are responsive and I’m also using lazy loading. LazySizes script.

But now I also want to add WEBP images to further optimize speed. Especially for the images above the fold. So the images that don’t lazy load.

---

<div class="post-metadata">

**Author:** ![GothSeiDank](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/gothseidank/32/13028_2.png) [@GothSeiDank](https://discourse.gohugo.io/u/GothSeiDank)\
**Post date:** [November 22, 2020, 1:44pm UTC](https://discourse.gohugo.io/t/html-picture-element-with-png-and-webp-images-and-lazy-loading-option/29566/4 "2020-11-22T13:44:38Z")

</div>

Why are you using a script to lazy load? All modern browsers support

```auto
<img src="foo.png" loading="lazy">

```

This works in all modern browsers, screw IE11. For images in markdown utilizing webp and png,  
I am using this shortcode:

```auto
<!-- Usage {< inline_image "image" "alt text" "width" "height" "additional classes" >} -->
{{ $image := .Get 0 }} <!-- Name of the image -->
{{ $alt := .Get 1 }}<!-- alt-text -->
{{ $width := .Get 2 }}
{{ $height := .Get 3 }}
{{ $classes := .Get 4 }}<!-- classes for figure -->

<div class="columns">
    <div class="column is-half is-offset-one-quarter">
        <div class="media">
            <figure class="brdr-yayellow image {{$classes}}">
                <picture>
                    <source type="image/webp" srcset="{{$image}}.webp">
                    <source type="image/png" srcset="{{$image}}.png">
                    <img
                         src="{{$image}}.png"
                         alt="{{$alt}}"
                         height="{{$height}}"
                         width="{{$width}}"
                         loading="lazy"
                         class="vert-middle"
                    >
                </picture>
                <figcaption>{{$alt}}</figcaption>
            </figure>
        </div>
    </div>
</div>

```

You will of course need to change the css classes to your own.

Don’t forget to utilize prefetch and preconnect as well, to further speed up your page.  
I have written an article about this just today: [https://tuxstash.de/blog/optimizing-my-blog/page/3/](https://tuxstash.de/blog/optimizing-my-blog/page/3/)

---

<div class="post-metadata">

**Author:** ![alexandros](https://avatars.discourse-cdn.com/v4/letter/a/ecc23a/32.png) [@alexandros](https://discourse.gohugo.io/u/alexandros)\
**Post date:** [November 22, 2020, 2:21pm UTC](https://discourse.gohugo.io/t/html-picture-element-with-png-and-webp-images-and-lazy-loading-option/29566/5 "2020-11-22T14:21:35Z")

</div>

> [@GothSeiDank](#):
>
> Why are you using a script to lazy load? All modern browsers support

Actually this is not the case as per [Can I Use](https://caniuse.com/loading-lazy-attr).

iOS Safari and Safari desktop require a user action in advanced settings, to enable lazy loading.

Until Apple decides to release browser versions with lazy loading enabled by default I think that it is useful to use a script for lazy loading.

---

<div class="post-metadata">

**Author:** ![davidsneighbour](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/davidsneighbour/32/24507_2.png) [@davidsneighbour](https://discourse.gohugo.io/u/davidsneighbour)\
**Post date:** [November 22, 2020, 2:50pm UTC](https://discourse.gohugo.io/t/html-picture-element-with-png-and-webp-images-and-lazy-loading-option/29566/6 "2020-11-22T14:50:38Z")

</div>

yah, sadly “all modern browsers” is coupled with 95% distribution 😉

![Selection_073](https://canada1.discourse-cdn.com/flex036/uploads/gohugo/original/2X/0/0c5949778f5d37a35f80a259f719eed01b9474dc.png)
