CSS math-shift Property
The CSS math-shift property controls the vertical positioning of superscripts in mathematical expressions.
It is primarily used with MathML to determine whether superscripts use normal positioning or a more compact vertical shift. The property accepts two values: normal and compact. It affects mathematical typesetting rather than ordinary HTML text positioning.
math-shift Syntax
The math-shift property accepts one keyword that determines how superscripts are vertically positioned in mathematical notation.
Basic Syntax
selector {
math-shift: normal;
}
Compact Superscript Positioning
math {
math-shift: compact;
}
Target a MathML Superscript
math msup {
math-shift: compact;
}
CSS-Wide Keywords
math {
math-shift: initial;
}
math {
math-shift: inherit;
}
math {
math-shift: unset;
}
math {
math-shift: revert;
}
math {
math-shift: revert-layer;
}
The initial value is normal. The property is inherited, allowing mathematical elements to receive the same positioning behavior from their ancestors.
Property Values
| Value | Description |
|---|---|
normal |
Uses the normal vertical shift for mathematical superscripts. This is the default. |
compact |
Allows a reduced vertical shift for superscripts in mathematical expressions. |
initial |
Resets the property to normal. |
inherit |
Uses the computed value from the parent element. |
unset |
Acts like inherit because the property is inherited. |
revert |
Reverts the property according to cascade origin rules. |
revert-layer |
Reverts the property according to cascade layer rules. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | normal |
| Inherited | Yes |
| Applies to | All elements |
| Computed value | As specified |
| Animation type | Discrete |
| Accepted values | normal, compact, and CSS-wide keywords |
| Primary use | Superscript positioning in MathML |
How It Works
Mathematical expressions frequently use superscripts to represent powers, exponents, and other notation. MathML provides elements such as <msup> to display these structures.
The math-shift property influences the vertical distance between a base expression and its superscript. It allows the mathematical layout engine to select normal or compact positioning.
Normal Positioning
math {
math-shift: normal;
}
The browser uses the normal superscript shift defined by the mathematical layout rules and available font metrics.
Compact Positioning
math {
math-shift: compact;
}
The browser may position superscripts closer to their base expressions, reducing the amount of vertical space required.
Mathematical Font Metrics
Mathematical fonts can contain specialized information for positioning superscripts and subscripts. The browser uses these metrics to calculate suitable shifts and spacing.
The math-shift property does not specify a pixel distance. Instead, it selects a mathematical positioning mode.
normal Value
The normal value uses the standard superscript positioning behavior for mathematical expressions.
Basic Example
math {
math-shift: normal;
}
Target a Superscript Expression
math msup {
math-shift: normal;
}
MathML Example
<math xmlns="http://www.w3.org/1998/Math/MathML">
<msup style="math-shift: normal;">
<mi>x</mi>
<mn>2</mn>
</msup>
</math>
The exponent is positioned using the normal mathematical superscript shift.
When to Use normal
Use normal when standard mathematical typography is preferred or when resetting a previously inherited compact setting.
Because normal is the initial value, an explicit declaration is usually unnecessary unless another rule changes the property.
compact Value
The compact value selects a reduced superscript shift when the mathematical layout engine supports the corresponding positioning behavior.
Basic Example
math {
math-shift: compact;
}
Apply to a Specific Expression
.compact-exponent {
math-shift: compact;
}
MathML Example
<math xmlns="http://www.w3.org/1998/Math/MathML">
<msup style="math-shift: compact;">
<mi>x</mi>
<mn>2</mn>
</msup>
</math>
The compact setting allows the exponent to be positioned using a smaller vertical shift than normal mathematical superscript positioning.
When to Use compact
Use compact when a mathematical expression benefits from tighter superscript placement, such as certain nested or space-constrained expressions.
Important Consideration
Compact positioning does not guarantee a particular vertical distance. The final result depends on mathematical font metrics, the expression structure, and the browser's rendering implementation.
Superscript Positioning
MathML superscripts are positioned according to mathematical typesetting rules rather than ordinary text baseline adjustments.
Simple Superscript
<math xmlns="http://www.w3.org/1998/Math/MathML">
<msup>
<mi>x</mi>
<mn>3</mn>
</msup>
</math>
The <msup> element identifies the first child as the base and the second child as the superscript.
Nested Superscripts
<math xmlns="http://www.w3.org/1998/Math/MathML">
<msup>
<mi>x</mi>
<msup>
<mi>y</mi>
<mn>2</mn>
</msup>
</msup>
</math>
Nested superscripts require additional sizing and positioning calculations. The browser uses mathematical depth, style, and font metrics to produce the final arrangement.
Superscripts and Subscripts
<math xmlns="http://www.w3.org/1998/Math/MathML">
<msubsup>
<mi>x</mi>
<mn>1</mn>
<mn>2</mn>
</msubsup>
</math>
The <msubsup> element contains a base, subscript, and superscript. The math-shift property concerns the superscript shift rather than directly specifying subscript positioning.
Positioning Constraints
Browsers must account for the dimensions of mathematical symbols and avoid inappropriate overlap. Consequently, a compact shift may produce a subtle difference or no obvious change for some expressions.
Relationship with math-style
The math-style property controls whether mathematical notation uses normal or compact layout styling. Although it shares the keyword compact with math-shift, the two properties serve different purposes.
Normal Mathematical Style
math {
math-style: normal;
}
Compact Mathematical Style
math {
math-style: compact;
}
Combined Example
math {
math-style: normal;
math-shift: normal;
}
math .compact-script {
math-style: compact;
math-shift: compact;
}
These declarations select compact mathematical styling and compact superscript shifting for the targeted expression.
Comparing the Properties
| Property | Purpose |
|---|---|
math-style |
Controls normal or compact mathematical layout styling. |
math-shift |
Controls normal or compact superscript vertical shifting. |
Setting math-style: compact does not automatically mean the computed math-shift value becomes compact. Each property has its own computed value.
Relationship with math-depth
The math-depth property controls mathematical nesting depth and can influence the size of nested mathematical content.
Set Mathematical Depth
math {
math-depth: 0;
}
Increase Nested Depth
math .nested-script {
math-depth: add(1);
}
Combine Depth and Shift
math .nested-script {
math-depth: add(1);
math-shift: compact;
}
The math-depth declaration adjusts the mathematical depth, while math-shift selects compact superscript positioning.
Comparing the Properties
| Property | Purpose |
|---|---|
math-depth |
Controls the nesting depth used for mathematical size calculations. |
math-shift |
Controls the vertical shift used for superscripts. |
math-style |
Controls the mathematical layout style. |
These properties can work together, but changing one does not automatically replace the purpose of the others.
Using math-shift with MathML
MathML provides semantic markup for mathematical expressions. The math-shift property is most useful when applied to MathML structures containing superscripts.
Inline MathML
<p>
The expression
<math xmlns="http://www.w3.org/1998/Math/MathML">
<msup>
<mi>x</mi>
<mn>2</mn>
</msup>
</math>
represents x squared.
</p>
Apply CSS to the Expression
math {
font-size: 28px;
}
math msup {
math-shift: compact;
}
Target a Specific Superscript
<math xmlns="http://www.w3.org/1998/Math/MathML">
<msup class="compact-power">
<mi>a</mi>
<mn>4</mn>
</msup>
</math>
.compact-power {
math-shift: compact;
}
Inherited Positioning
math {
math-shift: compact;
}
Because math-shift is inherited, descendants can receive the compact value unless another declaration or a browser style rule changes it.
Important Consideration
MathML already includes built-in rules for positioning mathematical scripts. Use math-shift when the default positioning needs adjustment, rather than applying it to every expression without a specific reason.
math-shift Example
This example compares the normal and compact values using the same MathML superscript expression.
HTML Example
<div class="math-shift-demo">
<div class="math-shift-item">
<p>math-shift: normal</p>
<math xmlns="http://www.w3.org/1998/Math/MathML">
<msup class="normal-shift">
<mi>x</mi>
<mn>2</mn>
</msup>
</math>
</div>
<div class="math-shift-item">
<p>math-shift: compact</p>
<math xmlns="http://www.w3.org/1998/Math/MathML">
<msup class="compact-shift">
<mi>x</mi>
<mn>2</mn>
</msup>
</math>
</div>
</div>
CSS Example
.math-shift-demo {
display: flex;
flex-wrap: wrap;
gap: 16px;
padding: 20px;
background-color: #f1f5f9;
}
.math-shift-item {
flex: 1 1 180px;
min-height: 150px;
padding: 20px;
text-align: center;
background-color: #fff;
border: 1px solid #cbd5e1;
border-radius: 6px;
}
.math-shift-item p {
margin: 0 0 25px;
font-weight: bold;
}
.math-shift-item math {
font-size: 40px;
}
.normal-shift {
math-shift: normal;
}
.compact-shift {
math-shift: compact;
}
math-shift: normal
math-shift: compact
The first expression uses normal superscript positioning. The second requests a compact vertical shift. Depending on the browser and mathematical font, the difference may be subtle or not visibly apparent for this simple expression.
Nested Superscript Example
<math xmlns="http://www.w3.org/1998/Math/MathML">
<msup style="math-shift: compact;">
<mi>x</mi>
<msup>
<mi>y</mi>
<mn>2</mn>
</msup>
</msup>
</math>
Nested superscripts provide another opportunity to observe mathematical shift behavior. The final positioning remains subject to the browser's mathematical layout rules.
Common Mistakes
Using a Numeric Value
math {
math-shift: 10px;
}
The property does not accept lengths or numeric offsets. Use normal or compact.
Using an Invalid Keyword
math {
math-shift: small;
}
The small keyword is invalid. Use compact to request compact superscript positioning.
Confusing math-shift with vertical-align
The vertical-align property controls alignment in supported layout contexts, including inline formatting. It is not a replacement for MathML superscript positioning rules.
Confusing math-shift with math-style
The math-style property controls mathematical layout styling, while math-shift controls the superscript shift mode.
Expecting Compact Mode to Reduce Font Size
The compact value selects a superscript positioning mode. It does not directly specify a smaller font-size.
Expecting Identical Results in Every Browser
Mathematical fonts and browser implementations can produce different visible results. Some expressions may show little or no difference between the two values.
Using math-shift for Ordinary HTML Superscripts
<p>Example<sup>2</sup></p>
Ordinary HTML <sup> elements are not MathML superscript structures. Use appropriate HTML and CSS text styling for ordinary superscript text.
Important Considerations
Keep the following points in mind when using math-shift:
- The property controls the vertical shift mode used for mathematical superscripts.
- Its initial value is
normal. - The property is inherited.
- It accepts
normalandcompact. - The
compactvalue allows reduced superscript shifting. - It does not accept pixel, percentage, or other length values.
- It does not directly control the size of mathematical text.
- It is primarily useful with MathML superscript structures.
- Mathematical font metrics influence the final superscript position.
- The property works independently of
math-styleandmath-depth. - It does not change the semantic structure of mathematical expressions.
- Test MathML expressions using the browsers and mathematical fonts your website supports.
Browser Support
The CSS math-shift property has limited availability and is not Baseline across all major browsers. Support for its compact superscript positioning behavior varies among browser engines.
Browsers that do not support the property ignore the declaration and continue using their default mathematical superscript positioning rules.
For current compatibility information, visit Can I Use? and search for math-shift.
When using the property, test actual MathML superscript expressions because recognizing the declaration does not necessarily guarantee a noticeable visual difference.
Specifications
The math-shift property is defined in the MathML Core specification, which describes how mathematical superscripts are positioned.
The specification defines the normal and compact values and their relationship to mathematical superscript layout.
For additional documentation and compatibility information, consult MDN Web Docs.
