关于background设置的问题



最近在写css的时候遇到了一些关于background设置的问题。当一个div中要设置背景图和背景色的时候,某些时候背景色会覆盖掉背景图。

<div class="bg_color">
        <div class="txt">
                测试占位
        </div>
</div>


情况1:背景色和背景图片都设在background里面。背景色和背景图片的都存在。调换背景色和背景图的顺序也都显示了。

.bg_color{background:url(../images/test_pic/top_nav.jpg) no-repeat 10px 0px red;}
.txt{height:60px;width:100%;}

情况2:背景图片设在background-image中,背景色设在background中,背景色覆盖了背景图

.bg_color{background-image:url(../images/test_pic/top_nav.jpg) no-repeat 10px 0px;background:red;}
.txt{height:60px;width:100%;}

情况3:设置和情况2相同,只是顺序反了一下,背景色覆盖了背景图

.bg_color{background:red;background-image:url(../images/test_pic/top_nav.jpg) no-repeat 10px 0px;}
.txt{height:60px;width:100%;}

情况4:背景图放在background中,背景色放在background-color中,背景色和背景图片都显示出来了。

.bg_color{background:url(../images/test_pic/top_nav.jpg) no-repeat 10px 0px;background-color:red;}
.txt{height:60px;width:100%;}

情况5:设置和情况4相同,只是顺序反了一下。只显示了背景图,没有显示背景色

.bg_color{background-color:red;background:url(../images/test_pic/top_nav.jpg) no-repeat 10px 0px;}
.txt{height:60px;width:100%;}

情况6:设置了background-image和background-color,背景色覆盖了背景图。

.bg_color{background-image:url(../images/test_pic/top_nav.jpg) no-repeat 10px 0px;background-color:red;}
.txt{height:60px;width:100%;}

情况7:设置和情况6相同,只是顺序反了一下。背景色覆盖了背景图。

.bg_color{background-color:red;background-image:url(../images/test_pic/top_nav.jpg) no-repeat 10px 0px;}
.txt{height:60px;width:100%;}

总结:background具有高优先级,background-color比background-image具有高优先级。使用时要注意先后顺序。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
08-24 4662

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值