CSS text-anchor Property
The CSS text-anchor property controls how SVG text is aligned relative to its designated anchor point.
It determines whether an SVG text chunk begins, ends, or is centered at its anchor position. The property is commonly used for SVG diagrams, charts, labels, and illustrations where text must align precisely with graphical elements.
text-anchor Syntax
The text-anchor property accepts three alignment keywords: start, middle, and end.
Basic Syntax
text {
text-anchor: middle;
}
Available Values
text {
text-anchor: start;
}
text {
text-anchor: middle;
}
text {
text-anchor: end;
}
CSS-Wide Keywords
text {
text-anchor: initial;
}
text {
text-anchor: inherit;
}
text {
text-anchor: unset;
}
text {
text-anchor: revert;
}
text {
text-anchor: revert-layer;
}
The initial value is start. In horizontal left-to-right text, this places the beginning of the text at the anchor position.
Using an SVG Presentation Attribute
<svg viewBox="0 0 300 100" xmlns="http://www.w3.org/2000/svg">
<text x="150" y="50" text-anchor="middle">Centered Text</text>
</svg>
The SVG text-anchor presentation attribute provides another way to specify the same alignment behavior. A corresponding CSS declaration takes precedence over the presentation attribute when both apply.
Property Values
| Value | Description |
|---|---|
start |
Aligns the beginning of the text chunk with the anchor position, according to the writing direction. |
middle |
Centers the text chunk around its anchor position along the inline direction. |
end |
Aligns the end of the text chunk with the anchor position, according to the writing direction. |
initial |
Resets the property to start. |
inherit |
Uses the computed value from the parent element. |
unset |
Uses the inherited value because the property is inherited. |
revert |
Reverts the property according to CSS cascade origin rules. |
revert-layer |
Reverts the property according to CSS cascade layer rules. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | start |
| Inherited | Yes |
| Applies to | SVG text content elements |
| Computed value | As specified |
| Animation type | Discrete |
| Percentages | Not accepted |
| Related SVG features | x, y, dx, dy, direction, writing-mode |
| Specification | Scalable Vector Graphics (SVG) 2 |
Valid Examples
.svg-label {
text-anchor: start;
}
.chart-label {
text-anchor: middle;
}
.axis-label {
text-anchor: end;
}
svg text {
text-anchor: middle;
}
Invalid Examples
text {
text-anchor: center;
}
text {
text-anchor: left;
}
text {
text-anchor: right;
}
text {
text-anchor: 50%;
}
text {
text-anchor: 20px;
}
Unlike text-align, the text-anchor property uses middle instead of center. It does not accept left, right, percentages, or lengths.
How It Works
SVG text is positioned using coordinates. The text-anchor property determines how a text chunk is aligned relative to those coordinates.
Step 1: Create SVG Text
<svg viewBox="0 0 300 100" xmlns="http://www.w3.org/2000/svg">
<text x="150" y="50">SVG Text</text>
</svg>
The x attribute establishes the horizontal text position, while y establishes the baseline position in this horizontal writing example.
Step 2: Apply Start Alignment
text {
text-anchor: start;
}
For left-to-right text, the text begins at the specified x coordinate and extends to the right.
Step 3: Apply Middle Alignment
text {
text-anchor: middle;
}
The text is centered horizontally around the specified coordinate.
Step 4: Apply End Alignment
text {
text-anchor: end;
}
For left-to-right text, the text ends at the specified coordinate and extends to the left.
Important Consideration
The anchor position does not change when text-anchor changes. Instead, the text is repositioned relative to that anchor.
Understanding the Anchor Position
The anchor position is the reference point used to align an SVG text chunk. It is commonly established with the x and y attributes of an SVG <text> element.
Horizontal Position
<text x="150" y="50">Example Text</text>
In horizontal writing, the x coordinate determines the horizontal anchor position.
Vertical Position
<text x="150" y="80">Example Text</text>
The y coordinate establishes the baseline position. The text-anchor property does not vertically center text around this coordinate.
Using the Same Anchor With Different Values
<svg viewBox="0 0 300 150" xmlns="http://www.w3.org/2000/svg">
<text x="150" y="30" text-anchor="start">Start</text>
<text x="150" y="75" text-anchor="middle">Middle</text>
<text x="150" y="120" text-anchor="end">End</text>
</svg>
All three text elements use the same horizontal anchor coordinate, but their text extends differently around that point.
Text Chunks
SVG can divide text into separately positioned text chunks. The text-anchor property aligns each chunk relative to its own anchor position rather than necessarily aligning an entire multi-position text element as one unit.
Start Alignment
The start value aligns the beginning of the text chunk with its anchor position.
CSS Example
.start-label {
text-anchor: start;
}
SVG Example
<svg viewBox="0 0 300 80" xmlns="http://www.w3.org/2000/svg">
<text class="start-label" x="50" y="45">Start Label</text>
</svg>
For horizontal left-to-right text, the first character begins at x="50", and the remaining text extends to the right.
Default Behavior
Because start is the initial value, SVG text uses this alignment unless another value is specified or inherited.
When to Use start
Use start when labels should begin at a specific coordinate, such as text positioned to the right of a diagram marker.
Middle Alignment
The middle value centers an SVG text chunk around its anchor position along the inline direction.
CSS Example
.centered-label {
text-anchor: middle;
}
SVG Example
<svg viewBox="0 0 300 80" xmlns="http://www.w3.org/2000/svg">
<text class="centered-label" x="150" y="45">Centered Label</text>
</svg>
The text is centered around x="150" in horizontal writing.
Centering a Chart Label
.chart-value {
text-anchor: middle;
}
This is useful when a label must be centered over a chart bar, marker, or other graphical element.
Important Consideration
The middle value centers text along its inline direction. It does not vertically center the text relative to its baseline. For vertical positioning, consider the SVG dominant-baseline property.
End Alignment
The end value aligns the end of an SVG text chunk with its anchor position.
CSS Example
.end-label {
text-anchor: end;
}
SVG Example
<svg viewBox="0 0 300 80" xmlns="http://www.w3.org/2000/svg">
<text class="end-label" x="250" y="45">End Label</text>
</svg>
For horizontal left-to-right text, the final character ends at x="250", and the text extends to the left.
Aligning Axis Labels
.axis-label {
text-anchor: end;
}
This is useful for labels positioned immediately before an axis or reference line.
When to Use end
Use end when the final edge of a text chunk must line up with a specific coordinate.
Text Direction and Writing Mode
The meaning of start and end depends on the text direction and writing mode.
Left-to-Right Text
.ltr-label {
direction: ltr;
text-anchor: start;
}
In horizontal left-to-right text, start places the beginning of the text at the anchor, and the text extends to the right.
Right-to-Left Text
.rtl-label {
direction: rtl;
text-anchor: start;
}
In horizontal right-to-left text, start aligns the beginning of the text chunk at the right side of its inline progression.
Direction Comparison
| Value | LTR Horizontal Text | RTL Horizontal Text |
|---|---|---|
start |
Text extends right from the anchor. | Text extends left from the anchor. |
middle |
Text is centered around the anchor. | Text is centered around the anchor. |
end |
Text extends left from the anchor. | Text extends right from the anchor. |
Vertical Writing Modes
.vertical-label {
writing-mode: vertical-rl;
text-anchor: middle;
}
For vertical text, the anchor alignment operates along the vertical inline direction rather than the horizontal axis.
Important Consideration
Do not assume that start always means left or that end always means right. Their behavior follows the writing direction.
SVG Text Elements
The text-anchor property is designed for SVG text content. It works with text positioned using SVG coordinates and text layout features.
The text Element
<text x="150" y="50" text-anchor="middle">SVG Label</text>
The <text> element establishes the text content and its position.
The tspan Element
<text x="150" y="30" text-anchor="middle">
<tspan x="150" dy="0">First Line</tspan>
<tspan x="150" dy="24">Second Line</tspan>
</text>
The <tspan> element can position portions of SVG text independently. Explicit positioning can establish separate text chunks that use the applicable anchor alignment.
Text Along a Path
<svg viewBox="0 0 300 120" xmlns="http://www.w3.org/2000/svg">
<defs>
<path id="label-path" d="M 20 90 Q 150 10 280 90"></path>
</defs>
<text text-anchor="middle">
<textPath href="#label-path" startOffset="50%">Curved SVG Text</textPath>
</text>
</svg>
The <textPath> element positions text along a path. The startOffset attribute and text-anchor property work together to position the text chunk along that path.
Using CSS Classes
.svg-label {
font-size: 16px;
fill: #334155;
text-anchor: middle;
}
CSS classes allow multiple SVG text elements to share the same alignment and visual styling.
Presentation Attribute vs. CSS
The SVG text-anchor attribute acts as a presentation attribute. When a matching CSS property declaration applies, the CSS cascade determines the effective value.
text-anchor vs. text-align
The text-anchor and text-align properties both affect text alignment, but they operate in different layout contexts.
| Property | Purpose |
|---|---|
text-anchor |
Aligns SVG text chunks relative to an anchor position. |
text-align |
Aligns inline content within a block container. |
text-align-last |
Controls alignment of the final line and lines before forced breaks in a block container. |
dominant-baseline |
Controls the dominant baseline used for SVG text positioning. |
alignment-baseline |
Controls how an SVG text element aligns with its parent baseline. |
Aligning HTML Text
.html-heading {
text-align: center;
}
Use text-align to center ordinary HTML heading text inside its container.
Aligning SVG Text
.svg-heading {
text-anchor: middle;
}
Use text-anchor to center SVG text around its anchor coordinate.
Centering SVG Text in Both Directions
.svg-centered-label {
text-anchor: middle;
dominant-baseline: middle;
}
The text-anchor property centers the text along its inline direction, while dominant-baseline helps position it along the perpendicular axis.
Practical Examples
Centered Chart Label
.chart-label {
text-anchor: middle;
}
Centers an SVG label over a chart marker or bar.
Axis Labels
.y-axis-label {
text-anchor: end;
}
Aligns the ends of labels to a shared coordinate near an axis.
Diagram Node Labels
.node-label {
text-anchor: middle;
dominant-baseline: middle;
}
Positions text near the center of an SVG diagram node.
Left-to-Right Label
.ltr-label {
direction: ltr;
text-anchor: start;
}
Positions the beginning of a horizontal left-to-right label at the anchor.
Right-to-Left Label
.rtl-label {
direction: rtl;
text-anchor: start;
}
Positions the beginning of a horizontal right-to-left label at the anchor.
Centered SVG Heading
.svg-title {
font-size: 22px;
font-weight: bold;
text-anchor: middle;
}
Centers a title around a specified SVG coordinate.
End-Aligned Values
.value-label {
text-anchor: end;
font-variant-numeric: tabular-nums;
}
Aligns the end of each value label to a common coordinate.
Inherited Alignment
.svg-label-group {
text-anchor: middle;
}
SVG text descendants can inherit the alignment value from a parent group.
Resetting Alignment
.default-label {
text-anchor: initial;
}
Restores the initial start alignment.
text-anchor Example
This example shows how start, middle, and end align SVG text relative to the same horizontal anchor position. The dashed vertical line marks the anchor coordinate for each label.
HTML Example
<svg class="text-anchor-demo" viewBox="0 0 420 220" xmlns="http://www.w3.org/2000/svg" role="img" aria-label="SVG text-anchor alignment comparison">
<line class="anchor-guide" x1="210" y1="15" x2="210" y2="205"></line>
<circle class="anchor-point" cx="210" cy="55" r="4"></circle>
<circle class="anchor-point" cx="210" cy="115" r="4"></circle>
<circle class="anchor-point" cx="210" cy="175" r="4"></circle>
<text class="anchor-label start-label" x="210" y="55">Start Alignment</text>
<text class="anchor-label middle-label" x="210" y="115">Middle Alignment</text>
<text class="anchor-label end-label" x="210" y="175">End Alignment</text>
</svg>
CSS Example
.text-anchor-demo {
display: block;
width: 100%;
max-width: 420px;
height: auto;
margin-inline: auto;
background: #f8fafc;
border: 1px solid #cbd5e1;
}
.text-anchor-demo .anchor-guide {
stroke: #94a3b8;
stroke-width: 1.5;
stroke-dasharray: 5 5;
}
.text-anchor-demo .anchor-point {
fill: #dc2626;
}
.text-anchor-demo .anchor-label {
font-size: 15px;
font-family: Arial, sans-serif;
fill: #1e293b;
direction: ltr;
}
.text-anchor-demo .start-label {
text-anchor: start;
}
.text-anchor-demo .middle-label {
text-anchor: middle;
}
.text-anchor-demo .end-label {
text-anchor: end;
}
The dashed line represents x="210". Each label uses the same anchor coordinate, but the text is positioned differently:
startbegins at the anchor and extends to the right.middleis centered around the anchor.endends at the anchor and extends to the left.
Comparing the Results
| Value | Result for LTR Horizontal Text |
|---|---|
start |
The beginning of the text is at the anchor. |
middle |
The center of the text is at the anchor. |
end |
The end of the text is at the anchor. |
Changing the Anchor Position
<text class="anchor-label middle-label" x="250" y="115">Middle Alignment</text>
Changing the x coordinate moves the anchor position. The text remains centered around the new coordinate.
Changing the Text Direction
.text-anchor-demo .anchor-label {
direction: rtl;
}
For horizontal right-to-left text, the physical placement of start and end reverses. The middle value continues to center text around the anchor.
Centering Text Vertically
.text-anchor-demo .middle-label {
text-anchor: middle;
dominant-baseline: middle;
}
The dominant-baseline property can be used alongside text-anchor to position text relative to the vertical anchor coordinate in horizontal writing.
Accessibility Considerations
SVG text alignment can improve the clarity of diagrams and charts, but visual positioning alone does not communicate meaning to assistive technologies.
Provide Accessible SVG Names
Use an accessible name for meaningful SVG graphics, such as a <title> element referenced by aria-labelledby.
<svg viewBox="0 0 300 100" role="img" aria-labelledby="chart-title" xmlns="http://www.w3.org/2000/svg">
<title id="chart-title">Chart label example</title>
<text x="150" y="50" text-anchor="middle">Chart Label</text>
</svg>
Maintain Readable Text Sizes
Ensure SVG labels remain readable when the graphic is displayed at different sizes.
Use Sufficient Contrast
Choose text and background colors with adequate contrast, especially for labels that convey important information.
Avoid Clipped Labels
Check that labels remain within the visible SVG area. Changing anchor alignment can cause text to extend beyond the viewport.
Support Different Languages
Use the appropriate text direction and test labels with translated content, which may be longer or use different writing directions.
Do Not Rely on Position Alone
When SVG text identifies data points or diagram components, provide sufficient context so the relationship remains understandable.
Preserve Text Content
SVG <text> elements contain actual text. Keep important labels as text when practical rather than converting every label into a graphical outline.
Common Mistakes
Using center Instead of middle
text {
text-anchor: center;
}
The correct value is middle.
Using left or right
text {
text-anchor: left;
}
The property accepts start, middle, and end, not physical left and right keywords.
Applying It to Ordinary HTML Paragraphs
p {
text-anchor: middle;
}
The property is intended for SVG text. Use text-align to align ordinary HTML paragraph text.
Expecting Vertical Centering
The middle value centers text along the inline direction. It does not automatically center the text vertically around the baseline in horizontal writing.
Ignoring the Anchor Coordinate
The property aligns text relative to an existing anchor position. It does not establish the position itself.
Ignoring Text Direction
The start and end values depend on the writing direction and should not be treated as fixed physical sides.
Using Percentages
text {
text-anchor: 50%;
}
Percentages are not valid values for text-anchor.
Confusing the Property With x and y
The x and y attributes establish text coordinates. The text-anchor property determines alignment relative to the anchor position.
Overlooking Inheritance
SVG text elements can inherit text-anchor from their ancestors. An inherited value may affect labels unexpectedly.
Expecting Smooth Animation
The property is discretely animated, so changes between its keywords do not interpolate smoothly.
Forgetting SVG Viewport Boundaries
Text may be clipped if its alignment causes it to extend outside the visible SVG viewport.
Important Considerations
Keep the following points in mind when using text-anchor:
- It controls the alignment of SVG text chunks relative to an anchor position.
- The initial value is
start. - The property is inherited.
- It accepts
start,middle, andend. - The
middlevalue is used for centering SVG text. - The
centerkeyword is invalid. - The
leftandrightkeywords are invalid. - It does not accept lengths or percentages.
- It is intended for SVG text content rather than ordinary HTML paragraphs.
- SVG coordinates establish the text position.
- The property determines alignment relative to that position.
- The
xattribute commonly establishes the horizontal anchor coordinate. - The
yattribute commonly establishes the baseline position. - The property does not vertically center text in horizontal writing.
- The
dominant-baselineproperty can help control perpendicular alignment. - The
startandendvalues respond to text direction. - Vertical writing modes change the direction of inline alignment.
- Separately positioned SVG text chunks can have separate anchor positions.
- The property works with SVG
<text>and<tspan>content. - It can be used with text positioned along SVG paths.
- The SVG presentation attribute provides an alternative to a CSS declaration.
- CSS declarations participate in the cascade and can override presentation attributes.
- It is discretely animated.
- Text length and font metrics affect the resulting visual position.
- Labels can extend outside the SVG viewport and become clipped.
- Test text alignment with different languages and font sizes.
- Use accessible names and descriptions for meaningful SVG graphics.
Browser Support
The CSS text-anchor property is widely supported for SVG text in modern browsers, including Chrome, Edge, Firefox, and Safari.
The start, middle, and end values have longstanding support in SVG implementations. Their visible behavior depends on the SVG text layout, anchor coordinates, writing direction, and font metrics.
For current compatibility information, visit Can I Use? and search for text-anchor.
Feature Detection
@supports (text-anchor: middle) {
.svg-label {
text-anchor: middle;
}
}
Feature queries can test whether a browser recognizes the CSS property and value. They do not guarantee identical SVG text rendering across browsers.
Fallback Behavior
.svg-label {
text-anchor: start;
text-anchor: middle;
}
If the second declaration is unsupported, the earlier valid declaration can remain in effect.
Testing Recommendations
Test the property with different SVG viewports, font sizes, text directions, and text lengths. Also verify alignment when SVG graphics are scaled responsively.
Specifications
The text-anchor property is defined in the Scalable Vector Graphics (SVG) 2 specification.
The specification defines the supported alignment values, text chunk behavior, inheritance, and interaction with SVG text positioning.
For additional documentation and examples, consult MDN Web Docs.
