Add CSS class to view image_field in twig In my custom view fields twig file views-view-fields--search--product_result.html.twig I try to theme my view item.

{{ fields.field_images.content }}
&lt;h5 class="card-title"&gt;{{ fields.title.content }}&lt;/h5&gt;


How do I add a CSS class to my image field {{ fields.field_images.content }}?
This is what worked for me: It's a preprocess function in the .theme file. I wish there was a way to do this directly into the template. This would apply that class everywhere the field appears.

/**
 * Implements hook_preprocess_field().
 * Adds a class to a People image.
 */
function themename_preprocess_field(&amp;$vars) {
  if ($vars['element']['#field_name'] == 'field_machine_name') {
    foreach($vars['items'] as $key =&gt; $item){
      $vars['items'][ $key ]['content']['#item_attributes']['class'][] = 'my_class';
    }
  }
}


themename, field_machine_name, and my_class are placeholders for your values.

This produces: 

&lt;img class="my_class" src="/your/image/location.png" alt="Your alt text." typeof="foaf:Image" width="458" height="354"&gt;