background-origin CSS property sets the background's origin: from the border start, inside the border, or inside the padding.

The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.

注意, background-origin 会被忽略当 background-attachment is fixed .

句法

/* Keyword values */
background-origin: border-box;
background-origin: padding-box;
background-origin: content-box;
/* Global values */
background-origin: inherit;
background-origin: initial;
background-origin: unset;
					

background-origin property is specified as one of the keyword values listed below.

border-box

The background is positioned relative to the border box.

padding-box

The background is positioned relative to the padding box.

content-box

The background is positioned relative to the content box.

形式定义

初始值 padding-box
适用于 所有元素。它还适用于 ::first-letter and ::first-line .
继承 no
计算值 如指定
动画类型 discrete

形式句法

<box>#

where
<box> = border-box | padding-box | content-box

范例

Setting background origins

.example {
  border: 10px double;
  padding: 10px;
  background: url('image.jpg');
  background-position: center left;
  background-origin: content-box;
}
					
#example2 {
  border: 4px solid black;
  padding: 10px;
  background: url('image.gif');
  background-repeat: no-repeat;
  background-origin: border-box;
}
					
div {
  background-image: url('logo.jpg'), url('mainback.png'); /* Applies two images to the background */
  background-position: top right, 0px 0px;
  background-origin: content-box, padding-box;
}
					

规范

规范 状态 注释
CSS Backgrounds and Borders Module Level 3
The definition of 'background-origin' in that specification.
候选推荐 初始定义。

浏览器兼容性

更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
background-origin Chrome 完整支持 1
完整支持 1
不支持 1 — 64 Prefixed 注意事项
Prefixed Implemented with the vendor prefix: -webkit-
注意事项 Chrome accepts alternate synonyms to its values: padding , border ,和 content .
Edge 完整支持 12 Firefox 完整支持 4
完整支持 4
Prefixed Implemented with the vendor prefix: -webkit-
不支持 1 — 4 Prefixed 注意事项
Prefixed Implemented with the vendor prefix: -moz-
注意事项 Used the -moz-background-clip: padding | border 句法。
IE 完整支持 9 注意事项
完整支持 9 注意事项
注意事项 In IE 7 and before, Internet explorer was behaving as if background-origin: border-box was set. In Internet Explorer 8, as if background-origin: padding-box , the regular default value, was set.
Opera 完整支持 10.5
完整支持 10.5
不支持 15 — 51 Prefixed 注意事项
Prefixed Implemented with the vendor prefix: -webkit-
注意事项 Opera accepts alternate synonyms to its values: padding , border ,和 content .
Safari 完整支持 3
完整支持 3
完整支持 3 Prefixed 注意事项
Prefixed Implemented with the vendor prefix: -webkit-
注意事项 Webkit accepts alternate synonyms to its values: padding , border ,和 content .
WebView Android 完整支持 4.1
完整支持 4.1
不支持 4.1 — 64 Prefixed 注意事项
Prefixed Implemented with the vendor prefix: -webkit-
注意事项 WebView accepts alternate synonyms to its values: padding , border ,和 content .
Chrome Android 完整支持 18
完整支持 18
不支持 18 — 64 Prefixed 注意事项
Prefixed Implemented with the vendor prefix: -webkit-
注意事项 Chrome accepts alternate synonyms to its values: padding , border ,和 content .
Firefox Android 完整支持 14 Opera Android 完整支持 11
完整支持 11
不支持 14 — 47 Prefixed 注意事项
Prefixed Implemented with the vendor prefix: -webkit-
注意事项 Opera accepts alternate synonyms to its values: padding , border ,和 content .
Safari iOS 完整支持 1
完整支持 1
完整支持 1 Prefixed 注意事项
Prefixed Implemented with the vendor prefix: -webkit-
注意事项 Webkit accepts alternate synonyms to its values: padding , border ,和 content .
Samsung Internet Android 完整支持 1.0
完整支持 1.0
不支持 1.0 — 9.0 Prefixed 注意事项
Prefixed Implemented with the vendor prefix: -webkit-
注意事项 Chrome accepts alternate synonyms to its values: padding , border ,和 content .
content-box Chrome 完整支持 1 Edge 完整支持 12 Firefox 完整支持 4 IE 完整支持 9 注意事项
完整支持 9 注意事项
注意事项 In IE 7 and IE 9 of Internet Explorer, it always behaved like background-clip: padding if overflow: hidden | auto | scroll .
Opera 完整支持 10.5 Safari 完整支持 3 WebView Android 完整支持 4.1 Chrome Android 完整支持 18 Firefox Android 完整支持 14 Opera Android 完整支持 11 Safari iOS 完整支持 1 Samsung Internet Android 完整支持 1.0

图例

完整支持

完整支持

见实现注意事项。

见实现注意事项。

要求使用供应商前缀或不同名称。

要求使用供应商前缀或不同名称。

另请参阅

元数据

  • 最后修改: