css ul 叠在一起_css中backgrund-size使用细节,你知道吗?

3d78074353e66d75097f6f56bf30c8b6.png

一、前言

在实际的前端开发工作中,我们经常会使用html+css做一些页面的布局,写一个完整的网页。而在各种网页中,往往少不了图片。图片几乎是每个网页中不可或缺的部分。缺少了图片的衬托,网页似乎缺少了一种视觉美感。

而在css中就有一种处理图片的方式,也就是以背景图片的方式将需要的图片引入到网页中的具体位置。掌握背景图片的使用也就显得十分的关键了。操作背景图片的属性有很多,这里就不一一的赘述了,主要说一下背景图片属性中的backgroun-size这个属性。很多人在使用的过程中会忽略其中的一个小细节,特别是很多的前端初学者。接下来带你揭秘这个细节。接着往下看吧。

二、background-size的使用细节

要说的细节就是,background-size必须要写在backgroun-imge属性后面才有效果。也就是要先引入背景图片,再设置背景图片的大小才可以,这个顺序不能乱,你若不信,给你验证一下。

示例一:background-size写在背景图片引入之前无效

html代码:

<body>
    <div class="box"></div>
</body>

css代码:

<style>
        * {
            margin: 0px;
            padding: 0px;
        }
        
        .box {
            width: 400px;
            height: 400px;
            margin: 20px auto;
            /*background-size: 50% 50%;*/ /* background-size:背景图片引入之前不起作用 */
            background: url(./image/风景01.jpg) no-repeat;
            background-size: 50% 50%;  /*  background-size:背景图片引入之后有效  */
        }
    </style>

效果展示:

25e9f3fcb29df7c6d1e92359c383700b.gif

实例二:这是很多初学者容易犯的错误

比如我们要使用一张精灵图实现这样的一个效果

635880b6d28228d78144d5ecda98f066.png

html代码:这个并没有什么问题

<ul>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
    </ul>

但是很多人处理css代码是这样的(不建议)

    <style>
        * {
            margin: 0px;
            padding: 0px;
        }
        
        ul {
            width: 500px;
            margin: 20px auto;
        }
        
        ul li {
            list-style: none;
            float: left;
            width: 32px;
            height: 32px;
            margin-left: 20px;
            background: url(./image/bg .png) no-repeat 0 0;
            background-size: 32px auto;
        }
        
        ul li:nth-child(2) {
            background: url(./image/bg .png) no-repeat 0 -32px;
        }
        
        ul li:nth-child(3) {
            background: url(./image/bg .png) no-repeat 0 -64px;
        }
        
        ul li:nth-child(4) {
            background: url(./image/bg .png) no-repeat 0 -96px;
        }
        
        ul li:nth-child(5) {
            background: url(./image/bg .png) no-repeat 0 -128px;
        }
    </style>

这个时候会发现,除了第一个可以实现具体的需求效果之外,其余的都不行,如下图:

1bb4bebcab515adea736a9d81a055f1b.png

这又是为什么呢?很多初学者百思不得其解。其实说说到底,还是backgroun-size的原因

1361ff07591676878384539285e726ef.png

在这个案例中如果重新引入了背景图(虽然是多余的操作),要达到需求,必须再引入背景图片的后面再写一次backgroun-size才有效果。

8f263beaa604636661da8b1ab3dd9a91.png

但是这个案例中,不建议这样处理,这样处理也没啥意义,重复代码太多,只是为了阐述background-size的使用细节才这样写的,也是很多初学者小白容易犯的一个错误。下面给出一个推荐的写法。

推荐的写法:

案例开头已经说过了,是使用精灵图来做的,所以五个图标唯一不同的地方应该是精灵图的位置,其他的都是相同的。这个时候只需要改变一下精灵图的位置就可以了,其他的相同的部分可以写在一起。

    <style>
        * {
            margin: 0px;
            padding: 0px;
        }
        
        ul {
            width: 500px;
            margin: 20px auto;
        }
        /*所有li相同的样式属性*/
        ul li {
            list-style: none;
            float: left;
            width: 32px;
            height: 32px;
            margin-left: 20px;
            background: url(./image/bg .png) no-repeat 0 0;
            background-size: 32px auto;
        }
        /*每个li背景图片的位置不同,改变每一个li图片的位置即可*/
        ul li:nth-child(2) {
            background-position: 0px -32px;
        }
        
        ul li:nth-child(3) {
            background-position: 0px -64px;
        }
        
        ul li:nth-child(4) {
            background-position: 0px -96px;
        }
        
        ul li:nth-child(5) {
            background-position: 0px -128px;
        }
    </style>

三、总结

对背景图片的操作在前端页面布局中很常用。对于background-size这个使用细节,一定要掌握。以上分享希望对大家有所帮助。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值