element

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

Revision Content

{{ CSSMozExtensionRef() }}{{ gecko_minversion_header("2.0") }}

The -moz-element() CSS function denotes a live <image> generated from an arbitrary HTML element. Though this should be usable in every context where an <image> is requested, only the {{ cssxref("background") }} and {{ cssxref("background-image") }} properties do accept it right now.

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.

You can use the {{ domxref("document.mozSetImageElement()") }} method to change the element being used as the background for a given CSS background element.

Syntax

-moz-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

Browser compatibility

Browser Lowest version Support of
Internet Explorer --- ---
Firefox (Gecko) 4.0 (Gecko 2.0) [*] -moz-element()
Opera --- ---
Chrome/Safari (WebKit) ---

---

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

See also

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

Bugs

  • {{ Bug("506826") }}
  • {{ WebkitBug("44650") }}

Revision Source

<p>{{ CSSMozExtensionRef() }}{{ gecko_minversion_header("2.0") }}</p>
<p>The <code>-moz-element()</code> <a href="/en/CSS" title="CSS">CSS</a> function denotes a live <code>&lt;image&gt;</code> generated from an arbitrary HTML element. Though this should be usable in every context where an <code>&lt;image&gt;</code> is requested, only the {{ cssxref("background") }} and {{ cssxref("background-image") }} properties do accept it right now.</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>You can use the {{ domxref("document.mozSetImageElement()") }} method to change the element being used as the background for a given CSS background element.</p>
<h2>Syntax</h2>
<pre>-moz-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>Browser compatibility</h2>
<table class="standard-table"> <tbody> <tr> <th>Browser</th> <th>Lowest version</th> <th>Support of</th> </tr> <tr> <td>Internet Explorer</td> <td>---</td> <td>---</td> </tr> <tr> <td>Firefox (Gecko)</td> <td>4.0 (Gecko 2.0) [*]</td> <td><code>-moz-element()</code></td> </tr> <tr> <td>Opera</td> <td>---</td> <td>---</td> </tr> <tr> <td>Chrome/Safari (WebKit)</td> <td>---</td> <td> <p>---</p> </td> </tr> </tbody>
</table>
<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>
<h2>Bugs</h2>
<ul> <li>{{ Bug("506826") }}</li> <li>{{ WebkitBug("44650") }}</li>
</ul>
Revert to this revision