Search code examples
javascriptvue.jsvuejs3vue-composition-apivue-script-setup

Blank background-image in vue.js


For a website, i want 70% of the page to be covered with an image. However the image is just blank. I use Vue.js as my frontend. Here is the code that results in a blank space:

<script setup lang="ts">
import { ref, onMounted, computed } from 'vue';
</script>


<template>

  <div style="background-image: `url(${require('../assets/bg.png')})`;
            height: 70vh" class="full-width-image">
    </div>
      <div class="container">
        <div class="row">
          <div class="col">

          </div>
        </div>
        <div class="row">
          <div class="col">
            <h1>Test</h1>
            <p>test test test!</p>
          </div>
        </div>
    </div>
</template> -

  <style>
  .full-width-image {
    margin: auto;
    padding: 0;
    background-size: cover;
    background-repeat: no-repeat;
    background-position: center center;

  }
  </style>

Here is how the site looks like: enter image description here


Solution

  • Import the image as module then bind it to the style background :

    <script setup lang="ts">
    import { ref, onMounted, computed } from 'vue';
    import bg from '../assets/bg.png'
    
    </script>
    
    
    <template>
    
      <div :style="{'background-image': url(bg), height: '70vh'}" class="full-width-image">
        </di
        ...
        </div>
    </template>