background-origin背景图片定位参考系&background-clip背景裁剪[专栏1]

本文详细介绍了CSS3中`background-origin`和`background-clip`两个属性,分别讲解了它们的作用和用法。`background-origin`定义了背景图片的定位参考系,而`background-clip`决定了背景图片的裁剪区域。文章通过多个实例比较了两者同时设置的效果,帮助读者深入理解这两个属性的差异。
摘要由CSDN通过智能技术生成

目录

一、background-origin背景图片定位参考系

二、background-clip背景裁剪

三、background-origin与background-clip同时设置的比较


一、background-origin背景图片定位参考系

ackground-origin值

含义

border-box

背景图片相对于元素border区域进行定位

padding-box(默认值)

背景图片相对于元素padding区域进行定位

content-box

背景图片相对于元素content区域进行定位

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>background-origin</title>
    <link rel="stylesheet" href="../../reset.css">
    <style>
        body{
            background-color: burlywood;
        }
        #Box{
            width: 1800px;
            height: 400px;
            background-color: gray;
            margin: 200PX auto;
        }
        div[class^="box"]{
            width: 344px;
            height: 296px;
            background-color: rgb(28, 68, 154);
            /* 使4个盒子的边框为15px */
            border: 15px dashed rgb(236, 20, 20);
            /* 使4个盒子显示在一行 */
            float: left;
            /* 设置每个盒子外边距10px */
            margin: 10px;
            /* 设置每个盒子内边距10px */
            padding: 20px;
            /* 每个盒子都设置背景,不平铺 */
            background-image: url(./flower.jpg) ;
            background-repeat:no-repeat;
        }
        
        .box2{
            background-origin: border-box;
        }
        .box3{
            
            background-origin: padding-box;
        }
        .box4{
            background-origin: content-box;
        }
    </style>
</head>
<body>
    <div id="Box">
        <div class="box1"></div>
        <div class="box2"></div>
        <div class="box3"></div>
        <div class="box4"></div>
    </div>
</body>
</html>

二、background-clip背景裁剪

background-clip:border-box; 图像从边框(包括border区域)开始裁剪(默认值), 表示裁剪超出边框区的内容

background-clip:padding-box; 图像从内边距(包括padding)开始裁剪, 表示裁剪超出内填充区的内容

background-clip:content-box; 图像从内容(包括content)开始裁剪, 表示裁剪超出内容区的内容

background-clip:text;文本剪裁(字体加背景),见后续geng

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>background-clip</title>
    <link rel="stylesheet" href="../../reset.css">
    <style>
        body{
            background-color: burlywood;
        }
        #Box{
            width: 1800px;
            height: 400px;
            background-color: gray;
            margin: 200PX auto;
        }
        div[class^="box"]{
            width: 344px;
            height: 296px;
            background-color: rgb(28, 68, 154);
            /* 使4个盒子的边框为15px */
            border: 15px dashed rgb(236, 20, 20);
            /* 使4个盒子显示在一行 */
            float: left;
            /* 设置每个盒子外边距10px */
            margin: 10px;
            /* 设置每个盒子内边距10px */
            padding: 20px;
            /* 每个盒子都设置背景,不平铺 */
            background-image: url(./flower.jpg) ;
            background-repeat:no-repeat;
        }
        
        .box2{
            background-clip: border-box;
        }
        .box3{
            
            background-clip: padding-box;
        }
        .box4{
            background-clip: content-box;
        }
    </style>
</head>
<body>
    <div id="Box">
        <div class="box1"></div>
        <div class="box2"></div>
        <div class="box3"></div>
        <div class="box4"></div>
    </div>
</body>
</html>

 

三、background-origin与background-clip同时设置的比较

background-origin与background-clip比较1

background-origin

border-box

background-clip

border-box

padding-box

content-box

background-origin与background-clip比较1附代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>background-origin与backgroiund-clip的比较1</title>
    <link rel="stylesheet" href="../../reset.css">
    <style>
        body{
            background-color: burlywood;
        }
        #Box{
            width: 1800px;
            height: 400px;
            background-color: gray;
            margin: 200PX auto;
        }
        div[class^="box"]{
            width: 344px;
            height: 296px;
            background-color: rgb(28, 68, 154);
            /* 使4个盒子的边框为15px */
            border: 15px dashed rgb(236, 20, 20);
            /* 使4个盒子显示在一行 */
            float: left;
            /* 设置每个盒子外边距10px */
            margin: 10px;
            /* 设置每个盒子内边距10px */
            padding: 20px;
            /* 每个盒子都设置背景,不平铺 */
            background-image: url(./flower.jpg) ;
            background-repeat:no-repeat;
        }
        
        .box2{
            background-origin:border-box;
            background-clip: border-box;
        }
        .box3{
            background-origin:border-box;
            background-clip: padding-box;
        }
        .box4{
            background-origin:border-box;
            background-clip: content-box;
        }
    </style>
</head>
<body>
    <div id="Box">
        <div class="box1"></div>
        <div class="box2"></div>
        <div class="box3"></div>
        <div class="box4"></div>
    </div>
</body>
</html>

background-origin与background-clip比较2

background-origin

padding-box

background-clip

border-box

padding-box

content-box

background-origin与background-clip比较2附代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>background-origin与background-clip比较2</title>
    <link rel="stylesheet" href="../../reset.css">
    <style>
        body{
            background-color: burlywood;
        }
        #Box{
            width: 1800px;
            height: 400px;
            background-color: gray;
            margin: 200PX auto;
        }
        div[class^="box"]{
            width: 344px;
            height: 296px;
            background-color: rgb(28, 68, 154);
            /* 使4个盒子的边框为15px */
            border: 15px dashed rgb(236, 20, 20);
            /* 使4个盒子显示在一行 */
            float: left;
            /* 设置每个盒子外边距10px */
            margin: 10px;
            /* 设置每个盒子内边距10px */
            padding: 20px;
            /* 每个盒子都设置背景,不平铺 */
            background-image: url(./flower.jpg) ;
            background-repeat:no-repeat;
        }
        
        .box2{
            background-origin:padding-box;
            background-clip: border-box;
        }
        .box3{
            background-origin:padding-box;
            background-clip: padding-box;
        }
        .box4{
            background-origin:padding-box;
            background-clip: content-box;
        }
    </style>
</head>
<body>
    <div id="Box">
        <div class="box1"></div>
        <div class="box2"></div>
        <div class="box3"></div>
        <div class="box4"></div>
    </div>
</body>
</html>

background-origin与background-clip比较3

background-origin

content-box

background-clip

border-box

padding-box

content-box

 background-origin与background-clip比较3附代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>background-origin与background-clip比较3</title>
    <link rel="stylesheet" href="../../reset.css">
    <style>
        body{
            background-color: burlywood;
        }
        #Box{
            width: 1800px;
            height: 400px;
            background-color: gray;
            margin: 200PX auto;
        }
        div[class^="box"]{
            width: 344px;
            height: 296px;
            background-color: rgb(28, 68, 154);
            /* 使4个盒子的边框为15px */
            border: 15px dashed rgb(236, 20, 20);
            /* 使4个盒子显示在一行 */
            float: left;
            /* 设置每个盒子外边距10px */
            margin: 10px;
            /* 设置每个盒子内边距10px */
            padding: 20px;
            /* 每个盒子都设置背景,不平铺 */
            background-image: url(./flower.jpg) ;
            background-repeat:no-repeat;
        }
        
        .box2{
            background-origin:content-box;
            background-clip: border-box;
        }
        .box3{
            background-origin:content-box;
            background-clip: padding-box;
        }
        .box4{
            background-origin:content-box;
            background-clip: content-box;
        }
    </style>
</head>
<body>
    <div id="Box">
        <div class="box1"></div>
        <div class="box2"></div>
        <div class="box3"></div>
        <div class="box4"></div>
    </div>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大怪冰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值