element() CSS function

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

The 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.

Syntax

css
element(id)

where:

id

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

Formal syntax

<element()> = 
element( <id-selector> )

<id-selector> =
<hash-token>

Examples

These examples work 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.

html
<div id="target-box">
  <p>This box uses the element with the #my-background ID as its background!</p>
</div>

<div id="background-container">
  <div id="my-background">
    <p>This text is part of the background. Cool, huh?</p>
  </div>
</div>
css
#target-box {
  width: 400px;
  height: 400px;
  background: -moz-element(#my-background) no-repeat;
}

#background-container {
  overflow: hidden;
  height: 0;
}

#my-background {
  width: 1024px;
  height: 1024px;
  background-image: linear-gradient(to right, red, orange, yellow, white);
}

#my-background p {
  transform-origin: 0 0;
  rotate: 45deg;
  color: white;
}

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

Page Preview

This example based on Vincent De Oliveira's creates a preview of the <div id="css-source"> inside <div id="css-result">.

HTML

html
<div id="css-source">
  <h1>Page Preview</h1>
</div>
<div id="css-result"></div>

CSS

css
#css-result {
  background: -moz-element(#css-source) no-repeat;
  width: 256px;
  height: 32px;
  background-size: 80%;
  border: dashed;
}

Result

Specifications

Specification
CSS Images Module Level 4
# element-notation

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
element()
Experimental
Chrome – No support
Chrome
footnote No support
Edge – No support
Edge
footnote No support
Firefox – Partial support
Firefox 4 – 28 (Release date: 2011-03-22)
prefix
footnote Partial support
prefix Implemented with the vendor prefix: -moz-
footnote -moz-element() is limited to background-image and background.
Firefox – Partial support
Firefox 29 – 56 (Release date: 2014-04-29)
prefix
footnote Partial support
prefix Implemented with the vendor prefix: -moz-
footnote -moz-element() is limited to background-image, background, border-image and border-image-source.
Firefox – Full support
Firefox 57 (Release date: 2017-11-14)
prefix
prefix Implemented with the vendor prefix: -moz-
Opera – No support
Opera
footnote No support
Safari – No support
Safari
footnote No support
Chrome Android – No support
Chrome Android
footnote No support
Firefox for Android – Partial support
Firefox for Android 4 – 28 (Release date: 2011-03-29)
prefix
footnote Partial support
prefix Implemented with the vendor prefix: -moz-
footnote -moz-element() is limited to background-image and background.
Firefox for Android – Partial support
Firefox for Android 29 – 59 (Release date: 2014-04-29)
prefix
footnote Partial support
prefix Implemented with the vendor prefix: -moz-
footnote -moz-element() is limited to background-image, background, border-image and border-image-source.
Firefox for Android – Full support
Firefox for Android 60 (Release date: 2018-05-09)
prefix
prefix Implemented with the vendor prefix: -moz-
Opera Android – No support
Opera Android
footnote No support
Safari on iOS – No support
Safari on iOS
footnote No support
Samsung Internet – No support
Samsung Internet
footnote No support
WebView Android – No support
WebView Android
footnote No support
WebView on iOS – No support
WebView on iOS
footnote No support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Partial support
Partial support
No support
No support
Experimental. Expect behavior to change in the future.
Requires a vendor prefix or different name for use.
Has more compatibility info.

See also