element

  • Revision slug: CSS/element
  • Revision title: element
  • Revision id: 352881
  • Created:
  • Creator: Alexei03a
  • Is current revision? No
  • Comment

Revision Content

{{CSSRef}}{{SeeCompatTable}}

Summary

The element() CSS function defines an {{xref_cssimage}} 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.

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 4 {{Spec2('CSS4 Image')}} Actually now deferred to CSS4.

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

<div>
  {{CSSRef}}{{SeeCompatTable}}</div>
<h2 id="Summary" name="Summary">Summary</h2>
<p>The <code>element()</code> <a href="/en-US/docs/CSS" title="CSS">CSS</a> function defines an {{xref_cssimage}} 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>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
element( <em>id</em> )</pre>
<p>where:</p>
<dl>
  <dt>
    <strong><code>id</code></strong></dt>
  <dd>
    The ID of an element to use as the background, specified using the HTML attribute #<em>id</em> on the element.</dd>
</dl>
<h2 id="Examples" name="Examples">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 id="A_somewhat_realistic_example">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 id="A_somewhat_more_bizarre_example">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 id="Specifications">Specifications</h2>
<table class="standard-table">
  <thead>
    <tr>
      <th>Specification</th>
      <th>Status</th>
      <th>Comment</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><a href="http://dev.w3.org/csswg/css4-images/" title="http://dev.w3.org/csswg/css4-images/">CSS Image Values and Replaced Content Level 4</a></td>
      <td>{{Spec2('CSS4 Image')}}</td>
      <td>Actually now deferred to CSS4.</td>
    </tr>
  </tbody>
</table>
<h2 id="Browser_compatibility" name="Browser_compatibility">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) [*] {{property_prefix("-moz")}}</td>
        <td>{{CompatNo}}</td>
        <td>{{CompatNo}}</td>
        <td>{{CompatNo}}</td>
        <td>{{CompatNo}}</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) [*] {{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 id="See_also" name="See_also">See also</h2>
<ul>
  <li>{{domxref("document.mozSetImageElement()")}}</li>
</ul>
Revert to this revision