CSS baseline-shift Property
The baseline-shift property adjusts the baseline position of SVG text and other applicable inline-level SVG content relative to its parent.
It can raise or lower text using the super and sub keywords, or apply a specific vertical offset using a length or percentage. The property is primarily used in SVG text layout rather than ordinary HTML text formatting.
baseline-shift Syntax
selector {
baseline-shift: baseline;
}
The property accepts one keyword, length, or percentage value.
.normal-baseline {
baseline-shift: baseline;
}
.subscript-text {
baseline-shift: sub;
}
.superscript-text {
baseline-shift: super;
}
.raised-text {
baseline-shift: 8px;
}
.lowered-text {
baseline-shift: -8px;
}
.percentage-shift {
baseline-shift: 25%;
}
CSS-wide values such as inherit, initial, revert, revert-layer, and unset are also accepted.
Positive length and percentage values raise the baseline, while negative values lower it in the usual horizontal writing orientation.
Property Values
| Value | Description |
|---|---|
baseline |
Uses the dominant baseline of the parent without an additional baseline shift. |
sub |
Shifts the baseline to the subscript position determined by the font's baseline information. |
super |
Shifts the baseline to the superscript position determined by the font's baseline information. |
<length> |
Moves the baseline by a specified CSS length. Positive values raise it, and negative values lower it in horizontal text. |
<percentage> |
Moves the baseline by a percentage of the computed line height. |
The initial value is baseline. The property is not inherited, although baseline shifts on nested SVG text can accumulate through the text layout hierarchy.
The property is animatable. Numeric shifts can be interpolated, while keyword changes do not produce the same continuous numeric interpolation.
Unlike many sizing properties, baseline-shift accepts negative lengths and percentages.
How baseline-shift Works
A baseline is an alignment reference used to position text. In horizontal writing, letters normally sit along a common baseline, although some characters extend below it.
The baseline-shift property changes the baseline used to position selected SVG text relative to the parent text's baseline.
<svg xmlns="http://www.w3.org/2000/svg" width="300" height="90" viewBox="0 0 300 90">
<text x="20" y="55" font-size="30">
Text
<tspan baseline-shift="10px">Raised</tspan>
</text>
</svg>
The tspan element is raised by 10 pixels relative to the surrounding text.
Baseline Position vs. Element Position
The property changes text alignment within the SVG text layout system. It does not change the element's CSS margins, padding, or positioning coordinates.
Parent Baseline
The shift is measured relative to the applicable baseline established by the parent text content. The result depends on the font, text layout, and baseline information available to the browser.
Writing Direction
For horizontal text, a positive shift normally moves text upward. For other writing modes, the shift follows the relevant baseline direction rather than necessarily moving toward the top of the screen.
The baseline Value
The baseline value is the default. It introduces no additional baseline shift.
.normal-text {
baseline-shift: baseline;
}
SVG text using this value follows the parent text's normal baseline alignment.
Resetting a Shift
.raised {
baseline-shift: 10px;
}
.raised .normal {
baseline-shift: baseline;
}
The nested element does not introduce another shift. However, it remains within the baseline context established by its raised parent.
Use baseline when no additional subscript, superscript, or numeric offset is needed.
The sub Value
The sub value shifts the baseline to a subscript position.
<svg xmlns="http://www.w3.org/2000/svg" width="220" height="80">
<text x="20" y="45" font-size="30">
H<tspan baseline-shift="sub" font-size="18">2</tspan>O
</text>
</svg>
The number 2 is placed below the normal text baseline, producing a chemical formula-style appearance.
Subscript Positioning
The amount of movement depends on the font's subscript metrics and the browser's text rendering behavior.
Subscript Size
The sub keyword changes baseline position but does not automatically guarantee a smaller font size.
.chemical-subscript {
baseline-shift: sub;
font-size: 65%;
}
Combining the shift with a smaller font size creates a more conventional subscript appearance.
Use sub for subscripts in SVG labels, chemical formulas, and other specialized text graphics.
The super Value
The super value shifts the baseline to a superscript position.
<svg xmlns="http://www.w3.org/2000/svg" width="220" height="80">
<text x="20" y="55" font-size="30">
x<tspan baseline-shift="super" font-size="18">2</tspan>
</text>
</svg>
The number 2 appears above the normal text baseline, producing an exponent-style appearance.
Superscript Positioning
The amount of movement depends on font metrics and the browser's implementation of superscript baseline positioning.
Superscript Size
.math-superscript {
baseline-shift: super;
font-size: 65%;
}
The smaller font size makes the superscript visually distinct from the main text.
Common Uses
Superscript positioning is useful for mathematical exponents, footnote markers, and other raised text within SVG graphics.
For ordinary HTML text, the semantic <sup> element is generally more appropriate.
Length Values
Length values provide explicit control over the baseline offset.
.raised-label {
baseline-shift: 8px;
}
.lowered-label {
baseline-shift: -8px;
}
In horizontal text, the first declaration raises the baseline by 8 pixels, while the second lowers it by 8 pixels.
Using Relative Units
.relative-shift {
baseline-shift: 0.4em;
}
The em unit makes the shift relative to the applicable font size, allowing it to scale with the typography.
Using rem Units
.root-relative-shift {
baseline-shift: 0.5rem;
}
The offset is based on the root element's font size.
Using calc()
.calculated-shift {
baseline-shift: calc(0.5em + 2px);
}
The calc() function combines relative and fixed measurements to calculate the shift.
Choosing an Offset
Small offsets are useful for adjusting labels and symbols. Larger offsets can cause text to overlap neighboring content, so test the result at different font sizes.
Percentage Values
Percentage values shift the baseline by a proportion of the computed line height.
.percentage-raised {
baseline-shift: 25%;
}
.percentage-lowered {
baseline-shift: -25%;
}
A positive percentage raises the baseline, while a negative percentage lowers it in horizontal text.
Percentage Calculation
For example, if the relevant computed line height is 40 pixels, a shift of 25% corresponds to 10 pixels.
Percentage vs. Length
Length values provide a direct offset, while percentage values follow the computed line height.
Percentage-based shifts can be useful when text dimensions change and the offset should remain proportional to the line height.
Using baseline-shift with SVG Text
The baseline-shift property is commonly used with the SVG <tspan> element, which allows portions of a text element to be styled independently.
Using a CSS Class
<svg xmlns="http://www.w3.org/2000/svg" width="300" height="100">
<style>
.raised {
baseline-shift: 12px;
fill: #1769aa;
}
</style>
<text x="20" y="60" font-size="28">
Normal <tspan class="raised">Raised</tspan>
</text>
</svg>
The CSS class raises only the text inside the <tspan> element.
Using the SVG Presentation Attribute
<tspan baseline-shift="super">2</tspan>
SVG also provides a baseline-shift presentation attribute. The corresponding CSS property can be used to control the same styling through stylesheets.
CSS Overrides Presentation Attributes
<svg xmlns="http://www.w3.org/2000/svg" width="250" height="90">
<style>
.custom-shift {
baseline-shift: 12px;
}
</style>
<text x="20" y="55" font-size="26">
Text <tspan class="custom-shift" baseline-shift="sub">Shifted</tspan>
</text>
</svg>
The CSS class overrides the presentation attribute because SVG presentation attributes participate in the CSS cascade with lower priority than ordinary author style declarations.
Nested Text Spans
Nested <tspan> elements can introduce additional baseline shifts relative to their parent text context.
Keep nested shifts manageable, since multiple offsets can make alignment difficult to predict.
baseline-shift vs. vertical-align
Both baseline-shift and vertical-align can affect the vertical placement of text, but they are intended for different layout contexts.
| Feature | baseline-shift |
vertical-align |
|---|---|---|
| Primary use | SVG text baseline adjustments | Inline-level and table-cell alignment in HTML |
| Typical elements | SVG text content, especially <tspan> |
HTML inline elements and table cells |
| Subscript and superscript | sub, super |
sub, super |
| Numeric offsets | Lengths and percentages | Lengths and percentages in applicable inline layout |
HTML Superscript
<p>x<sup>2</sup></p>
The HTML <sup> element communicates the superscript relationship semantically.
HTML Subscript
<p>H<sub>2</sub>O</p>
The HTML <sub> element communicates the subscript relationship semantically.
CSS vertical-align
.html-superscript {
vertical-align: super;
font-size: smaller;
}
This is an appropriate CSS approach for visual superscript styling in ordinary HTML inline content.
Do not assume that baseline-shift can replace vertical-align in normal HTML inline layout. Its standardized use is associated with SVG text layout.
Working with font-size
The font-size property controls the size of text, while baseline-shift controls its baseline offset.
Combining a Smaller Font with a Shift
.superscript {
font-size: 60%;
baseline-shift: super;
}
The text is reduced in size and raised to the superscript baseline position.
Using an Explicit Offset
.custom-superscript {
font-size: 0.65em;
baseline-shift: 0.6em;
}
The font size and baseline shift are specified separately, giving greater control over the final appearance.
Font Metrics Matter
Different fonts can have different baseline and superscript metrics. The same sub or super value may therefore produce slightly different visual positions.
Use explicit lengths when precise visual alignment is required, and check the result with the actual font used in the design.
baseline-shift Example
The following example compares normal, raised, lowered, subscript, and superscript baseline positions using SVG text.
Each row includes a dashed reference line to show how the selected text moves relative to the original baseline.
<svg class="baseline-shift-demo" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 480 310" role="img" aria-label="Comparison of normal, raised, lowered, subscript, and superscript SVG text">
<line x1="20" y1="45" x2="460" y2="45" />
<text x="25" y="45">Normal <tspan class="normal">Text</tspan></text>
<line x1="20" y1="105" x2="460" y2="105" />
<text x="25" y="105">Raised <tspan class="raised">Text</tspan></text>
<line x1="20" y1="165" x2="460" y2="165" />
<text x="25" y="165">Lowered <tspan class="lowered">Text</tspan></text>
<line x1="20" y1="225" x2="460" y2="225" />
<text x="25" y="225">H<tspan class="subscript">2</tspan>O</text>
<line x1="20" y1="285" x2="460" y2="285" />
<text x="25" y="285">x<tspan class="superscript">2</tspan></text>
</svg>
.baseline-shift-demo {
display: block;
width: 100%;
max-width: 480px;
height: auto;
background-color: #f4f8fc;
border: 1px solid #c5d8e8;
}
.baseline-shift-demo line {
stroke: #b4c7d7;
stroke-width: 1;
stroke-dasharray: 5 4;
}
.baseline-shift-demo text {
fill: #173b5e;
font-family: Arial, sans-serif;
font-size: 24px;
}
.baseline-shift-demo tspan {
fill: #1769aa;
font-weight: bold;
}
.baseline-shift-demo .normal {
baseline-shift: baseline;
}
.baseline-shift-demo .raised {
baseline-shift: 12px;
}
.baseline-shift-demo .lowered {
baseline-shift: -12px;
}
.baseline-shift-demo .subscript {
baseline-shift: sub;
font-size: 65%;
}
.baseline-shift-demo .superscript {
baseline-shift: super;
font-size: 65%;
}
The normal text remains aligned with its reference baseline. The raised text moves above the line, while the lowered text moves below it.
The subscript and superscript examples demonstrate font-relative baseline positioning combined with smaller text sizes.
The exact positions of the sub and super examples may vary slightly between fonts and browsers.
Important Considerations
Keep the following points in mind when using baseline-shift:
- Primary use: The property is intended for SVG text layout and applicable SVG inline content.
- Initial value: The default is
baseline. - Subscript: The
subkeyword shifts text toward the subscript baseline. - Superscript: The
superkeyword shifts text toward the superscript baseline. - Positive offsets: Positive lengths and percentages raise text in horizontal writing.
- Negative offsets: Negative lengths and percentages lower text in horizontal writing.
- Percentages: Percentage shifts are based on the computed line height.
- Font size: Baseline shifting does not automatically provide the desired smaller font size for subscripts or superscripts.
- Font metrics: The exact position of
subandsupercan vary by font. - SVG presentation attributes: The property can also be expressed using the SVG
baseline-shiftpresentation attribute. - Nested text: Additional baseline shifts can accumulate through nested SVG text content.
- Writing modes: Baseline movement follows the applicable text baseline direction.
- Layout: The property changes baseline alignment rather than the element's margins or padding.
- HTML text: Use
<sub>,<sup>, orvertical-alignfor ordinary HTML subscript and superscript formatting. - Browser differences: Test the result in target browsers, especially when using font-dependent keyword values.
- Accessibility: Preserve meaningful text and appropriate semantics rather than relying on visual baseline position alone to communicate information.
Use baseline-shift when SVG text requires precise baseline adjustments, especially for subscripts, superscripts, labels, and specialized typography.
Browser Support
The baseline-shift property is supported for SVG text in modern browsers, including Chrome, Edge, Firefox, and Safari. Its applicability and rendering details differ from those of properties used for ordinary HTML inline layout.
The baseline, sub, super, length, and percentage values are associated with SVG baseline positioning. Font-dependent positioning may vary between browser engines.
Using a Fallback
When exact SVG text placement is essential, SVG positioning attributes such as dy can provide an alternative to baseline shifting.
<svg xmlns="http://www.w3.org/2000/svg" width="240" height="80">
<text x="20" y="50" font-size="28">
Text <tspan dy="-10" font-size="18">Raised</tspan>
</text>
</svg>
The dy attribute adjusts the text position relative to the current text position. Unlike baseline-shift, it is an SVG text-positioning mechanism rather than a baseline alignment property.
Visit Can I Use? to review current browser compatibility.
Specifications
The baseline-shift property is defined in the Scalable Vector Graphics (SVG) 1.1 specification and described in the SVG 2 text layout specification.
