Using svg as background-image does not work
See original GitHub issueWhen you use the default options and rules given here, svg paths cannot be used in CSS background-image: url()
anymore.
Example:
background-image: url('~/assets/some-icon.svg')
Output
background-image: url('[Object object]')
Expected
background-image: url("data:image/svg+xml;utf8,<svg> optimized svg </svg>");
This would require https://github.com/bhovhannes/svg-url-loader to work
The webpack config could then look like that, I tried to make it work locally, but somehow the resulting SVG data gets escaped (guess this is some kind of nuxt XSS protection).
config.module.rules.push({
test: /\.svg$/,
oneOf: [
{
resourceQuery: /css/,
use: {
loader: 'svg-url-loader',
options: { noquotes: false }
}
},
{
use: [
'vue-loader',
{
loader: 'svg-to-vue-component/loader',
options
}
]
}
]
})
You could use it then like this:
background-image: url('~/assets/some-icon.svg?css')
Issue Analytics
- State:
- Created 5 years ago
- Reactions:14
- Comments:10 (4 by maintainers)
Top Results From Across the Web
SVG CSS background image not showing in Chrome
Maybe some browsers/OS have problems in rendering the svg in this way. Since it is not a real SVG just convert it in...
Read more >Why doesn't my SVG background-image work? - Stack Overflow
Try clearing your browser cache and refreshing once, and try other browsers too. Also, if by some chance you happen to be using...
Read more >How to add SVGs with CSS (background-image)
Notice with this method we have to treat the SVG code before it will work. In the example, we convert the SVG to...
Read more >Using svg for background - The freeCodeCamp Forum
The other issue might be in how you linked them. You might try embedding inline svg instead (which can be done in HTML...
Read more >SVG background image not showing - Essential Objects
However, if using a very simple image, it has benefits to use scalable vector graphics. The html below works fine in multiple web...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
just for the record, since @3b3ziz has modified it himself 👍 , the good way is now :
Here it is way to resolve them.