Description
In .vue template, we usually use img tag to show a image, like
<img src="./link/to/image.png">
which is OK.
However sometime we need to use background-image within inline style property:
<div style="background-image: url(./link/to/image.png)"
But it's not working, somehow I need to write it like this:
<div :style="{'background-image': 'url(' + require('./link/to/image.png') + ')'}">
or the ES2015 way:
<div :style="{'background-image': `url(${require('./link/to/image.png')})`}">
which is still not neat.
So I'm just wondering is there any better way to do this?
Metadata
Metadata
Assignees
Labels
No labels
Activity
Krispomoho commentedon Feb 17, 2017
I have met the same question, especially the image can't be converted as base64
NemoAlex commentedon Feb 17, 2017
Maybe it's a bad idea to use base64 image in style property, it makes HTML kinda ugly.
yyx990803 commentedon Feb 19, 2017
Why not just use CSS instead of inline styles?
blade254353074 commentedon Mar 3, 2017
Required images is transformed to base64 DataURI is due to the limit option of url-loader.
You can reduce the limit, may be 10000 (bytes). It's useful to reduce the number of requests.
Then, vue-loader is piping
<style></style>
to the corresponding loader (depends on your config).Such as scss-loader, postcss-loader, css-loader, style-loader, and css-loader will piping the results to url-loader and file-loader.
So you can just write css to instead of inline styles:
url(image.png)
=>require('./image.png')
url(~module/image.png)
=>require('module/image.png')
NemoAlex commentedon Mar 3, 2017
@yyx990803 @blade254353074 I know what we CAN. But it's a little bit annoying to do these workarounds.
blade254353074 commentedon Mar 3, 2017
For reducing requests, url-loader limit option must be configured......Although it's annoying.
NemoAlex commentedon Mar 3, 2017
@blade254353074 I believe the size limitation is not relevant in this conversation.
blade254353074 commentedon Mar 3, 2017
@NemoAlex But you said "base64 image in style property makes HTML kinda ugly"...
willChowFront commentedon Mar 29, 2017
@yyx990803 I have the same issue .I need change this background-image frequently by data in component, in css , i can't find a way to solve it
shshaw commentedon Jul 10, 2017
This isn't really vue-loader's fault, but I kept running into a similar issue where an image URL contained a space, so the style was invalid and would not appear on the element.
Ensure your
backgroundImage
declarations are wrapped inurl(
and quotes so the style works correctly, no matter the file name.ES2015 Style:
Or without ES2015:
dimitrieh commentedon Mar 31, 2018
for Googlers, I had success with (using Nuxt):
:style="{ backgroundImage: 'url(' + require(
@/assets/img/${page.image}) + ')' }"
johnyluyte commentedon May 15, 2018
for Googlers, I has success with (same using Nuxt):
:style="{ backgroundImage: 'url(' + require('@/assets/imgs/bg'+backgroundId+'.png') + ')' }"
@dimitrieh 's answer is indeed correct, but his Template literals was "absorbed" by github's Markdown parser. If you copy his code from his comment directly, the code is not going to work.
However if you are using ES2015 you should be using Template literals. My code here is just for understanding the concept.
PierBover commentedon Jun 13, 2018
Again, for Googlers...
In my case (an old Vue project) the problem seemed to be that the image wasn't being bundled by Webpack, even if the URL was right. Using
require(...)
returns the correct URL in the final bundle, but will also "tell Webpack" to bundle the image.chandregowda commentedon Jun 16, 2018
I placed the image inside public/img/<image.jpg> and then referred the image path mentioned in data property
<div class="text-center mb-4" :style="{ backgroundImage:
url('${bgImage}')}">
17 remaining items
onuriltan commentedon Dec 30, 2019
@pratyushtewari actually I tried those
~assets/img.jpeg
= worked@assets/img.jpeg
= didn't work~@/assets/img.jpeg
= workedYes the guide is a little bit misleading, I can open a pr but just in english language :)
coolternet commentedon Mar 7, 2020
it doesn't work for me
kayoderock commentedon Jun 15, 2020
Don't waste time, @onuriltan is right.
arijitnaskar commentedon Aug 12, 2020
This is Working for me:
But This is throwing compile error:
Can someone help please.
Error:
Errors compiling template:
pratyushtewari commentedon Aug 12, 2020
@arijitnaskar - Can you try this and see if this works?
:style="{ backgroundImage: url('" + require('../assets/images/bg1.jpg') + "') , backgroundSize: cover}"
arijitnaskar commentedon Aug 13, 2020
Mine is working with this:
nejat-njonjo commentedon Mar 28, 2021
Close this.
EmmyMay commentedon May 11, 2021
I am having this same issue. The path is resolving correctly but not as a string.
nejat-njonjo commentedon May 18, 2021
Solutions were provided above.
VivianS-GitHub commentedon Jul 19, 2021
how if hving multiple image?
kuitao1018 commentedon Oct 14, 2021
:style="{ 'background-image': 'url(' + ImgData + ')' }"
Penguin-learning-to-walk commentedon Jan 30, 2023
мне помогло следующее :
<footer class="footer-area bg-img background-overlay" style="background-image: url('src/assets/img/bg-img/4.jpg');">
kipropbrian commentedon Oct 18, 2024
If you have an issue where background-image works in development but leads to 404 in when you build, just move the image to public.