Text placement
SVG Text Alignment: Center Labels in Badges and Diagrams
To center a single line of text inside an SVG rectangle, put the text’s x and y at the rectangle’s center and set text-anchor="middle" and dominant-baseline="central". The first attribute centers the text horizontally around x. The second places the font’s central baseline at y in horizontal text. Neither attribute finds the rectangle for you.
Center a label inside a badge
The rectangle below starts at (20, 12) and measures 120 × 40. Its center is (20 + 120 / 2, 12 + 40 / 2), or (80, 32). The rectangle and text use the same coordinate system, so you can use that point directly.
<svg xmlns="http://www.w3.org/2000/svg" width="160" height="64"
viewBox="0 0 160 64" role="img" aria-label="OK status badge">
<rect x="20" y="12" width="120" height="40" rx="12" fill="#17365d"/>
<text x="80" y="32" text-anchor="middle" dominant-baseline="central"
font-family="system-ui, sans-serif" font-size="16" font-weight="600"
fill="white">OK</text>
</svg>

Save this as badge.svg and open it in a browser. Change OK to Pending and update the aria-label to match. Keep x="80": the longer word grows around the same point. If the label outgrows the badge, widen the rectangle and recalculate its center rather than nudging each word into place.
To see what each attribute does, keep (80, 32) fixed. Start with text-anchor="start" and dominant-baseline="auto". In this left-to-right example, the label starts at x="80" and sits on the usual alphabetic baseline at y="32". Change only text-anchor to middle: the word shifts sideways, but the baseline stays put. Now change dominant-baseline to central to see the vertical shift.
Line up diagram labels along one edge
For a column of labels on the left of diagram markers, center the labels vertically but align their ends horizontally. In this left-to-right example, both labels end at the same anchor, x="100". Their y values match the centers of their respective markers.
<svg xmlns="http://www.w3.org/2000/svg" width="180" height="88"
viewBox="0 0 180 88" role="img" aria-label="Input and Validation labels beside markers">
<path d="M108 26H136 M108 62H136" stroke="#64748b"/>
<g fill="#17365d">
<circle cx="136" cy="26" r="5"/>
<circle cx="136" cy="62" r="5"/>
</g>
<g direction="ltr" text-anchor="end" dominant-baseline="central"
font-family="system-ui, sans-serif" font-size="14" fill="#17365d">
<text x="100" y="26">Input</text>
<text x="100" y="62">Validation</text>
</g>
</svg>
The line starts at x="108", leaving eight SVG units between the text anchor and the start of the line. That is a coordinate gap, not a measurement from the last visible pixel of a letter. As MDN’s text-anchor reference explains, alignment applies to each text chunk, and start and end depend on text direction. If you localize a diagram into a right-to-left language, check which side of the anchor the label occupies instead of assuming end always means right-aligned.
If the text still looks slightly off-center
First check that the text and shape share a coordinate system. A transform on only one of them can move the visible label away from the point you calculated. Next, inspect the text’s computed styles: a CSS text-anchor or dominant-baseline declaration can override the corresponding SVG presentation attribute.
Finally, check the font and the actual label. dominant-baseline selects a baseline, not the midpoint of the visible letters. middle and central are distinct baseline choices, and a browser may use heuristics if the font lacks baseline data. Try auto, middle, and central at the same coordinates with a label such as Ag: the descender makes it easier to see why the visible letters can look uneven around the center. If the label is fixed and needs a small visual adjustment, tune its y only after checking the intended font and output size. If a substituted font changes the result, see the SVG text and font portability guide.
Try the badge in SVG Vector Lab’s online editor. Swap in your longest label, export the SVG, and reopen it in the browser where it will be used. Check the label against the badge center and confirm the exported text still has the intended position, anchor, and baseline.