CSS3实例分享之多重背景的实现(Multiple backgrounds)

CSS3的诞生为我们解决了这一问题,在CSS3里,通过background-image或者background可以为一个容器设置多张背景图像,也就是说可以把不同背景图象只放到一个块元素里。

首先我们来看一下语法吧:

background : [background-image] | [background-origin] | [background-clip] | [background-repeat] | [background-size] | [background-attachment] | [background-position]

多个背景图片的url之间使用逗号隔开即可,如果有多个背景图片,而其他属性只有一个(例如background-repeat只有一个),那么所有背景图片都应用该属性值。

下面我们就看一个例子吧:

这里我们要使用5张图片作为一个div容器的背景,我们来看一下代码:

style{
.div1{
    margin:50px auto;
    width:700px;
    height:450px;
    border:10px dashed #ff00ff;
    background-image:url(images/1.jpg),url(images/2.jpg),url(images            /3.jpg),url(images/4.jpg),url(images/5.jpg);
    background-repeat:no-repeat,no-repeat,no-repeat,no-repeat,no-repeat;
    background-position:top left,top right,bottom left,bottom right,center center;
    }
}
<div class="div1">
<a href="#" title="脚本之家">脚本之家</a>
</div>
    

在上面的代码中有这一句:

background-repeat:no-repeat;

写一个值就行了,效果是完全一样的。

当然上面设置背景图片的各个属性时是分开写的,那么我们也可以把背景图片的各个属性写在一块,这时的CSS代码如下:

代码如下:

.div1{

...

background:url(images/1.jpg) no-repeat top left,

url(images/2.jpg) no-repeat top right,

url(images/3.jpg) no-repeat bottom left,

url(images/4.jpg) no-repeat bottom right,

url(images/5.jpg) no-repeat center center;

...

}

转载于:https://www.cnblogs.com/guhui1994/p/5906292.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值