element()
element() CSS 函式定義了一個從任意 HTML 元素生成的 <image> 值。這個影像是即時的,這意味著如果 HTML 元素髮生變化,使用結果值的 CSS 屬性會自動更新。
一個特別有用的場景是,在 HTML <canvas> 元素中渲染影像,然後將其用作背景。
在 Gecko 瀏覽器上,你可以使用非標準的 document.mozSetImageElement() 方法來更改用作給定 CSS 背景元素背景的元素。
語法
css
element(id)
其中
- id
-
要用作背景的元素 ID,使用 HTML 屬性 #id 在元素上指定。
正式語法
<element()> =
element( <id-selector> )
<id-selector> =
<hash-token>
示例
這些示例在支援 -moz-element() 的 Firefox 構建版本中有效。
一個比較現實的例子
此示例使用一個隱藏的 <div> 作為背景。背景元素使用漸變,但還包含作為背景一部分渲染的文字。
html
<div id="target-box">
<p>This box uses the element with the #my-background ID as its background!</p>
</div>
<div id="background-container">
<div id="my-background">
<p>This text is part of the background. Cool, huh?</p>
</div>
</div>
css
#target-box {
width: 400px;
height: 400px;
background: -moz-element(#my-background) no-repeat;
}
#background-container {
overflow: hidden;
height: 0;
}
#my-background {
width: 1024px;
height: 1024px;
background-image: linear-gradient(to right, red, orange, yellow, white);
}
#my-background p {
transform-origin: 0 0;
rotate: 45deg;
color: white;
}
ID 為“my-background”的 <div> 元素被用作內容的背景,內容包括段落“此框使用 ID 為 #my-background 的元素作為其背景!”。
頁面預覽
這個基於 Vincent De Oliveira 的示例在 <div id="css-result"> 中建立了 <div id="css-source"> 的預覽。
HTML
html
<div id="css-source">
<h1>Page Preview</h1>
</div>
<div id="css-result"></div>
CSS
css
#css-result {
background: -moz-element(#css-source) no-repeat;
width: 256px;
height: 32px;
background-size: 80%;
border: dashed;
}
結果
規範
| 規範 |
|---|
| CSS 影像模組第 4 級 # element-notation |
瀏覽器相容性
載入中…