CSS Portal

CSS text-anchor Property

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!

Description

The text-anchor property controls how SVG text is positioned relative to the text element’s specified anchor point (for example the x/y coordinates on a element). Instead of changing where the anchor point itself sits in the document, it changes how the glyphs are rendered around that anchor: the entire string (or each positioned chunk) is shifted so that a particular edge or center of its rendered glyph box aligns with the anchor. This property is applied to text-level elements in SVG (including and ) and affects layout at the glyph/geometry level rather than at the block-box level used by typical HTML flow layout.

Because it operates on glyph metrics and the current user coordinate system, text-anchor interacts closely with transformations, rotations, and explicit positioning. If an element has transforms applied (or is placed along a rotated path), the alignment behavior is resolved in the transformed coordinate space, so the visual relationship between anchor point and drawn text follows whatever transforms are in effect. When you use multiple x positions or nested elements, each positioned segment can be aligned independently, which lets you create complex label arrangements where parts of a label are anchored differently relative to their own coordinates.

The visual baseline and writing direction also affect how anchor placement appears, so text-anchor is often used together with baseline and writing-mode controls to achieve predictable results. For baseline alignment concerns, consider how dominant-baseline influences the vertical reference for glyph placement, and for horizontal orientation or line progression you may need to account for writing-mode. If you’re coming from HTML text layout, note that this property is distinct from how block or inline content is aligned in CSS; it serves a different purpose than text-align.

In practice, designers and developers use text-anchor to place labels and annotations precisely (for example centering chart labels, aligning tick labels to marks, or offsetting labels on icons) without altering the element’s coordinate. For predictable results, pair it with explicit positioning attributes and be mindful of transforms: if you rotate or translate the text container via transform, the anchoring will follow the transformed coordinate system. Because it works with glyph geometry rather than box-model rules, small differences in font metrics, letter spacing, or complex scripts can affect final placement, so test with your actual fonts and scripts when precise alignment matters.

Definition

Initial value
start
Applies to
<text>, <textPath>, and <tspan> elements in <svg>
Inherited
yes
Computed value
as specified
Animatable
yes
JavaScript syntax
object.style.textAnchor

Syntax

selector {
  text-anchor: start | middle | end | inherit;
}

Values

  • start(Default) The start of the text string is at the initial x coordinate.
  • middleThe midpoint of the text string is at the initial x coordinate.
  • endThe end of the text string is at the initial x coordinate.
  • inheritTakes the value from its parent element.

Example

<div class="example">
<p class="desc">SVG text-anchor example - anchors demonstrated at the vertical guide line</p>
<svg viewBox="0 0 300 200" xmlns="http://www.w3.org/2000/svg" role="img" aria-label="Text anchor demonstration">
<line class="guide" x1="150" y1="10" x2="150" y2="190" />
<circle class="anchor-point" cx="150" cy="50" r="3" />
<circle class="anchor-point" cx="150" cy="100" r="3" />
<circle class="anchor-point" cx="150" cy="150" r="3" />
<text class="anchor-start" x="150" y="50">start</text>
<text class="anchor-middle" x="150" y="100">middle</text>
<text class="anchor-end" x="150" y="150">end</text>
</svg>
</div>
.example {
  max-width: 520px;
  margin: 20px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial;
}

.desc {
  margin: 0 0 8px 0;
  color: #333;
  font-size: 14px;
}

svg {
  width: 100%;
  height: auto;
  border: 1px solid #e6e6e6;
  background: #fff;
  box-sizing: border-box;
  display: block;
}

.guide {
  stroke: #a0a0a0;
  stroke-width: 1.5;
  stroke-dasharray: 4 2;
}

.anchor-point {
  fill: #222;
  opacity: 0.6;
}

text {
  font-size: 16px;
  dominant-baseline: middle;
}

.anchor-start {
  text-anchor: start;
  fill: #1b9e77;
}

.anchor-middle {
  text-anchor: middle;
  fill: #d95f02;
}

.anchor-end {
  text-anchor: end;
  fill: #7570b3;
}

Browser Support

The following information will show you the current browser support for the CSS text-anchor property. Hover over a browser icon to see the version that first introduced support for this CSS property.

This property is supported by all modern browsers.
Desktop
Chrome
Edge
Firefox
Opera
Safari
Tablets & Mobile
Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-

Last updated by CSSPortal on: 1st January 2026

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!