The text-anchor
attribute is used to align (start-, middle- or end-alignment) a string of text relative to a given point.
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.
As a presentation attribute, it also can be used as a property directly inside a CSS stylesheet.
Categories | Presentation attribute |
---|---|
Value | start | middle | end | inherit |
Animatable | Yes |
Normative document | SVG 1.1 (2nd Edition) |
<?xml version="1.0"?> <svg width="120" height="120" viewBox="0 0 120 120" xmlns="http://www.w3.org/2000/svg" version="1.1"> <!-- 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>
Live sample
The following elements can use the text-anchor
attribute
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | ? | ? | ? | ? | ? | ? |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | ? | ? | ? | ? | ? | ? | ? |
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/text-anchor