Add custom css class

Hello,
I have this code:

<figure role="group"> <?php if($img = $page->cover()->toFile()): ?> <?= $img->resize(1160) ?> <?php endif ?> </figure>

which generates this HTML code:

<figure role="group">  <img src="http://localhost:8888/media/pages/blog/title-post-test/2482944617-1555430072/imagetest.jpg">  </figure>

I need to add a CSS class in order to get:

<figure role="group">  <img class="mycustomclasscss"  src="http://localhost:8888/media/pages/blog/title-post-test/2482944617-1555430072/imagetest.jpg">  </figure>

Is it possible to get help? thank you

You can do it like that:

<figure role="group">
  <?php if($img = $page->cover()->toFile()): ?>
    <img class="mycustomclasscss" src="<?= $img->resize(1160)->url() ?>">
  <?php endif ?>
</figure>
1 Like

You probably don’t want an empty figure if the image doesn’t exist, so I suggest to wrap the if statement around the figure tag:

<?php if($img = $page->cover()->toFile()): ?>
<figure role="group">
    <img class="mycustomclasscss" src="<?= $img->resize(1160)->url() ?>">
</figure>
<?php endif ?>
1 Like

it works great, thanks!
My approach was the use of .setAttribute but obviously wrong.

@texnixe
thank you so much for this improvement!