要准确裁剪背景图片先要准确渲染,background-origin与background-clip

概念

background-origin: 背景图片从盒子哪里开始渲染
background-clip: 背景图片裁剪到盒子哪里

取值

background-origin:

默认值:背景图片从padding区域开始渲染
boder-box: 背景图片从边框里面开始
padding-box:背景图片从padding区域开始渲染
content-box: 背景图片从内容区域开始渲染

background-clip:

默认值: 背景图片裁剪到padding区域最外层边缘线
boder-box: 背景图片裁剪到边框最外层边缘线
padding-box: 背景图片裁剪到padding区域最外层边缘线
content-box: 背景图片裁剪到内容区域最外层边缘线

起步

我们在使用背景图片时,肯定首先是要确认我们的背景图片要从哪里开始渲染,
确定了渲染的状态, 我们才能准确的对背景图片进行裁剪。当然背景图片默认的渲染状态是从padding区域开始渲染的,我们在没有确认渲染的区域时,后期所做的裁剪工作实际上都是对照默认渲染区padding区域来进行裁剪的。

图文助解

background-origin:

代码:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <style type="text/css">
        .box,.box1,.box2,.box3 {
            width: 200px;
            height: 200px;
            border: 20px dotted #ccc;
            padding: 30px;
            background: url('images/bg.png') no-repeat;
            background-size: 100% 100%;
            margin-top: 50px;
        }
        .box1 {
            background-origin:border-box;
        }
        .box2 {
            background-origin: content-box;
        }
        .box3 {
            background-origin: padding-box;
        }
    </style>
</head>
<body>
<div class="box"></div>
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
</body>
</html>

图片:

image

图片描述

background-clip:

代码:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <style type="text/css">
    .box,.box1,.box2,.box3 {
            width: 200px;
            height: 200px;
            border: 20px dotted #ccc;
            padding: 30px;
            background: url('images/bg.png') no-repeat;
            background-size: 100% 100%;
            margin-top: 50px;
        }
        .box1 {
            background-clip: border-box;
        }
        .box2 {
            background-clip: content-box;
        }
        .box3 {
            background-clip: padding-box;
        }
    </style>
</head>
<body>
<div class="box"></div>
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
</body>
</html>

图片:

图片描述

图片描述

以上就是对background-origin和background-clip的解述,我们在裁剪背景图片做某些效果时就一定得准确的先渲染。如果以上那些地方理解有误还请指点,笔者会虚心学习。

posted @ 2020-01-19 11:55  热爱前端知识  阅读(244)  评论(0编辑  收藏  举报