CSS dominant-baseline Property
The dominant-baseline property determines which baseline is used as the primary reference for positioning and aligning text.
It is particularly useful for SVG text, where text can be aligned using alphabetic, central, mathematical, or other baseline references. The property also participates in CSS inline text layout, although some values and behaviors are primarily relevant to SVG.
Unlike text-anchor, which controls horizontal anchoring in typical SVG text, dominant-baseline controls the baseline reference used for positioning text along the axis perpendicular to its inline direction.
dominant-baseline Syntax
The dominant-baseline property accepts keywords identifying the baseline used for text alignment.
Basic Syntax
selector {
dominant-baseline: auto;
}
Common Values
.text {
dominant-baseline: alphabetic;
}
.text {
dominant-baseline: central;
}
.text {
dominant-baseline: middle;
}
.text {
dominant-baseline: hanging;
}
.text {
dominant-baseline: mathematical;
}
.text {
dominant-baseline: ideographic;
}
Additional Values
.text {
dominant-baseline: text-before-edge;
}
.text {
dominant-baseline: text-after-edge;
}
.text {
dominant-baseline: reset-size;
}
.text {
dominant-baseline: use-script;
}
.text {
dominant-baseline: no-change;
}
Some additional values originate from earlier SVG specifications and have limited or inconsistent browser support. The current SVG specification defines a smaller set of primary values.
CSS-Wide Keywords
.text {
dominant-baseline: inherit;
}
.text {
dominant-baseline: initial;
}
.text {
dominant-baseline: unset;
}
.text {
dominant-baseline: revert;
}
.text {
dominant-baseline: revert-layer;
}
The initial value is auto.
Property Values
The following values identify the dominant baseline or control how the baseline is selected.
| Value | Description |
|---|---|
auto |
Selects the dominant baseline according to the writing mode and applicable layout rules. |
alphabetic |
Uses the alphabetic baseline, commonly associated with Latin text. |
central |
Uses the central baseline, positioned near the center of the text's em box. |
middle |
Uses a baseline derived from the alphabetic baseline and the font's x-height. |
hanging |
Uses the hanging baseline, associated with certain writing systems. |
mathematical |
Uses the mathematical baseline, associated with mathematical typesetting. |
ideographic |
Uses the ideographic baseline, commonly associated with CJK typography. |
text-before-edge |
Legacy SVG value associated with the text's before-edge baseline. |
text-after-edge |
Legacy SVG value associated with the text's after-edge baseline. |
reset-size |
Legacy SVG value that recalculates baseline-related sizing information. |
use-script |
Legacy SVG value intended to select a baseline based on the dominant writing script. |
no-change |
Legacy SVG value intended to preserve the existing dominant baseline selection. |
The legacy values should not be relied upon for new designs without testing. Prefer values defined by the current SVG specification.
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | auto |
| Inherited | Yes |
| Applies to | Text content elements and supported inline formatting contexts |
| Computed value | As specified |
| Animation type | Discrete |
| Percentages | Not applicable |
How It Works
Text is positioned using font metrics and baseline references. The dominant-baseline property determines which baseline is treated as dominant.
Step 1: Create SVG Text
<svg viewBox="0 0 300 100" xmlns="http://www.w3.org/2000/svg">
<text x="20" y="50">Example Text</text>
</svg>
Step 2: Select a Baseline
svg text {
dominant-baseline: middle;
}
Step 3: Position the Text
The browser uses the selected baseline when positioning the text relative to the SVG coordinates.
Change the Baseline
svg text {
dominant-baseline: alphabetic;
}
Changing the baseline changes the text's position relative to its reference coordinate, even though the coordinate itself remains unchanged.
Important Distinction
The property does not directly set the font size, line height, or text alignment along the inline axis. It changes the baseline reference used for alignment.
Understanding Baselines
A baseline is a reference line used to position and align text. Fonts may provide several baseline references for different writing systems and typographic purposes.
Alphabetic Baseline
The alphabetic baseline is the familiar line on which most Latin letters appear to rest. Descenders, such as those in lowercase g and p, extend below it.
Central Baseline
The central baseline is associated with the center of the em box and is useful when text needs to be positioned around a central reference.
Middle Baseline
The middle baseline is based on the alphabetic baseline and approximately half the font's x-height.
Hanging Baseline
The hanging baseline is associated with scripts whose glyphs are traditionally positioned relative to an upper reference line.
Mathematical Baseline
The mathematical baseline provides a reference for mathematical symbols and expressions.
Ideographic Baseline
The ideographic baseline is associated with the alignment of ideographic characters.
Why Baselines Matter
Different baseline choices allow text from different writing systems or typographic contexts to be aligned appropriately.
The exact visible position depends on the font's metrics and the browser's baseline calculations.
auto
The auto value lets the browser select the dominant baseline according to the writing mode and relevant layout rules.
Basic Example
svg text {
dominant-baseline: auto;
}
Horizontal Writing
For horizontal SVG text, the automatic dominant baseline is normally alphabetic.
Vertical Writing
For vertical SVG text, the automatic dominant baseline is normally central.
Practical Uses
Use auto when the browser's default baseline selection is appropriate and no special alignment is required.
alphabetic
The alphabetic value uses the alphabetic baseline as the dominant baseline.
Basic Example
svg text {
dominant-baseline: alphabetic;
}
SVG Example
<svg viewBox="0 0 300 100" xmlns="http://www.w3.org/2000/svg">
<line x1="10" y1="60" x2="290" y2="60" stroke="#cc3333"/>
<text x="20" y="60" style="dominant-baseline: alphabetic;">HTML and CSS</text>
</svg>
How It Works
The alphabetic baseline is positioned at the text's reference coordinate. Most Latin letters appear above the line, while descenders may extend below it.
Practical Uses
This value is useful when SVG text should follow conventional Latin typography.
central
The central value uses the central baseline as the dominant baseline.
Basic Example
svg text {
dominant-baseline: central;
}
SVG Example
<svg viewBox="0 0 300 100" xmlns="http://www.w3.org/2000/svg">
<line x1="10" y1="50" x2="290" y2="50" stroke="#cc3333"/>
<text x="20" y="50" style="dominant-baseline: central;">Centered Text</text>
</svg>
How It Works
The text is positioned using the central baseline, which is associated with the middle of the em box.
Practical Uses
Central alignment is useful for labels positioned around reference lines, particularly in SVG diagrams.
Important Consideration
The central baseline is not necessarily the visual center of the visible glyphs. Font metrics influence the result.
middle
The middle value uses a baseline derived from the alphabetic baseline and the font's x-height.
Basic Example
svg text {
dominant-baseline: middle;
}
SVG Example
<svg viewBox="0 0 300 100" xmlns="http://www.w3.org/2000/svg">
<line x1="10" y1="50" x2="290" y2="50" stroke="#cc3333"/>
<text x="20" y="50" style="dominant-baseline: middle;">Middle Baseline</text>
</svg>
How It Works
The middle baseline is positioned approximately halfway between the alphabetic baseline and the top of the font's x-height.
Practical Uses
This value is commonly used to position SVG labels near the vertical center of icons, shapes, or diagram markers.
Important Consideration
The middle and central values are not identical. Their results depend on different font metrics.
hanging
The hanging value uses the hanging baseline.
Basic Example
svg text {
dominant-baseline: hanging;
}
SVG Example
<svg viewBox="0 0 300 100" xmlns="http://www.w3.org/2000/svg">
<line x1="10" y1="30" x2="290" y2="30" stroke="#cc3333"/>
<text x="20" y="30" style="dominant-baseline: hanging;">Hanging Baseline</text>
</svg>
How It Works
The hanging baseline provides an upper alignment reference used by certain writing systems.
Practical Uses
This value can be useful when aligning text that follows hanging-baseline typographic conventions.
Important Consideration
The visible result depends on the font's available baseline information and browser handling.
mathematical
The mathematical value uses the mathematical baseline as the dominant baseline.
Basic Example
svg text {
dominant-baseline: mathematical;
}
SVG Example
<svg viewBox="0 0 300 100" xmlns="http://www.w3.org/2000/svg">
<line x1="10" y1="50" x2="290" y2="50" stroke="#cc3333"/>
<text x="20" y="50" style="dominant-baseline: mathematical;">x + y = 10</text>
</svg>
How It Works
The mathematical baseline provides a reference associated with mathematical typography.
Practical Uses
It may be useful for mathematical labels and expressions in SVG diagrams.
Important Consideration
Support for mathematical baseline metrics can vary by font and browser. For complex mathematical notation, use appropriate semantic math markup rather than relying on baseline styling alone.
ideographic
The ideographic value uses the ideographic baseline as the dominant baseline.
Basic Example
svg text {
dominant-baseline: ideographic;
}
SVG Example
<svg viewBox="0 0 300 100" xmlns="http://www.w3.org/2000/svg">
<line x1="10" y1="60" x2="290" y2="60" stroke="#cc3333"/>
<text x="20" y="60" style="dominant-baseline: ideographic;">漢字</text>
</svg>
How It Works
The ideographic baseline provides an alignment reference associated with ideographic writing systems.
Practical Uses
This value is useful when working with CJK text in SVG layouts.
Important Consideration
Actual alignment depends on the font's metrics and the browser's handling of ideographic baselines.
Text Edge Values
Earlier SVG specifications included text-before-edge and text-after-edge values for selecting baselines associated with the edges of text.
text-before-edge
svg text {
dominant-baseline: text-before-edge;
}
text-after-edge
svg text {
dominant-baseline: text-after-edge;
}
Historical Behavior
These values were intended to align text using baseline references near its before and after edges.
Current Usage
These are legacy SVG values and are not part of the primary value set in the current SVG specification.
Important Consideration
Browser support and behavior may differ. Avoid depending on these values for new designs unless their behavior has been verified in the target browsers.
reset-size
The reset-size value is a legacy SVG keyword related to recalculating the baseline table using the current font size.
Syntax
svg text {
dominant-baseline: reset-size;
}
Historical Purpose
Earlier SVG specifications used this value to establish a new baseline table while retaining the existing dominant baseline identifier.
Current Usage
This value is not part of the current primary SVG value set and should not be relied upon for modern cross-browser layouts.
Recommended Approach
Use a currently defined baseline keyword and test the alignment with the intended font.
use-script
The use-script value is a legacy SVG keyword intended to select the dominant baseline according to the writing script.
Syntax
svg text {
dominant-baseline: use-script;
}
Historical Purpose
Earlier SVG specifications described script-based baseline selection for text containing different writing systems.
Current Usage
This value is not part of the current primary SVG value set and has limited practical browser support.
Recommended Approach
Use auto when automatic baseline selection is appropriate, or specify a supported baseline keyword when precise alignment is required.
no-change
The no-change value is a legacy SVG keyword intended to preserve the existing dominant baseline selection.
Syntax
svg text {
dominant-baseline: no-change;
}
Historical Purpose
Earlier SVG specifications used this value to retain the inherited baseline table without recalculating its identifier or size.
Current Usage
This value is not part of the current primary SVG value set.
Recommended Approach
For ordinary inheritance, use the default inherited behavior or the CSS-wide inherit keyword.
dominant-baseline vs. alignment-baseline
The dominant-baseline and alignment-baseline properties both participate in text alignment, but they serve different purposes.
dominant-baseline
svg text {
dominant-baseline: alphabetic;
}
Selects the dominant baseline used for the text's baseline table and alignment context.
alignment-baseline
svg tspan {
alignment-baseline: middle;
}
Specifies which baseline of an inline-level box should align with the corresponding baseline in its alignment context.
Comparison
| Property | Purpose |
|---|---|
dominant-baseline |
Establishes the dominant baseline used for text alignment. |
alignment-baseline |
Controls which baseline of an inline-level box participates in alignment. |
Important Consideration
The properties are related but not interchangeable. Their effects depend on the text formatting context and browser support.
dominant-baseline vs. text-anchor
The dominant-baseline and text-anchor properties control different aspects of SVG text positioning.
dominant-baseline
svg text {
dominant-baseline: middle;
}
Controls the baseline reference used along the axis perpendicular to the inline text direction.
text-anchor
svg text {
text-anchor: middle;
}
Controls how text is anchored along the inline direction relative to its positioning coordinate.
Combined Example
.centered-label {
text-anchor: middle;
dominant-baseline: middle;
}
SVG Example
<svg viewBox="0 0 200 100" xmlns="http://www.w3.org/2000/svg">
<circle cx="100" cy="50" r="40" fill="#e8f1f8"/>
<text x="100" y="50" class="centered-label">CSS</text>
</svg>
The combination positions the label around the center of the circle using the selected horizontal anchor and baseline reference.
Important Consideration
Baseline-based centering is not always identical to the visual center of the glyphs. Font metrics influence the result.
SVG Text Positioning
SVG text elements use coordinates to position text within a graphic.
The x Attribute
The x attribute specifies a horizontal positioning coordinate for text in a typical horizontal writing mode.
The y Attribute
The y attribute specifies a vertical positioning coordinate for text in a typical horizontal writing mode.
Baseline Selection
.svg-label {
dominant-baseline: middle;
}
Positioning Example
<svg viewBox="0 0 300 120" xmlns="http://www.w3.org/2000/svg">
<rect x="20" y="20" width="260" height="80" fill="#e8f1f8"/>
<text x="150" y="60" text-anchor="middle" dominant-baseline="middle">Centered Label</text>
</svg>
CSS and SVG Attributes
The dominant-baseline property can be specified through CSS or an SVG presentation attribute.
<text dominant-baseline="middle">Example</text>
text {
dominant-baseline: middle;
}
Important Consideration
CSS declarations and SVG presentation attributes participate in the cascade. A CSS declaration may override a presentation attribute when the cascade gives it higher priority.
Writing Modes
The writing-mode property influences how text is arranged and which baseline is selected automatically.
Horizontal Writing
.horizontal-text {
writing-mode: horizontal-tb;
dominant-baseline: auto;
}
In horizontal SVG writing, the automatic dominant baseline is normally alphabetic.
Vertical Writing
.vertical-text {
writing-mode: vertical-rl;
dominant-baseline: auto;
}
In vertical SVG writing, the automatic dominant baseline is normally central.
Explicit Baseline
.vertical-text {
writing-mode: vertical-rl;
dominant-baseline: ideographic;
}
Important Consideration
Baseline positioning is relative to the writing mode. Do not assume that every baseline adjustment corresponds to vertical movement on the screen.
Font Metrics
Fonts contain measurements that help browsers position glyphs and calculate baseline alignment.
The Em Box
The em box is a font-relative measurement area used in text layout.
The x-Height
The x-height describes the height of typical lowercase letters such as x in many Latin fonts.
Ascenders and Descenders
Ascenders extend upward from the main body of lowercase text, while descenders extend below the alphabetic baseline.
Font Differences
Two fonts with the same font-size may have different baseline metrics and therefore appear differently positioned.
Example
.svg-label {
font-family: Arial, sans-serif;
font-size: 24px;
dominant-baseline: middle;
}
Important Consideration
When precise SVG label placement matters, test with the actual font used in the design rather than assuming identical alignment across fonts.
dominant-baseline Example
This example displays the same SVG text using four different baseline values.
Each label uses the same reference line. Changing dominant-baseline changes how the text is positioned relative to that line.
<svg class="baseline-demo" viewBox="0 0 420 240" xmlns="http://www.w3.org/2000/svg" role="img" aria-label="Comparison of alphabetic, middle, central, and hanging text baselines">
<g class="baseline-row">
<line x1="20" y1="35" x2="400" y2="35"/>
<text x="25" y="35" class="alphabetic-text">Alphabetic</text>
</g>
<g class="baseline-row">
<line x1="20" y1="95" x2="400" y2="95"/>
<text x="25" y="95" class="middle-text">Middle</text>
</g>
<g class="baseline-row">
<line x1="20" y1="155" x2="400" y2="155"/>
<text x="25" y="155" class="central-text">Central</text>
</g>
<g class="baseline-row">
<line x1="20" y1="215" x2="400" y2="215"/>
<text x="25" y="215" class="hanging-text">Hanging</text>
</g>
</svg>
.baseline-demo {
display: block;
width: 100%;
max-width: 420px;
background-color: #f8f9fa;
}
.baseline-demo .baseline-row line {
stroke: #cc3333;
stroke-width: 1;
stroke-dasharray: 5 4;
}
.baseline-demo text {
fill: #333333;
font-family: Arial, sans-serif;
font-size: 22px;
}
.baseline-demo .alphabetic-text {
dominant-baseline: alphabetic;
}
.baseline-demo .middle-text {
dominant-baseline: middle;
}
.baseline-demo .central-text {
dominant-baseline: central;
}
.baseline-demo .hanging-text {
dominant-baseline: hanging;
}
The dashed red lines mark the reference coordinates used by the SVG text elements.
With alphabetic, the text rests on the reference line. The middle and central values position the text around different central baseline references, while hanging uses an upper baseline reference.
The precise position may differ slightly depending on the font and browser.
Accessibility
The dominant-baseline property changes text positioning, but it does not change the text's meaning or reading order.
Readable SVG Text
Choose baseline values that keep labels legible and prevent overlap with nearby shapes or other text.
Text Contrast
Maintain sufficient contrast between SVG text and its background.
Accessible SVG Graphics
Provide an accessible name or text alternative when an SVG graphic conveys information.
Do Not Rely on Position Alone
Important relationships should not depend entirely on the visual position of labels.
Responsive SVG Layouts
Use an appropriate viewBox and responsive sizing so labels remain readable when the graphic scales.
Font and Language Support
Use fonts that support the required characters and writing systems. Baseline alignment cannot compensate for missing glyphs.
Important Considerations
Keep the following points in mind when using dominant-baseline:
- Purpose: Selects the dominant baseline used for text alignment.
- Initial value:
auto. - Inheritance: The property is inherited.
- Primary use: SVG text positioning and supported inline text layout.
- Alphabetic: Uses the familiar baseline associated with Latin text.
- Central: Uses the central baseline associated with the em box.
- Middle: Uses a baseline derived from the alphabetic baseline and x-height.
- Hanging: Uses a hanging baseline reference.
- Mathematical: Uses a mathematical baseline reference.
- Ideographic: Uses an ideographic baseline reference.
- Legacy values: Some earlier SVG keywords have limited or inconsistent support.
- Font metrics: Different fonts can produce different alignment results.
- Writing mode: The effect of baseline selection depends on text orientation.
- text-anchor: Controls anchoring along the inline text direction.
- alignment-baseline: Controls which baseline participates in inline alignment.
- Animation: Changes between values use discrete behavior.
- Accessibility: Text must remain readable and meaningful regardless of baseline positioning.
For SVG labels, alphabetic, middle, and central are useful starting points. Choose the value that matches the intended typographic alignment and test it with the actual font.
Browser Support
The CSS dominant-baseline property is widely supported for common SVG text positioning in modern browsers, including Chrome, Edge, Firefox, and Safari.
Support for individual baseline values and their exact rendering may vary, especially for legacy SVG keywords and specialized writing systems.
Common Example
svg text {
dominant-baseline: middle;
}
SVG Presentation Attribute
<text dominant-baseline="middle">Example</text>
Feature Detection
@supports (dominant-baseline: middle) {
svg text {
dominant-baseline: middle;
}
}
Feature detection can determine whether a browser recognizes a declaration, but it does not guarantee identical baseline calculations or rendering across fonts and browsers.
Compatibility Considerations
Test the specific baseline value and writing system required by the design. Avoid relying on legacy values without confirming their behavior.
For current compatibility information, visit Can I Use? .
Specifications
The dominant-baseline property is defined in the SVG 2 specification, with related baseline concepts described in CSS Inline Layout.
SVG 2: dominant-baseline Property
CSS Inline Layout Module Level 3
These specifications describe baseline selection, text alignment, and the interaction between font metrics and inline formatting.
