-
Notifications
You must be signed in to change notification settings - Fork 916
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
background-image in style property, what is the best practice? #646
background-image in style property, what is the best practice? #646
Comments
I have met the same question, especially the image can't be converted as base64 |
Maybe it's a bad idea to use base64 image in style property, it makes HTML kinda ugly. |
Why not just use CSS instead of inline styles? |
Required images is transformed to base64 DataURI is due to the limit option of url-loader. Then, vue-loader is piping
|
@yyx990803 @blade254353074 I know what we CAN. But it's a little bit annoying to do these workarounds. |
For reducing requests, url-loader limit option must be configured......Although it's annoying. |
@blade254353074 I believe the size limitation is not relevant in this conversation. |
@NemoAlex But you said "base64 image in style property makes HTML kinda ugly"... |
@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 |
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 ES2015 Style:
Or without ES2015:
|
for Googlers, I had success with (using Nuxt):
|
for Googlers, I has success with (same using Nuxt):
@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. |
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 |
I placed the image inside public/img/<image.jpg> and then referred the image path mentioned in data property
|
我没有找到更好的方法
or
|
I was able to accomplish what I believe is a similar end goal by using a computed property to build a collection of individual CSS attributes (dynamic or not), and then assign the whole group to the DOM element as an inline style. Template:
Computed Props:
And I could still attach additional properties with normal CSS, but it looks like any properties assigned via the computed property may take precedence as I think they are applied on top of the regular CSS. Regular CSS:
|
|
//place it in style tag..it worked for me |
Dynamic data. |
Hello everyone, if you want to use css background property from your assests, you can use it as follows; |
I had to format mine like this with url('') |
I had difficulties with how to set dynamic background images in nuxt but i did it and it worked perfectly. In SCRIPT In HTML {{service.name}}just in case if someone is also finding the same problems i faced. it took me some hours to figure it out |
We need to put this into the nuxt documentation. Please! |
Let me try it with Nuxt! Then I can send a pr to nuxt docs :) |
Actually Next Team stated about this issue in the docs, I read it carefully in order to catch the info :) |
@onuriltan I saw this on nuxt, but your solution had both ~ and @ together and which is not mentioned in the nuxt documentation. In the sass and css files I need to use the "@" path. |
@pratyushtewari actually I tried those
Yes the guide is a little bit misleading, I can open a pr but just in english language :) |
it doesn't work for me |
Don't waste time, @onuriltan is right. |
This is Working for me:
But This is throwing compile error:
Can someone help please. Error: Errors compiling template:
|
@arijitnaskar - Can you try this and see if this works?
|
Mine is working with this:
|
Close this. |
Solutions were provided above. |
how if hving multiple image? |
:style="{ 'background-image': 'url(' + ImgData + ')' }" |
мне помогло следующее : |
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. |
In .vue template, we usually use img tag to show a image, like
which is OK.
However sometime we need to use background-image within inline style property:
But it's not working, somehow I need to write it like this:
or the ES2015 way:
which is still not neat.
So I'm just wondering is there any better way to do this?
The text was updated successfully, but these errors were encountered: