Hi. I’m working with responsive banner images but, for some reason, only the largest image size specified in the srcset
is being created in the media folders.
The use case is that I have two banner images on top of one another, with the top one fading in and out with a CSS animation to create a changing image effect. Hence the foreach
statement. However, even if I use one image and an if
statement, the outcome is the same.
My banner image snippet contains the following…
<?php $images = $page->bannerimg()->toFiles(); foreach($images as $image): ?>
<img
class="absolute inset-0 w-full h-full rounded dark:brightness-[0.7]"
loading="lazy"
alt="<?= $image->text() ?>"
src="<?= $image->resize(440)->url() ?>"
srcset="<?= $image->srcset([440, 768, 1024, 1340]) ?>"
sizes="(min-width: 1420px) 1340px,
(min-width: 768px) 1024px,
(min-width: 440px) 768px,
440px"
width="<?= $image->resize(1340)->width() ?>"
>
<?php endforeach ?>
(As far as I can tell, I have the code set up correctly as per the Cookbook recipe.)
When a relevant page renders, the image tags for both banner images do include all the relevant variants in the srcset
, i.e. -440x.webp
, -768x.webp
, etc. And, in the relevant media folders, the ‘jobs’ subfolders do contain JSON files for each corresponding size. However, only the largest -1340x.webp
version of each image has actually been generated. As a result, even though the srcset
lists the different sizes, the largest image is the only one available and so that’s what appears in the Dev Tools Network tab.
I’m using TailwindCSS, so the w-full
and h-full
classes mean that image width and height are both set to 100% of the container size. In theory, I shouldn’t need them because of inset-0
but I added them as part of my investigation.
I tried adding a height line as well – height="<?= $image->resize(893)->height() ?>"
. That created a new -893x.webp
image but with exactly the same dimensions (1340x893) as the -1340x.webp
version.
What am I missing? No doubt it will turn out to be something obvious!
Kirby version is 3.9.6.1.
Many thanks.