I am attempting to add a background image to my vue.js project

岁酱吖の 提交于 2019-12-05 14:54:41

I do not agree with the comments. This is actually a true question about Vue.js. :)

You are using single file components, so I suppose you created your project with vue-cli and the webpack template...

If you open your Vue.js devtools, you can inspect your components. This is particularly handy to determine their visual limits.

All components, including App.vue, are injected into your index.html and have, to some extent, an independent existence. So if you want to set a full background image for the body, not just for a specific component inside of it, you have two main options:

  1. Create a static stylesheet
  2. Put your style rules in App.vue

Static stylesheet

Create a style.css file and put it in the static folder. Here, it will not be processed by Webpack. In this file, you could have the following code:

body {
  background-image: url('background.jpg');
}

Open your index.html and include your stylesheet:

<link rel="stylesheet" href="/static/style.css">

Style rules in App.vue

With this solution, you can put the following code in the style section of App.vue:

body {
  background-image: url('background.jpg');
}

However, you have to remove the scoped attribute!

The explanation is in the vue-loader documentation:

When a tag has the scoped attribute, its CSS will apply to elements of the current component only.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!