0

How to add background in Drupal 8 views-view-fields.html.twig' files.

I try like this but it's don't work. Using views I just output image paht.

 <div class="cover-img" style="background:url('{{ field_image }}');">
    </div>

Thank you

1

2 Answers 2

1

Adding CSS background image hardcoded in the Twig (or TPL in D7 sites) isn't a best practice. You should do this via your theme's css file like style.css, or via a module (some theme's give you the opportunity to add it directly on your site).

I would suggest to restore any such changes in the Twig and redoing it in the css file.

0

Download Image URL Formatter module

This module add a url formatter for image field. Then you can output image url directly.

Once enabled, views will give you an option to change the formatter to url when you edit or add your image field.

After you select url as your formatter, {{ field_image }} will output url instead of picture.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.