这是 实验性技术
检查 浏览器兼容性表格 要小心谨慎在生产中使用这之前。

element() CSS function defines an <image> value generated from an arbitrary HTML element. This image is live, meaning that if the HTML element is changed, the CSS properties using the resulting value are automatically updated.

A particularly useful scenario for using this would be to render an image in an HTML <canvas> element, then use that as a background.

On Gecko browsers, you can use the non-standard document.mozSetImageElement() method to change the element being used as the background for a given CSS background element.

句法

element(id)
					

where:

id
The ID of an element to use as the background, specified using the HTML attribute # id on the element.

范例

These examples can be viewed live in builds of Firefox that support -moz-element() .

A somewhat realistic example

This example uses a hidden <div> as a background. The background element uses a gradient, but also includes text that is rendered as part of the background.

<div style="width:400px; height:400px; background:-moz-element(#myBackground1) no-repeat;">
  <p>This box uses the element with the #myBackground1 ID as its background!</p>
</div>
<div style="overflow:hidden; height:0;">
  <div id="myBackground1" style="width:1024px; height:1024px; background-image: linear-gradient(to right, red, orange, yellow, white);">
  <p style="transform-origin:0 0; transform: rotate(45deg); color:white;">This text is part of the background. Cool, huh?</p>
  </div>
</div>
					

<div> element with the ID "myBackground1" is used as the background for the content including the paragraph "This box uses the element with the #myBackground1 ID as its background!".

example1.png

A somewhat more bizarre example

This example uses a hidden <button> element in a repeating pattern as its background. This demonstrates that you can use arbitrary elements as background, but doesn't necessarily demonstrate good design practices.

<div style="width:400px; height:100px; background:-moz-element(#myBackground2);">
</div>
<div style="overflow:hidden; height:0;">
  <button id="myBackground2" type="button">Evil button!</button>
</div>
					

example2.png

规范

规范 状态 注释
CSS Images Module Level 4
The definition of 'Using Elements as Images: the element() notation' in that specification.
工作草案 Deferred from CSS3 Images.

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 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
element() Experimental Chrome 不支持 No Edge 不支持 No Firefox 完整支持 57 Prefixed
完整支持 57 Prefixed
Prefixed Implemented with the vendor prefix: -moz-
不支持 29 — 57 Prefixed 注意事项
Prefixed Implemented with the vendor prefix: -moz-
注意事项 -moz-element() is limited to background-image , background , border-image and border-image-source .
不支持 4 — 29 Prefixed 注意事项
Prefixed Implemented with the vendor prefix: -moz-
注意事项 -moz-element() is limited to background-image and background .
IE 不支持 No Opera 不支持 No Safari 不支持 No WebView Android 不支持 No Chrome Android 不支持 No Firefox Android 完整支持 60 Prefixed
完整支持 60 Prefixed
Prefixed Implemented with the vendor prefix: -moz-
不支持 29 — 60 Prefixed 注意事项
Prefixed Implemented with the vendor prefix: -moz-
注意事项 -moz-element() is limited to background-image , background , border-image and border-image-source .
不支持 4 — 29 Prefixed 注意事项
Prefixed Implemented with the vendor prefix: -moz-
注意事项 -moz-element() is limited to background-image and background .
Opera Android 不支持 No Safari iOS 不支持 No Samsung Internet Android 不支持 No

图例

完整支持

完整支持

不支持

不支持

实验。期望将来行为有所改变。

实验。期望将来行为有所改变。

见实现注意事项。

见实现注意事项。

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

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

另请参阅

元数据

  • 最后修改:
  1. CSS
  2. CSS 参考