9

I want to set path for background image in less file app\design\frontend\vendor\theme\web\<lessfile> in magento2.

25

First add image in app\design\frontend\vendor\theme\web\images folder and write css as below:

background: url('@{baseDir}images/icon_sprite.png');

after add new image run command as below:

- php bin/magento cache:clean
- php bin/magento setup:static-content:deploy
| improve this answer | |
  • This is not working for me in Magento 2.1.4. Does this only apply to production mode or should this work in developer mode as well? – Giel Berkers Mar 14 '17 at 12:27
  • 1
    This will work in both mode, just you have to run php bin/magento setup:static-content:deploy command after add image in app\design\frontend\vendor\theme\web\images folder. if still not working remove var/view_preprocessed folder. – Dipesh Rangani Mar 14 '17 at 13:34
  • 1
    Thanks for the feedback. Turned out that my problem was elsewhere. Something to do with corrupt PNG files. Yeah I know, long story... your answer is still correct though. However, you could add that it in some cases it might be necessary to define @baseDir in your themes' theme.less. By default the variable is set to ../. – Giel Berkers Mar 14 '17 at 15:11
  • Thank you ! How about using it this style in _email-extend.less ? and what is the difference with using background: url("@{baseUrl}images/icn.png'); – medmek Oct 21 '18 at 18:02

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