试一试
background-origin: border-box;
background-repeat: no-repeat;
background-origin: padding-box;
background-repeat: no-repeat;
background-origin: content-box;
background-repeat: no-repeat;
<section id="default-example">
<div id="example-element">This is the content of the element.</div>
</section>
#example-element {
background-image: url("/shared-assets/images/examples/leopard.jpg");
color: #d73611;
text-shadow: 2px 2px black;
padding: 20px;
border: 10px dashed #333333;
font-size: 2em;
font-weight: bold;
}
请注意,当 background-attachment 为 fixed 时,background-origin 将被忽略。
语法
css
/* Keyword values */
background-origin: border-box;
background-origin: padding-box;
background-origin: content-box;
/* Global values */
background-origin: inherit;
background-origin: initial;
background-origin: revert;
background-origin: revert-layer;
background-origin: unset;
background-origin 属性的值为下面列出的关键字之一。
值
border-box-
背景相对于边框盒定位。
padding-box-
背景相对于内边距盒定位。默认值。
content-box-
背景相对于内容盒定位。
正式定义
| 初始值 | padding-box |
|---|---|
| 应用于 | 所有元素。也适用于 ::first-letter 和 ::first-line。 |
| 继承性 | 否 |
| 计算值 | 同指定值 |
| 动画类型 | 一个可重复的列表 |
正式语法
background-origin =
<visual-box>#
<visual-box> =
content-box |
padding-box |
border-box
示例
设置背景起点
css
.example {
border: 10px double;
padding: 10px;
background: url("image.jpg");
background-position: center left;
background-origin: content-box;
}
css
#example2 {
border: 4px solid black;
padding: 10px;
background: url("image.gif");
background-repeat: no-repeat;
background-origin: border-box;
}
css
div {
background-image:
url("logo.jpg"), url("main-back.png"); /* Applies two images to the background */
background-position:
top right,
0px 0px;
background-origin: content-box, padding-box;
}
使用两个渐变
在此示例中,该盒子有一个粗虚线边框。第一个渐变使用 padding-box 作为 background-origin,因此背景位于边框内部。第二个渐变使用 content-box,因此只显示在内容后面。
css
.box {
margin: 10px 0;
color: white;
background:
linear-gradient(
90deg,
rgb(131 58 180 / 100%) 0%,
rgb(253 29 29 / 60%) 60%,
rgb(252 176 69 / 100%) 100%
),
radial-gradient(circle, white 0%, black 28%);
border: 20px dashed black;
padding: 20px;
width: 400px;
background-origin: padding-box, content-box;
background-repeat: no-repeat;
}
html
<div class="box">Hello!</div>
规范
| 规范 |
|---|
| CSS Backgrounds and Borders Module Level 3 # background-origin |
浏览器兼容性
加载中…