Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

alignment-baseline: choose the baseline used for alignment

Align text using a named baseline instead of treating its bounding box as the reference.

Version: CSS Modules

Syntax

alignment-baseline: middle;

alignment-baseline chooses which baseline of a box or SVG text is aligned with the corresponding reference. The baseline value uses the parent’s dominant baseline; alphabetic, ideographic and middle refer to different font-related metrics. This property selects a baseline, whereas baseline-shift adds an offset and dominant-baseline establishes a dominant baseline choice. It is not a replacement for align-items: center.

In SVG text, alignment is relative to the current text position given by coordinates such as x and y. middle is related to the x-height metric, not simply half of the visible glyph bounding box. Font metrics and writing mode matter. The Inline Layout specification also describes other formatting contexts, but successful parsing does not prove that all those contexts behave identically in a target browser.

The example renders Hx in 40px Arial at x=10 and y=60, then changes alphabetic to middle. In the tested browser, getBBox reports the glyph top moving from 24.00 to 34.38 while width and height remain unchanged. The computed value is middle. This is a measured SVG text movement, not a fabricated fixed baseline offset or a claim about every font.

Examples

const ns = 'http://www.w3.org/2000/svg';
const svg = document.createElementNS(ns,'svg');
svg.setAttribute('width','300'); svg.setAttribute('height','140');
const text = document.createElementNS(ns,'text');
text.setAttribute('x','10'); text.setAttribute('y','60');
text.style.cssText = 'font:40px Arial;alignment-baseline:alphabetic';
text.textContent = 'Hx'; svg.append(text); document.body.append(svg);
const before = text.getBBox();
text.style.alignmentBaseline = 'middle';
const after = text.getBBox();
console.log('computed:', getComputedStyle(text).alignmentBaseline);
console.log('glyph top:', before.y.toFixed(2), after.y.toFixed(2));
console.log('middle moved down:', after.y > before.y);
console.log('same glyph size:', before.width === after.width && before.height === after.height);
svg.remove();

Result

computed: middle
glyph top: 24.00 34.38
middle moved down: true
same glyph size: true

Executed in Chromium 157.0.8103.51 with the available Arial font. Exact glyph metrics can differ with font substitution or platform. Verify the required HTML or SVG context and retain a readable fallback; this example establishes SVG behavior only.

Sources