# Kirby3-srcset with caption

**URL:** https://forum.getkirby.com/t/kirby3-srcset-with-caption/15412
**Category:** Questions
**Tags:** v3
**Created:** [August 27, 2019, 9:18am UTC](https://forum.getkirby.com/t/kirby3-srcset-with-caption/15412 "2019-08-27T09:18:01Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![Oziris](https://avatars.discourse-cdn.com/v4/letter/o/df788c/32.png) [@Oziris](https://forum.getkirby.com/u/Oziris)
#### Post date: [August 27, 2019, 9:18am UTC](https://forum.getkirby.com/t/kirby3-srcset-with-caption/15412/1 "2019-08-27T09:18:01Z")

</div>

Hey,  
I’m using the great plugin from @bnomei [kirby3-srcset](https://github.com/bnomei/kirby3-srcset). But this plugin doesn’t allow to set a caption to the image. A good practice would be to wrap the image inside a `figure` and a `figcaption` for the caption text. How do you think this could be done with the new Html:: method ?

```
 'tags' => [
        'srcset' => [
            // https://getkirby.com/docs/reference/text/kirbytags/image
            'attr' => ['caption', /* The caption attr */ 'preset', 'lazy', 'prefix', 'class', 'imgclass', 'link', 'linkclass', 'target', 'rel', 'snippet'],
            'html' => function ($tag) {
                // TODO: move code to class
                try { 
                    $file = Kirby::instance()->file($tag->value, $tag->parent());
                    if ($file) {
                        $preset = (string) $tag->preset;
                        if (\Kirby\Toolkit\Str::contains($preset, ' ') || \Kirby\Toolkit\Str::contains($preset, ',')) {
                            $preset = str_replace(['[', ']', ',', ' ', 'px'], ['', '', ' ', ' ', ''], $preset);
                            $preset = array_map(function ($v) {
                                return trim($v);
                            }, explode(' ', $preset));
                        }
                        $prefix = (string) $tag->prefix;
                        $class = $tag->class ? trim($tag->class) : null;
                        $imgclass = $tag->imgclass ? trim($tag->imgclass) : null;
                        $snippet = $tag->snippet ? trim($tag->snippet) : 'plugin-srcset-img';
                        $srcset = \Bnomei\Srcset::srcset($file, $preset, boolval($tag->lazy), $prefix, $class, $imgclass, $snippet);
                        if ($tag->link) {
                            $attr = [
                                'href' => trim($tag->link),
                            ];
                            if ($tag->linkclass) {
                                $attr['class'] = trim($tag->linkclass);
                            }
                            if ($tag->target) {
                                $attr['target'] = trim($tag->target);
                            }
                            if ($tag->rel) {
                                $attr['rel'] = trim($tag->rel);
                            }
                            // wrap $srcset in array to avoid encoding
                            // https://github.com/getkirby/kirby/blob/master/src/Toolkit/Html.php#L367
                            return \Kirby\Toolkit\Html::tag('a', [$srcset], $attr);
                            
                        } else {
                            return $srcset . PHP_EOL;
                        }
                    }
                    return '';
                } catch (Exception $ex) {
                    return $ex->getMessage();
                }
            },
        ],
    ],

```

---

<div class="post-metadata">

### Author: ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)
#### Post date: [August 27, 2019, 9:28am UTC](https://forum.getkirby.com/t/kirby3-srcset-with-caption/15412/2 "2019-08-27T09:28:02Z")

</div>

> [@Oziris](#):
>
> A good practice would be to wrap the image inside a `figure`

According to the readme, you can set the `figure` option to true or false and since the plugin inherits the original image tag attributes, there should be a caption attribute?

---

<div class="post-metadata">

### Author: ![Oziris](https://avatars.discourse-cdn.com/v4/letter/o/df788c/32.png) [@Oziris](https://forum.getkirby.com/u/Oziris)
#### Post date: [August 27, 2019, 9:57am UTC](https://forum.getkirby.com/t/kirby3-srcset-with-caption/15412/3 "2019-08-27T09:57:42Z")

</div>

Oh yes, the author has made some changes 12 hours ago, I didn’t noticed. It’s working !

Sorry for the inconvenience, and once again : congratulations for this wonderful plugin.

---

<div class="post-metadata">

### Author: ![Oziris](https://avatars.discourse-cdn.com/v4/letter/o/df788c/32.png) [@Oziris](https://forum.getkirby.com/u/Oziris)
#### Post date: [August 27, 2019, 11:43am UTC](https://forum.getkirby.com/t/kirby3-srcset-with-caption/15412/4 "2019-08-27T11:43:15Z")

</div>

But… 😉 I don’t understand why any lazyloading plugins are not using the “unknown height” technique as I mentionned in a [previous thread](https://forum.getkirby.com/t/lazyload-kirbytag-with-unknown-image-height/15214).  
Every time I have to fix it by myself to avoid the overlapping/flashing effect on load, and specially needed on a masonry layout…

With your help, I could fix it on Kirby 2 by overriding the default kirbytag, but here with this plugin, it’s a new challenge that comes up

---

<div class="post-metadata">

### Author: ![bnomei](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bnomei/32/775_2.png) [@bnomei](https://forum.getkirby.com/u/bnomei)
#### Post date: [August 27, 2019, 3:56pm UTC](https://forum.getkirby.com/t/kirby3-srcset-with-caption/15412/5 "2019-08-27T15:56:42Z")

</div>

thanks for using my plugin and even the still un-announced v3. 😝

i could add the possibility to set the style attribute but personally i think using it is bad practice so i will not. but here is my solution for your problem.

add an basic ratio css class to your css and apply it to every figure. that is setting the `class` not the `imgclass` property.

```css
.ratio-container {
    position: relative;
}
.ratio-container:after {
    content: '';
    display: block;
    height: 0;
    width: 100%;
    /* 16:9 = 56.25% = calc(9 / 16 * 100%) */
    padding-bottom: 42.86%; /*default: pick whatever is best for you*/
}
.ratio-container > * {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: block;
}

```

further more use sass mixin or do it by hand to generate **100** padding-bottom variants. like these

```css
.ratio-49.ratio-container:after {
    padding-bottom: 49%;
}
.ratio-50.ratio-container:after {
    padding-bottom: 50%; 
}
.ratio-51.ratio-container:after {
    padding-bottom: 51%;
}

```

when building you srcsets from images apply the needed ratio padding-bottom variant as well as the container.

```php
$classesForFigure = 'ratio-container ratio-' . round($image->dimensions()->ratio() * 100); 

```

edited: you probably will not need 100. maybe a range from 30 to 70 is good enough.

source: **tip-specifying-image-dimensions-minimizing-reflows-and-avoiding-page-jumps**

> **[aFarkas/lazysizes](https://github.com/aFarkas/lazysizes#tip-specifying-image-dimensions-minimizing-reflows-and-avoiding-page-jumps)**
>
> High performance and SEO friendly lazy loader for images (responsive and normal), iframes and more, that detects any visibility changes triggered through user interaction, CSS or JavaScript without...

---

<div class="post-metadata">

### Author: ![bnomei](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bnomei/32/775_2.png) [@bnomei](https://forum.getkirby.com/u/bnomei)
#### Post date: [August 27, 2019, 4:25pm UTC](https://forum.getkirby.com/t/kirby3-srcset-with-caption/15412/6 "2019-08-27T16:25:14Z")

</div>

i am aware that you use the same technique before as linked in the k2 version thread but without the pre-generated ratio variants. i summed it up like this so i can link it from the readme of the plugins as well.

---

<div class="post-metadata">

### Author: ![Oziris](https://avatars.discourse-cdn.com/v4/letter/o/df788c/32.png) [@Oziris](https://forum.getkirby.com/u/Oziris)
#### Post date: [August 27, 2019, 5:01pm UTC](https://forum.getkirby.com/t/kirby3-srcset-with-caption/15412/7 "2019-08-27T17:01:53Z")

</div>

Haha thank you very much, I did’nt know this smart method with pseudo :after. But this looks a bit hacky for me, and I still think that it would be easier to use the ratio with padding style directly inside a div container.  
The kirbytag could work as an all-in technique without additional style.

Note : to work, we just have to create an absolute div style wrapping the image, and set the ratio padding to the image. For me it’s a damn challenge to custom your code, for you it’s surely fingers in the nose 😉  
Let’s say something like `(lazysrcset: myfile.jpg ratio:yes)` and the magic is done !

---

<div class="post-metadata">

### Author: ![bnomei](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bnomei/32/775_2.png) [@bnomei](https://forum.getkirby.com/u/bnomei)
#### Post date: [August 27, 2019, 7:47pm UTC](https://forum.getkirby.com/t/kirby3-srcset-with-caption/15412/8 "2019-08-27T19:47:15Z")

</div>

the css creates a pseudo block using `after` which has the padding. the image is just stretched to fill the space the pseudo element creates. i do not think its hacky – just like the name implies `after` is an pseudo element and we need space where there was none.  
solving it by creating another div does not make it better. you still have to stretch the image to fill the div. so its just one div more.

but you have a valid point. all this can not be done from the kirbytag. one would need php for that. i will think about that and report back.

---

<div class="post-metadata">

### Author: ![bnomei](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bnomei/32/775_2.png) [@bnomei](https://forum.getkirby.com/u/bnomei)
#### Post date: [August 27, 2019, 8:02pm UTC](https://forum.getkirby.com/t/kirby3-srcset-with-caption/15412/9 "2019-08-27T20:02:09Z")

</div>

this is why i do not like using the style attribute – it is the `emperor` himself: [https://stuffandnonsense.co.uk/archives/css\_specificity\_wars.html](https://stuffandnonsense.co.uk/archives/css_specificity_wars.html)

😉

---

<div class="post-metadata">

### Author: ![bnomei](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bnomei/32/775_2.png) [@bnomei](https://forum.getkirby.com/u/bnomei)
#### Post date: [August 27, 2019, 10:04pm UTC](https://forum.getkirby.com/t/kirby3-srcset-with-caption/15412/10 "2019-08-27T22:04:22Z")

</div>

magic done. almost… you still need to provide the css as described in the readme.

> **[bnomei/kirby3-srcset](https://github.com/bnomei/kirby3-srcset/releases/tag/v3.0.1)**
>
> Kirby 3 Plugin for creating lazyloading image srcset - bnomei/kirby3-srcset

i thought about this a while and i think its a great addition. thanks @Oziris !

---

<div class="post-metadata">

### Author: ![Oziris](https://avatars.discourse-cdn.com/v4/letter/o/df788c/32.png) [@Oziris](https://forum.getkirby.com/u/Oziris)
#### Post date: [August 28, 2019, 7:54am UTC](https://forum.getkirby.com/t/kirby3-srcset-with-caption/15412/11 "2019-08-28T07:54:06Z")

</div>

Yeah ! Thank you, I appreciate very much !

I have read the article and I have to say the authors has some heavy arguments. But as we know, things are more complicated than “good or bad” practices, and must be estimated for the efficiency they offer, I think.  
I would like to know what changed your mind?

I have tried your last realease on my masonry layout, and every items are oversized by height, but I didn’t digged your code yet, I don’t know from where it come from. I have to say I don’t understand the additionnal css to include, same for the logic from data-ratio. For me it’s like : "Ok, we have a straight line solution (apply style % ratio directly inside the code), or we have another solution that consist to go to point D and C before going to point B from A. 🙂

Did you take in account the figcaption option when applying the ratio to the figure ?

Edit : this would be the thing I would do, maybe I’m wrong :

```
<figure>
   <div class="container" style="padding-bottom: calc(100% / [$image->dimensions()->ratio()])">
      <img ....>
   <div>
   <figcaption>
      Blabla
   <figcaption>
</figure>

```

With basic style

```
.container {
 display: block;
 position: relative;
 height: 0;
}

.container img {
 position: absolute;
 top: 0;
 left: 0;
 width: 100%;
 height: 100%;
}
```

---

<div class="post-metadata">

### Author: ![bnomei](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bnomei/32/775_2.png) [@bnomei](https://forum.getkirby.com/u/bnomei)
#### Post date: [August 28, 2019, 11:28am UTC](https://forum.getkirby.com/t/kirby3-srcset-with-caption/15412/12 "2019-08-28T11:28:55Z")

</div>

- adding a div wrapper is not what i want. this is different element order than the kirby image tag creates. i will not do that. its not DCBA and it is not a more complicated solution. i am adding nothing to the dom. you want to add an structure element where no structure is happening. the render props of that image need to reflect what we want. i am solving an render problem with css logic. that is what css is for.
- adding inline style makes it hard to override that element with css if you need to remove or adjust the padding bottom. i will not do that. otherwise the padding bottom will be there no matter what the global css says.
- send me a picture of the oversized mazonry layout. preferable as an issue in the plugin repo. post the html code as well or give the an url. but i probablly just have something wrong in the code i created at close to midnight. i will fix that later today.
- i did not take the figcaption into consideration since i never used the caption param of the image kirbytag before. you are right it [creates an figcaption element](https://github.com/getkirby/kirby/blob/80b69380e672565a849037232c9951d1e32774c8/tests/Cms/KirbyText/KirbyTagsTest.php#L91). i will [adjust my code and css](https://github.com/bnomei/kirby3-srcset/issues/13) to solve that.

---

<div class="post-metadata">

### Author: ![bnomei](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bnomei/32/775_2.png) [@bnomei](https://forum.getkirby.com/u/bnomei)
#### Post date: [August 28, 2019, 2:19pm UTC](https://forum.getkirby.com/t/kirby3-srcset-with-caption/15412/13 "2019-08-28T14:19:14Z")

</div>

done. [https://github.com/bnomei/kirby3-srcset/releases/tag/v3.0.2](https://github.com/bnomei/kirby3-srcset/releases/tag/v3.0.2)

adding a div for the case with figcaption was indeed the only proper solution. i apologize for being ignorant in that regard. you need to grab the updated css from the readme as well.
