Skip to content

Vue dynamic background image inline component

I’m building a banner with Vue that needs to have a dynamic background, however, it doesn’t seem to be working. Not sure what I’m doing wrong. I’ve tried a few other ways and it works if I do an image tag something like

<img :src="require(`@/assets/images/${backgroundImage}`)" />

But obviously this needs to be an inline background image.



    class="w-full h-64 bg-auto bg-no-repeat bg-center lg:bg-cover relative"
    :style="{ backgroundImage: url(require('@/assets/images/' + backgroundImage))}"
    <div class="w-full h-full flex flex-col justify-center items-center text-white px-6">
      <div class="hero-text rounded text-center py-8 px-12">
        <p class="text-base lg:text-md uppercase font-medium">{{ smallLeadingText }}</p>
        <h1 class="text-xl md:text-3xl lg:text-5xl uppercase font-bold">{{ mainText }}</h1>
        <p class="text-base lg:text-md">{{ subText }}</p>

export default {
  name: "PageHero",
  props: {
    backgroundImage: String,
    smallLeadingText: {
      type: String,
      required: false
    mainText: {
      type: String,
      required: true
    subText: {
      type: String,
      required: false


  backgroundImage="mc-background.png "
  smallLeadingText="Powerful, secure &amp; affordable"
  mainText="Minecraft hosting"
  subText="Plans suitable for all budgets"



Looks like you’ve just got some syntax errors in your style attribute around string quoting. Try

<div :style="{ backgroundImage: `url(${require('@/assets/images/' + backgroundImage)})` }">

Might be easier to create some computed properties to resolve everything though

computed: {
  bgImage () {
    return require('@/assets/images/' + this.backgroundImage)
  inlineStyle () {
    return {
      backgroundImage: `url(${this.bgImage})` 


<div :style="inlineStyle">

Demo ~