CSS 函数 element() 定义了一个从任意的 HTML 元素中生成的图像 <image> 值。该图像值是实时的,这意味着如果被指定的 HTML 元素被更改,则使用结果值的 CSS 属性将自动更新。
一个特别实用的应用场景是,在某个 HTML <canvas> 元素中渲染图像,然后将其用作背景。
在使用 Gecko 渲染引擎的浏览器中,可以使用非标准的 document.mozSetImageElement() 方法,将某个元素指定为其他元素的背景图像。
语法
element(id)
- id - 要用作背景的元素的 ID,使用元素上的 HTML 属性 #id 指定。
例子
这个例子将文档中另一个隐藏的 <div> 元素作为背景。被隐藏的元素本身使用了渐变背景,也包含了一些文字,渐变背景和文字都成为了第一个元素的背景。
<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; rotate: 45deg; color:white;">
This text is part of the background. Cool, huh?
</p>
</div>
</div>
ID 为“myBackground1”的 <div> 元素用作内容的背景,包括段落“此框使用具有 #myBackground1 ID 的元素作为其背景!