element

  • Revision slug: CSS/element
  • Revision title: element
  • Revision id: 34946
  • Created:
  • Creator: teoli
  • Is current revision? No
  • Comment 19 words added

Revision Content

{{ CSSRef() }}

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 {{ HTMLElement("canvas") }} element, then use that as a background.

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

Usage note: Though this should be usable in every context where an <image> is allowed, only the {{ cssxref("background") }} and {{ cssxref("background-image") }} properties do accept it right now (and only in Gecko-based browsers).

This should change in the future to be more in adequacy with the specification.

Syntax

element( id )

where:

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

Examples

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

A somewhat realistic example

This example uses a hidden {{ HTMLElement("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: -moz-linear-gradient(left, red, orange, yellow, white);">
    <p style="-moz-transform-origin:0 0; -moz-transform: rotate(45deg); color:white;">This text is part of the background. Cool, huh?</p>
    </div>
  </div>

The {{ HTMLElement("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 {{ HTMLElement("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

Specifications

Specification Status Comment
CSS Image Values and Replaced Content Level 3 {{ Spec2('CSS3 Image') }}  

Browser compatibility

{{ CompatibilityTable() }}

Feature Firefox (Gecko) Chrome Internet Explorer Opera Safari
Basic support 4.0 (2.0) [*] {{ property_prefix("-moz") }} {{ CompatNo() }} {{ CompatNo() }} {{ CompatNo() }} {{ CompatNo() }}
Feature Firefox Mobile (Gecko) Android IE Phone Opera Mobile Safari Mobile
Basic support 4.0 (2.0) [*] {{ property_prefix("-moz") }} {{ CompatNo() }} {{ CompatNo() }} {{ CompatNo() }} {{ CompatNo() }}

[*] Usage limited to the {{ cssxref("background") }} and {{ cssxref("background-image") }} CSS properties.

See also

  • {{ domxref("document.mozSetImageElement()") }}

Revision Source

<p>{{ CSSRef() }}</p>
<p>The <code>element()</code> <a href="/en/CSS" title="CSS">CSS</a> function defines an <code>&lt;image&gt;</code> 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.</p>
<p>A particularly useful scenario for using this would be to render an image in an HTML {{ HTMLElement("canvas") }} element, then use that as a background.</p>
<p>On Gecko browsers, you can use the non-standard {{ domxref("document.mozSetImageElement()") }} method to change the element being used as the background for a given CSS background element.</p>
<div class="warning"><strong>Usage note:</strong> Though this should be usable in every context where an <code>&lt;image&gt;</code> is allowed, only the {{ cssxref("background") }} and {{ cssxref("background-image") }} properties do accept it right now (and only in Gecko-based browsers).<br> <br> This should change in the future to be more in adequacy with the specification.</div>
<h2>Syntax</h2>
<pre>element( <em>id</em> )
</pre>
<p>where:</p>
<dl> <dt style="margin-left: 40px;"><strong><code>id</code></strong></dt> <dd style="margin-left: 40px;">The ID of an element to use as the background, specified using the HTML attribute #<em>id</em> on the element.</dd>
</dl>
<h2>Examples</h2>
<p>These examples can be <a href="/samples/cssref/moz-element.html" title="https://developer.mozilla.org/samples/cssref/moz-element.html">viewed live</a> in builds of Firefox that support <code>-moz-element()</code>.</p>
<h3>A somewhat realistic example</h3>
<p>This example uses a hidden {{ HTMLElement("div") }} as a background. The background element uses a gradient, but also includes text that is rendered as part of the background.</p>
<pre class="brush: html">  &lt;div style="width:400px; height:400px; background:-moz-element(#myBackground1) no-repeat;"&gt;
    &lt;p&gt;This box uses the element with the #myBackground1 ID as its background!&lt;/p&gt;
  &lt;/div&gt;

  &lt;div style="overflow:hidden; height:0;"&gt;
    &lt;div id="myBackground1" style="width:1024px; height:1024px; background-image: -moz-linear-gradient(left, red, orange, yellow, white);"&gt;
    &lt;p style="-moz-transform-origin:0 0; -moz-transform: rotate(45deg); color:white;"&gt;This text is part of the background. Cool, huh?&lt;/p&gt;
    &lt;/div&gt;
  &lt;/div&gt;
</pre>
<p>The {{ HTMLElement("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!".</p>
<p><img alt="example1.png" class="internal default" src="/@api/deki/files/4624/=example1.png"></p>
<h3>A somewhat more bizarre example</h3>
<p>This example uses a hidden {{ HTMLElement("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.</p>
<pre class="brush: html">  &lt;div style="width:400px; height:100px; background:-moz-element(#myBackground2);"&gt;
  &lt;/div&gt;

  &lt;div style="overflow:hidden; height:0;"&gt;
    &lt;button id="myBackground2" type="button"&gt;Evil button!&lt;/button&gt;
  &lt;/div&gt;
</pre>
<p><img alt="example2.png" class="internal default" src="/@api/deki/files/4625/=example2.png"></p>
<h2>Specifications</h2>
<table class="standard-table"> <thead> <tr style="background-color: rgb(255, 204, 255);"> <th scope="col">Specification</th> <th scope="col">Status</th> <th scope="col">Comment</th> </tr> </thead> <tbody> <tr> <td><a class=" external" href="http://dev.w3.org/csswg/css3-images/#element-reference" title="http://dev.w3.org/csswg/css3-images/#element-reference">CSS Image Values and Replaced Content Level 3</a></td> <td>{{ Spec2('CSS3 Image') }}</td> <td> </td> </tr> </tbody>
</table>
<h2>Browser compatibility</h2>
<p>{{ CompatibilityTable() }}</p>
<div id="compat-desktop"> <table class="compat-table"> <tbody> <tr> <th>Feature</th> <th>Firefox (Gecko)</th> <th>Chrome</th> <th>Internet Explorer</th> <th>Opera</th> <th>Safari</th> </tr> <tr> <td>Basic support</td> <td>4.0 (2.0) <a class="link-https" href="https://bugzilla.mozilla.org/show_bug.cgi?id=506826" title="https://bugzilla.mozilla.org/show_bug.cgi?id=506826"></a> [*] {{ property_prefix("-moz") }}</td> <td>{{ CompatNo() }}</td> <td>{{ CompatNo() }}</td> <td>{{ CompatNo() }}</td> <td>{{ CompatNo() }} <a class="link-https" href="https://bugs.webkit.org/show_bug.cgi?id=44650" title="https://bugs.webkit.org/show_bug.cgi?id=44650"></a></td> </tr> </tbody> </table>
</div>
<div id="compat-mobile"> <table class="compat-table"> <tbody> <tr> <th>Feature</th> <th>Firefox Mobile (Gecko)</th> <th>Android</th> <th>IE Phone</th> <th>Opera Mobile</th> <th>Safari Mobile</th> </tr> <tr> <td>Basic support</td> <td>4.0 (2.0) <a class="link-https" href="https://bugzilla.mozilla.org/show_bug.cgi?id=506826" title="https://bugzilla.mozilla.org/show_bug.cgi?id=506826"></a> [*] {{ property_prefix("-moz") }}</td> <td>{{ CompatNo() }}</td> <td>{{ CompatNo() }}</td> <td>{{ CompatNo() }}</td> <td>{{ CompatNo() }}</td> </tr> </tbody> </table>
</div>
<p>[*] Usage limited to the {{ cssxref("background") }} and {{ cssxref("background-image") }} CSS properties.</p><h2 name="SeeAlso">See also</h2>
<ul> <li>{{ domxref("document.mozSetImageElement()") }}</li>
</ul>
Revert to this revision