vue-cli项目中,css文件background-image设置问题

使用绝对路径

在vue-cli项目中的放在public目录下的资源会被直接复制,不会经过webpack的打包处理。目录如下
在这里插入图片描述

我们可以在css文件中这么用

#header {
	position:relative;
	height:72px;
	background:url("/image/logo.png");
	overflow:hidden;
}

文件打包生成以后,他访问的是这个地址
http://localhost:8080/image/logo.png

相对路径

不建议使用相对路径,可能会引起各种问题

#header {
	position:relative;
	height:72px;
	background:url("../../assets/logo.png");
	overflow:hidden;
}

访问项目,图片路径是http://localhost:8080/img/logo.82b9c7a5.png

使用服务器上面的路径

目录结构如下
在这里插入图片描述

我们可以通过less或者sass,访问服务器上面的路径,将绝对路径中修改一下,以便模拟

在common.less里面

/*
css 加载图片的路径 调用方式 => url('@{cssHost}doctor-bg.png')  
现在为本地地址
*/ 
@cssHost :'http://localhost:8080/image/'; 

在dashboard.less中

@charset "utf-8";
@import "../../assets/common.less";
* {margin:0;padding:0;box-sizing:border-box;}
.dash {
	position:relative;
	font-family:"Microsoft Yahei", Arial, sans-serif;
    background-image:url("@{cssHost}dashboard/bg.png");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值