Keywords: SVG | text centering | text-anchor | dominant-baseline | rectangle text
Abstract: This article provides an in-depth analysis of text centering techniques in SVG rectangles, covering both horizontal and vertical alignment through text-anchor and dominant-baseline properties. It includes practical code examples and explores text wrapping solutions, offering developers a complete technical reference.
Fundamentals of SVG Text Positioning
Understanding SVG's coordinate system is crucial for text positioning. SVG uses a Cartesian coordinate system with the origin at the top-left corner, where the x-axis extends rightward and the y-axis extends downward. Text element positioning is based on initial text positions controlled by specific attributes.
Horizontal Centering Implementation
Horizontal centering is achieved through the text-anchor property, which controls text alignment relative to the initial x-coordinate. When set to middle, the geometric center of the text aligns with the initial x position.
<svg width="200" height="100">
<rect x="0" y="0" width="200" height="100" stroke="red" stroke-width="3px" fill="white"/>
<text x="100" y="50" text-anchor="middle">Centered Text</text>
</svg>
Vertical Centering Implementation
Vertical centering is controlled by the dominant-baseline property, which defines the text baseline position relative to the initial y-coordinate. Using the middle value aligns the vertical center of the text with the initial y position.
<svg width="200" height="100">
<rect x="0" y="0" width="200" height="100" stroke="red" stroke-width="3px" fill="white"/>
<text x="100" y="50" dominant-baseline="middle">Vertically Centered</text>
</svg>
Complete Centering Solution
Combining both horizontal and vertical centering properties achieves perfect text centering. Set the text's x and y coordinates to the rectangle's center point while applying both centering attributes.
<svg width="200" height="100">
<rect x="0" y="0" width="200" height="100" stroke="red" stroke-width="3px" fill="white"/>
<text x="50%" y="50%" dominant-baseline="middle" text-anchor="middle">Perfectly Centered</text>
</svg>
CSS-Based Style Management
For multiple text elements requiring consistent centering styles, CSS provides efficient batch management, enhancing code maintainability.
<style>
.svg-text-center {
text-anchor: middle;
dominant-baseline: middle;
}
</style>
<svg width="200" height="100">
<rect x="0" y="0" width="200" height="100" stroke="red" stroke-width="3px" fill="white"/>
<text x="50%" y="50%" class="svg-text-center">Styled Centering</text>
</svg>
Text Wrapping Techniques
SVG doesn't natively support automatic text wrapping, but similar effects can be achieved through various methods. Manual line breaking using <tspan> elements provides the most straightforward approach.
<svg width="200" height="100">
<rect x="0" y="0" width="200" height="100" stroke="red" stroke-width="3px" fill="white"/>
<text x="50%" y="40%" text-anchor="middle" dominant-baseline="middle">
<tspan x="50%" dy="0">First line of text</tspan>
<tspan x="50%" dy="1.2em">Second line of text</tspan>
</text>
</svg>
Property Value Analysis
The text-anchor property supports three main values: start (left alignment), middle (center alignment), and end (right alignment). dominant-baseline supports values beyond middle, including central, with subtle differences in vertical alignment behavior.
Practical Application Scenarios
This centering technique finds extensive application in data visualization, chart labeling, UI icons, and other scenarios where precise text positioning significantly enhances visual appeal and user experience.
Browser Compatibility Considerations
Modern mainstream browsers provide excellent support for text-anchor and dominant-baseline properties, though older browser versions may require fallback solutions.