vue cli 3 – use background image in style tag
As Max Martynov, highlighted in the comments, you should use url('~@/assets/image.svg')
.
Webpack has some specific rules when resolving assets[1].
In order to resolve an alias (@
), as you are using, it is mandatory webpack handles the request as a module request. Using the prefix ~
enforces webpack to treat the request as a module request, similar to require('some-module/image.svg')
.
References
- https://vuejs-templates.github.io/webpack/static.html#asset-resolving-rules
Vue3 css background-image injection
As an alternative to setting the variable dynamically as a CSS class via v-bind()
, it's also possible to bind as inline style:
<template>
<header :style="{backgroundImage: `url('$imageUrl')`}"></header>
</template>
See https://vuejs.org/guide/essentials/class-and-style.html#binding-inline-styles
how to set background image url for local files?
When you're using relative paths, Webpack is unable to resolve them properly if they are found inside your inline style
attributes. Webpack, can, however, resolve the image path properly if it is use as an <img>
element source in the template directly. Therefore, the solution to use a resolved image path as a CSS attribute is to simply reference it as a computed property.
In your template, you can use v-bind:style="heroImage"
to reference a computed property:
<template>
<div id="app">
<div v-bind:style="heroImage">
Content without background image
</div>
</div>
</template>
Then, in your VueJS component itself, you can do:
computed: {
heroImage() {
return {
backgroundImage: `url${require('../assets/images/HeroImg.jpg')}`
};
}
}
Vue.js data-bind style backgroundImage not working
The issue is that the value for backgroundImage
needs to be a string like this:
<div class="circular" v-bind:style="{ backgroundImage: 'url(' + image + ')' }"></div>
Here's a simplified fiddle that's working: https://jsfiddle.net/89af0se9/1/
Re: the comment below about kebab-case, this is how you can do that:
<div class="circular" v-bind:style="{ 'background-image': 'url(' + image + ')' }"></div>
In other words, the value for v-bind:style
is just a plain Javascript object and follows the same rules.
UPDATE: One other note about why you may have trouble getting this to work.
You should make sure your image
value is quoted so that the end resulting string is:
url('some/url/path/to/image.jpeg')
Otherwise, if your image URL has special characters in it (such as whitespace or parentheses) the browser may not apply it properly. In Javascript, the assignment would look like:
this.image = "'some/url/path/to/image.jpeg'"
or
this.image = "'" + myUrl + "'"
Technically, this could be done in the template, but the escaping required to keep it valid HTML isn't worth it.
More info here: Is quoting the value of url() really necessary?
How can I setting CSS background in vue-cli 3?
your url is not a relative or webpack url , and it has to be in double quote
if you want to use relative url it has to be url("../style/images/close.png");
if you want to use webpack
in vue.config.js
const path = require("path");
function resolve(dir) {
return path.join(__dirname, dir);
}
module.exports = {
chainWebpack: config => {
config.resolve.alias.set("@", resolve("src"))
}
}
in css:
background: url("@/style/images/close.png");
Vue style (background-image) binding not reactive
It's not working because of the semicolons you put at the end of url() directives, css rules set in object notation with Vue don't require semicolons :)
Related Topics
Why Is a Div with "Position: Absolute" Not by Default Relative to the Document
Inside Transparent Arrow on the Left
Edge Customize Cursor Doesn't Work
How to Create a Sidebar and Content Area Using Flexbox
Css3 Transition from Width Auto to "N" Px
Two Column Layout with One Having a Fixed Width in CSS
How to Create a Squeezed Box Using Only CSS
How to Add Tableview Footer in Javafx Tableview
Mime Type Error When Adding a CSS File to Angular
Change Ie Background Color on Unopened, Focused Select Box
How to Merge .CSS Files with SASS (Or Other Tool)
Css: How to Adjust My Font Size Fill All the Space in a Justified Layout
Equal Height Bootstrap Cards Within Slick Carousel
Less CSS Retina Media Queries Without Redundancy
The Old Center a Image in a Div Issue ( Image Size Variable - Div Size Fixed )