--- title: element slug: Web/CSS/element() tags: - CSS - CSS Function - Layout - Web - 参考 - 布局 translation_of: Web/CSS/element() --- <div>{{CSSRef}}{{SeeCompatTable}}</div> <p><a href="/en-US/docs/Web/CSS">CSS</a> 函数 <strong><code>element()</code></strong> 定义了一个从任意的 HTML 元素中生成的图像 {{cssxref("<image>")}} 值。该图像值是实时的,这意味着如果被指定的 HTML 元素被更改,the CSS properties using the resulting value are automatically updated.</p> <p>一个特别实用的应用场景是,在某个 HTML {{HTMLElement("canvas")}} 元素中渲染图像,然后将其用作背景。</p> <p>在使用 Gecko 渲染引擎的浏览器中,可以使用非标准的 {{domxref("document.mozSetImageElement()")}} 方法,将某个元素指定为其他元素的背景图像。</p> <h2 id="Syntax" name="Syntax">语法</h2> <pre class="syntaxbox notranslate">element(<var>id</var>)</pre> <p>其中:</p> <dl> <dt><var>id</var></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">示例</h2> <p>在支持 <code>-moz-element()</code> 的 Firefox 中,可以<a href="/samples/cssref/moz-element.html" title="https://developer.mozilla.org/samples/cssref/moz-element.html">实地考察</a>这些例子。</p> <h3 id="一个比较现实的例子">一个比较现实的例子</h3> <p>这个例子将文档中另一个隐藏的 {{HTMLElement("div")}} 元素作为背景。被隐藏的元素本身使用了渐变背景,也包含了一些文字,渐变背景和文字都成为了第一个元素的背景。</p> <pre class="brush: html notranslate"><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></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="default internal" src="/@api/deki/files/4624/=example1.png"></p> <h3 id="一个有点怪怪的例子">一个有点怪怪的例子</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 notranslate"><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> </pre> <p><img alt="example2.png" class="default internal" src="/@api/deki/files/4625/=example2.png"></p> <h2 id="规范">规范</h2> <table class="standard-table"> <thead> <tr> <th>规范</th> <th>状态</th> <th>备注</th> </tr> </thead> <tbody> <tr> <td>{{SpecName('CSS4 Images', '#element-notation', 'Using Elements as Images: the element() notation')}}</td> <td>{{Spec2('CSS4 Images')}}</td> <td>Deferred from CSS3 Images.</td> </tr> </tbody> </table> <h2 id="Browser_compatibility" name="Browser_compatibility">浏览器兼容性</h2> <p>{{Compat("css.types.image.element")}}</p> <h2 id="See_also" name="See_also">参见</h2> <ul> <li>{{cssxref("_image", "image()")}}</li> <li>{{cssxref("image-set", "image-set()")}}</li> <li>{{cssxref("<image>")}}</li> <li>{{cssxref("<gradient>")}}</li> <li>{{cssxref("element()")}}</li> <li>{{cssxref("cross-fade")}}</li> <li>{{domxref("document.mozSetImageElement()")}}</li> </ul>