text-anchor
text-anchor CSS 屬性將包含文字字串的框對齊,其中包裹區域由 inline-size 屬性確定,然後文字相對於元素的錨點放置,該錨點使用 x 和 y(或 dx 和 dy)屬性定義。如果存在,CSS 屬性的值將覆蓋元素的 text-anchor 屬性的任何值。
元素內的每個單獨文字片段都是獨立對齊的;因此,多行 <text> 元素的每一行文字都將根據 text-anchor 的值進行對齊。text-anchor 值僅對 <text>、<textPath> 和 <tspan> SVG 元素起作用。text-anchor 不適用於自動換行文字;有關此內容,請參閱 text-align。
語法
css
text-anchor: start;
text-anchor: middle;
text-anchor: end;
/* Global values */
text-anchor: inherit;
text-anchor: initial;
text-anchor: revert;
text-anchor: revert-layer;
text-anchor: unset;
值
正式定義
正式語法
text-anchor =
start |
middle |
end
示例
三個 <text> 元素被賦予相同的 x 位置,但 text-anchor 的值不同。包含一條虛線紅線以標記所有三個錨點的 x 軸位置。
html
<svg
viewBox="0 0 200 150"
height="150"
width="200"
xmlns="http://www.w3.org/2000/svg">
<line
x1="100"
y1="0"
x2="100"
y2="150"
stroke="red"
stroke-dasharray="10,5" />
<text x="100" y="40">Anchored</text>
<text x="100" y="80">Anchored</text>
<text x="100" y="120">Anchored</text>
</svg>
css
text:nth-of-type(1) {
text-anchor: start;
}
text:nth-of-type(2) {
text-anchor: middle;
}
text:nth-of-type(3) {
text-anchor: end;
}
規範
| 規範 |
|---|
| Scalable Vector Graphics (SVG) 2 # TextAnchorProperty |
瀏覽器相容性
載入中…
另見
dominant-baseline- SVG
<text>元素 - SVG
text-anchor屬性