Add Background Image in Vue.js with Examples
In this guide, I’ll show some ways to set background image in Vue.js. Let’s see the ways with examples:
Example 1
We are able to set inline background image in kebab-case and camelCase properties.
Kebab-case:
<div :style="{'background-image':'url(https://example.com/bg.png)'}"></div>
camelCase:
<div :style="{backgroundImage:'url(https://example.com/bg.png)'}"></div>
Example 2
If we need to display background image from server-side, we can show like this way:
<div :style="{backgroundImage:`url(${article.image})`}"></div>
Example 3
We can set background image using vue data property. Let’s see the example:
<template>
<section>
<div :style="image" class="image"></div>
</section>
</template>
<script>
export default {
data() {
return {
image: {backgroundImage: "url(https://via.placeholder.com/150)"}
};
}
};
</script>
<style>
.image {
height: 150px;
background-repeat: no-repeat;
}
</style>
Comment
Preview may take a few seconds to load.
Markdown Basics
Below you will find some common used markdown syntax. For a deeper dive in Markdown check out this Cheat Sheet
Bold & Italic
Italics *asterisks*
Bold **double asterisks**
Code
Inline Code
`backtick`Code Block```
Three back ticks and then enter your code blocks here.
```
Headers
# This is a Heading 1
## This is a Heading 2
### This is a Heading 3
Quotes
> type a greater than sign and start typing your quote.
Links
You can add links by adding text inside of [] and the link inside of (), like so:
Lists
To add a numbered list you can simply start with a number and a ., like so:
1. The first item in my list
For an unordered list, you can add a dash -, like so:
- The start of my list
Images
You can add images by selecting the image icon, which will upload and add an image to the editor, or you can manually add the image by adding an exclamation !, followed by the alt text inside of [], and the image URL inside of (), like so:
Dividers
To add a divider you can add three dashes or three asterisks:
--- or ***

Comments (0)