text-anchor - SVG: Scalable Vector Graphics 编辑

The text-anchor attribute is used to align (start-, middle- or end-alignment) a string of pre-formatted text or auto-wrapped text where the wrapping area is determined from the inline-size property relative to a given point. It is not applicable to other types of auto-wrapped text. For those cases you should use text-align. For multi-line text, the alignment takes place for each line.

The text-anchor attribute is applied to each individual text chunk within a given <text> element. Each text chunk has an initial current text position, which represents the point in the user coordinate system resulting from (depending on context) application of the x and y attributes on the <text> element, any x or y attribute values on a <tspan>, <tref> or <altGlyph> element assigned explicitly to the first rendered character in a text chunk, or determination of the initial current text position for a <textPath> element.

Note: As a presentation attribute, text-anchor can be used as a CSS property.

As a presentation attribute, it can be applied to any element but it has effect only on the following five elements: <altGlyph>, <text>, <textPath>, <tref>, and <tspan>

html, body, svg {
  height: 100%;
}
<svg viewBox="0 0 120 120" xmlns="http://www.w3.org/2000/svg">
  <!-- Materialisation of anchors -->
  <path d="M60,15 L60,110 M30,40 L90,40 M30,75 L90,75 M30,110 L90,110" stroke="grey" />

  <!-- Anchors in action -->
  <text text-anchor="start" x="60" y="40">A</text>
  <text text-anchor="middle" x="60" y="75">A</text>
  <text text-anchor="end" x="60" y="110">A</text>

  <!-- Materialisation of anchors -->
  <circle cx="60" cy="40" r="3" fill="red" />
  <circle cx="60" cy="75" r="3" fill="red" />
  <circle cx="60" cy="110" r="3" fill="red" />

  <style><![CDATA[
  text {
    font: bold 36px Verdana, Helvetica, Arial, sans-serif;
  }
  ]]></style>
</svg>

Usage notes

Default valuestart
Valuestart | middle | end
Animatablediscrete
start
The rendered characters are aligned such that the start of the text string is at the initial current text position. For an element with a direction property value of ltr (typical for most European languages), the left side of the text is rendered at the initial text position. For an element with a direction property value of rtl (typical for Arabic and Hebrew), the right side of the text is rendered at the initial text position. For an element with a vertical primary text direction (often typical for Asian text), the top side of the text is rendered at the initial text position.
middle
The rendered characters are aligned such that the middle of the text string is at the current text position. (For text on a path, conceptually the text string is first laid out in a straight line. The midpoint between the start of the text string and the end of the text string is determined. Then, the text string is mapped onto the path with this midpoint placed at the current text position.)
end
The rendered characters are shifted such that the end of the resulting rendered text (final current text position before applying the text-anchor property) is at the initial current text position. For an element with a direction property value of ltr (typical for most European languages), the right side of the text is rendered at the initial text position. For an element with a direction property value of rtl (typical for Arabic and Hebrew), the left side of the text is rendered at the initial text position. For an element with a vertical primary text direction (often typical for Asian text), the bottom of the text is rendered at the initial text position.

Specifications

SpecificationStatusComment
Scalable Vector Graphics (SVG) 2
The definition of 'text-anchor' in that specification.
Candidate RecommendationNo significant change
Scalable Vector Graphics (SVG) 1.1 (Second Edition)
The definition of 'text-anchor' in that specification.
RecommendationInitial definition

Browser compatibility

BCD tables only load in the browser

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:135 次

字数:7242

最后编辑:7 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文