css中设置多张背景图片

有一些应用场景中,我们需要叠加设置多张图片,一般我们可能是采用img来设置,但还有一种更简单的方法,采用背景属性来设置,css提供了可同时设置多张背景图的属性。语法如下:

.bg{
    background:
    url('../img/bg1.png'),
    url('../img/bg2.png'),
    url('../img/bg3.png')
}

为不同的背景图片设置大小和位置:

.bg{
    height:100%;
    width:100%;
    background:
    url('../img/bg1.png'),
    url('../img/bg2.png'),
    url('../img/bg3.png');
    background-size:75%,50%,cover;
    background-position:top50px right 80px,40px 40px,top center;
    background-repeat:no-repeat;
}

设置background-size和position属性时,按照背景的显示顺序进行设置。背景图片最先设置的图片显示在最上面,最后设置的图片显示在最下面。

另外背景可以混合设置:比如你可以同时设置图片和背景色,很多时候我们开发中会遇到以下这种效果,使用这种方法可以最快搞定,而且不用再考虑适配性问题。

实现代码:

<!doctype html>
<html>
<head>
	<meta charset="utf-8">
	<title>An example</title>
	<meta name="viewport" content="width=device-width">
<style>
html,body {
	margin: 0;
	padding: 0;
}

.bg-multi {
	height: 100vh;
	width: 100vw;
	background: 
	url('icon-empty.png'),#FAFAFA;
	background-size:30vw, cover;
	background-position:center,center;
	background-repeat: no-repeat;
}
</style>
</head>
<body>
	<div class="bg-multi"></div>
</body>
</html>

 

  • 3
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值