CSS---Tips2背景

background

div{
    background: red url(img.png) no-repeat scroll left top/100% border-box content-box;
}

对应的属性全称

[background-color]

[background-image]

[background-repeat]

[background-attachment]

[background-position]/[background-size]

[background-origin]

[background-clip]

background-attachment

  • scroll 默认值 背景固定在元素上,不会随着内容一起滚动
  • fixed 背景固定在视窗上,内容滚动时背景不动
body {
    background-image: url(11.jpg); 
    background-attachment: fixed;
}

background-origin

设置背景起始位置

  • content-box 在内容盒子内部绘制背景
  • border-box 在元素盒子内部绘制背景
  • padding-box 在内边距盒子内部绘制背景
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        div {
            width: 200px;
            height: 300px;
            float: left;
            border: 10px dashed red;
            padding: 20px;
            margin: 20px;
            background-image: url(../img/11.jpg);
            background-repeat: no-repeat;
        }
        .div1{
            background-origin: content-box;
        }
        .div2{
            background-origin: border-box;
        }
        .div3{
            background-origin: padding-box;
        }
    </style>
</head>
<body>
<div class="div1"></div>
<div class="div2"></div>
<div class="div3"></div>
</body>
</html>

结果图

background-clip

在内边距盒子内部裁剪背景。

  • content-box 在内容盒子内部裁剪背景
  • border-box 在元素盒子内部裁剪背景
  • padding-box 在内边距盒子内部裁剪背景
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        div {
            width: 200px;
            height: 300px;
            float: left;
            border: 10px dashed red;
            padding: 20px;
            margin: 20px;
            background-image: url(../img/11.jpg);
            background-repeat: no-repeat;
        }
        .div1{
            background-clip: content-box;
        }
        .div2{
            background-clip: border-box;
        }
        .div3{
            background-clip: padding-box;
        }
    </style>
</head>
<body>
<div class="div1"></div>
<div class="div2"></div>
<div class="div3"></div>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值