2022.10.14

1 多背景

background-image: url(图片路径), url(图片路径);
  • 不同的背景图像【逗号】隔开,也可以给不同的图片设置多个不同的属性(如背景位置,背景重复等)

  • 书写顺序越靠前,显示越靠上

    • 单独定义

    .box { 
    background-image: url(img_flwr.gif), url(paper.gif); 
    background-position: right bottom, left top; 
    background-repeat: no-repeat, repeat; 
    }
    • 简写

    .box {
    background: url(img_flwr.gif) right bottom no-repeat, url(paper.gif) left top repeat;
    }

2 背景图尺寸设置

  • background-size: 数值

    • px:设置背景图像的高度和宽度。第一个值设置宽度,第二个值设置高度。如果只设置一个值,则第二个 值会被设置为 “auto”。

    • percentage:以父元素的百分比来设置背景图像的宽度和高度。第一个值设置宽度,第二个值设置高度。如 果只设置一个值,则第二个值会被设置为 “auto”。

    • cover:覆盖,等比例缩放背景图片到铺满整个盒子,但是背景图可能会无法完整显示在盒子中(宽高给大点)

    • contain:包含,等比例缩放背景图片到完整显示在盒子中,有一边到达边界就停止放大, 可能导致另一边留白 但是背景图可能在区域范围内铺不满(宽高给大点)

 .wrap div {
            width: 200px;
            height: 200px;
            border: 1px solid red;
            margin: 5px;
        }
​
        /* 显示越靠前的背景,书写顺序越靠前 */
        .wrap .box {
            background-image: url(./img/bg1.jpg), url(./img/yd.jpg);
        }
​
        .wrap .box1 {
            background: url(./img/bg1.jpg), url(./img/7.jpg);
        }
​
        .wrap .box2 {
            background: url(./img/bg1.jpg) no-repeat, url(./img/7.jpg) no-repeat;
        }
​
        /* background-size: 数值 */
        /* px */
        .wrap .box3 {
            background: url(./img/yd.jpg) no-repeat;
            background-size: 100px 100px;
            background-size: 100px;
        }
​
        /* 百分比 */
        .wrap .box4 {
            background: url(./img/7.jpg) no-repeat;
            background-size: 50% 50%;
            background-size: 50%;
            background-size: 20%;
        }
​
        /* cover:覆盖,等比例缩放背景图片到铺满整个盒子,但是背景图可能会无法完整显示在盒子中(宽高给大点) */
        .wrap .box5 {
            width: 900px;
            height: 400px;
            background: url(./img/7.jpg) no-repeat;
            background-size: cover;
        }
​
        /* contain:包含,等比例缩放背景图片到完整显示在盒子中,但是背景图可能在区域范围内铺不满(宽高给大点) */
        .wrap .box6 {
            width: 600px;
            height: 400px;
            background: url(./img/7.jpg) no-repeat;
            background-size: contain;
        }

3 背景图定位区域——background-origin属性

  • background-origin: padding-box; 默认值,背景图相对于内填充区域来定位

  • background-origin: content-box; 背景图相对于内容来定位

  • background-origin: border-box; 背景图片相对于边框区域来定位

        .wrap div {
            width: 200px;
            height: 200px;
            border: 20px dotted red;
            margin: 50px;
            padding: 50px;
        }
​
        /* 默认值,背景图相对于内填充区域来定位 */
        .wrap .box1 {
            background: url(./img/bg2.jpg) no-repeat;
            background-origin: padding-box;
        }
​
        /* 背景图相对于内容来定位 */
        .wrap .box2 {
            background: url(./img/bg2.jpg) no-repeat;
            background-origin: content-box;
        }
​
        /* 背景图片相对于边框区域来定位 */
        .wrap .box3 {
            background: url(./img/bg2.jpg) no-repeat;
            background-origin: border-box;
        }

4 背景颜色绘制区域——background-clip属性

  • background-clip: border-box; 背景被裁剪到边框区域,在内容区、内填充区、边框区域显示,默认值

  • background-clip: padding-box; 背景被裁剪到内填充区域,在内容区、内填充区显示

  • background-clip: content-box; 背景被裁剪到内容区域,仅在内容区域显示

     .wrap div {
            width: 200px;
            height: 200px;
            background: plum;
            padding: 30px;
            margin: 30px;
            border: 20px dotted black;
        }
​
        /* 背景被裁剪到内容区域,仅在内容区域显示 */
        .wrap div:nth-child(1) {
            /* content-box */
            background-clip: content-box;
        }
​
        /* 背景被裁剪到内填充区域,在内容区、内填充区显示 */
        .wrap div:nth-child(2) {
            /* padding-box */
            background-clip: padding-box;
        }
​
        /* 背景被裁剪到边框区域,在内容区、内填充区、边框区域显示,默认值 */
        .wrap div:nth-child(3) {
            /* border-box */
            background-clip: border-box;
        }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值