CSS math-depth Property

The CSS math-depth property controls the nesting depth used when rendering mathematical expressions in MathML.

It helps browsers determine how mathematical content should be scaled when expressions contain nested fractions, superscripts, subscripts, and other structures. The property accepts integer values, the add() function, and the auto-add keyword. It works with math-style and font-size to influence the presentation of mathematical notation.

math-depth Syntax

The math-depth property accepts an integer, a relative adjustment using add(), or the auto-add keyword.

Basic Syntax

selector {
  math-depth: 0;
}

Explicit Depth

.math-expression {
  math-depth: 2;
}

Relative Depth

.math-expression {
  math-depth: add(1);
}

Automatic Depth Adjustment

.math-expression {
  math-depth: auto-add;
}

Negative Depth

.math-expression {
  math-depth: -1;
}

CSS-Wide Keywords

.math-expression {
  math-depth: initial;
}
.math-expression {
  math-depth: inherit;
}
.math-expression {
  math-depth: unset;
}
.math-expression {
  math-depth: revert;
}
.math-expression {
  math-depth: revert-layer;
}

The initial value is 0. Unlike many CSS properties, math-depth is inherited.

Property Values

Value Description
<integer> Sets the mathematical depth to a specified integer, including zero or a negative value.
add(<integer>) Adjusts the inherited mathematical depth by the specified integer.
auto-add Increments the inherited depth by one when the element's computed math-style is compact; otherwise, it preserves the inherited depth.
initial Resets the property to 0.
inherit Uses the computed mathematical depth of 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 0
Inherited Yes
Applies to All elements
Computed value An integer
Animation type By computed value type (integer)
Primary use Mathematical typesetting, especially MathML
Related properties math-style, font-size

How It Works

Mathematical expressions often contain smaller expressions nested inside larger ones. For example, a fraction can contain another fraction, or a superscript can contain additional superscripts.

The browser uses mathematical depth to help determine appropriate font sizing for these nested structures.

Depth Zero

math {
  math-depth: 0;
}

A depth of zero represents the base mathematical level.

Increase the Depth

math .nested {
  math-depth: add(1);
}

The nested element receives a depth one level greater than the computed depth of its parent.

Set an Explicit Depth

math .deep {
  math-depth: 3;
}

This establishes depth three regardless of the parent's mathematical depth.

Depth and Rendering

Mathematical depth influences the sizing rules used for mathematical content. Increasing the depth generally produces smaller mathematical text, subject to the browser's math font metrics and minimum-size behavior.

The property does not change the nesting structure of the MathML document. It changes the depth value used during rendering.

Integer Values

An integer sets the mathematical depth directly. The value does not depend on the parent's computed depth.

Base Depth

.math-base {
  math-depth: 0;
}

Depth One

.math-level-one {
  math-depth: 1;
}

Depth Two

.math-level-two {
  math-depth: 2;
}

Negative Depth

.math-larger {
  math-depth: -1;
}

Negative values are permitted. They represent a mathematical depth below zero and may affect mathematical font scaling according to the browser's implementation.

Reset Nested Depth

math {
  math-depth: 2;
}
math .reset-depth {
  math-depth: 0;
}

The nested element explicitly returns to depth zero instead of inheriting depth two.

When to Use Integer Values

Use explicit integers when a particular mathematical component requires a fixed depth independent of its surrounding expression.

add() Function

The add() function adjusts mathematical depth relative to the computed depth of the parent element.

Increase by One

.nested-math {
  math-depth: add(1);
}

Increase by Two

.deeply-nested-math {
  math-depth: add(2);
}

Decrease by One

.shallower-math {
  math-depth: add(-1);
}

Relative Depth Example

.math-parent {
  math-depth: 2;
}
.math-parent .math-child {
  math-depth: add(1);
}

The child computes to depth three because its parent's computed depth is two.

Nested Relative Adjustments

.level-one {
  math-depth: add(1);
}
.level-two {
  math-depth: add(1);
}

When the second element is nested inside the first, each relative adjustment increases the depth by one.

Important Consideration

The add() function used by math-depth is not the same as the general CSS calc() function. Its argument must be an integer.

auto-add Value

The auto-add keyword automatically adjusts mathematical depth based on the element's computed math-style value.

Basic Example

.nested-expression {
  math-depth: auto-add;
}

Compact Mathematical Style

.compact-expression {
  math-style: compact;
  math-depth: auto-add;
}

When math-style computes to compact, auto-add increases the inherited depth by one.

Normal Mathematical Style

.normal-expression {
  math-style: normal;
  math-depth: auto-add;
}

When math-style computes to normal, auto-add retains the inherited depth.

When to Use auto-add

Use auto-add when depth should respond to mathematical styling rather than always increasing by a fixed amount.

Important Consideration

The auto-add value does not automatically count every nested MathML element. Its behavior depends specifically on the computed math-style value.

Relationship with math-style

The math-style property determines whether mathematical expressions use normal or compact styling.

Normal Style

math {
  math-style: normal;
}

Compact Style

math .compact {
  math-style: compact;
}

Combined Example

math {
  math-depth: 0;
  math-style: normal;
}
math .nested {
  math-style: compact;
  math-depth: auto-add;
}

The nested element receives a depth one greater than its parent because its computed mathematical style is compact.

Comparing the Properties

Property Purpose
math-depth Controls the mathematical depth used for size calculations.
math-style Controls whether mathematical layout uses normal or compact styling.

The two properties are related but not interchangeable. Mathematical depth influences sizing, while mathematical style influences the presentation of certain mathematical structures.

Relationship with font-size

The math-depth property interacts with mathematical font sizing. Browsers can use math font metrics to determine appropriate sizes at different depths.

Set the Base Font Size

math {
  font-size: 28px;
  math-depth: 0;
}

Increase Mathematical Depth

math .script {
  math-depth: add(1);
}

The browser may render the nested mathematical content at a smaller size based on the font's mathematical sizing rules.

Explicit Font Size

math .custom-size {
  font-size: 18px;
  math-depth: 1;
}

An explicit font-size declaration can affect the resulting appearance independently of the mathematical depth.

Important Consideration

The exact scaling is not a fixed percentage for every font or browser. Mathematical fonts can provide specialized metrics for script and nested expression sizing.

Using math-depth with MathML

MathML provides semantic elements for mathematical notation. CSS mathematical properties can influence how those elements are displayed.

Simple MathML Expression

<math xmlns="http://www.w3.org/1998/Math/MathML">
  <msup>
    <mi>x</mi>
    <mn>2</mn>
  </msup>
</math>

The <msup> element represents a superscript expression.

Apply Mathematical Depth

math {
  font-size: 28px;
  math-depth: 0;
}
math msup mn {
  math-depth: add(1);
}

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>

MathML layout rules already account for nested scripts. Explicit math-depth declarations are most useful when customizing or overriding those depth calculations.

Fractions

<math xmlns="http://www.w3.org/1998/Math/MathML">
  <mfrac>
    <mn>1</mn>
    <msup>
      <mi>x</mi>
      <mn>2</mn>
    </msup>
  </mfrac>
</math>

Nested fractions and scripts demonstrate why mathematical depth matters. The browser uses MathML structure and mathematical styling rules to size the components appropriately.

math-depth Example

This example compares a mathematical expression at depth zero with the same expression at greater depths. The MathML markup remains unchanged.

HTML Example

<div class="math-depth-demo">
  <div class="math-depth-item">
    <p>math-depth: 0</p>
    <math class="depth-zero" xmlns="http://www.w3.org/1998/Math/MathML">
      <msup><mi>x</mi><mn>2</mn></msup>
    </math>
  </div>
  <div class="math-depth-item">
    <p>math-depth: 1</p>
    <math class="depth-one" xmlns="http://www.w3.org/1998/Math/MathML">
      <msup><mi>x</mi><mn>2</mn></msup>
    </math>
  </div>
  <div class="math-depth-item">
    <p>math-depth: 2</p>
    <math class="depth-two" xmlns="http://www.w3.org/1998/Math/MathML">
      <msup><mi>x</mi><mn>2</mn></msup>
    </math>
  </div>
</div>

CSS Example

.math-depth-demo {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  padding: 20px;
  background-color: #f1f5f9;
}
.math-depth-item {
  flex: 1 1 150px;
  min-height: 130px;
  padding: 15px;
  text-align: center;
  background-color: #fff;
  border: 1px solid #cbd5e1;
  border-radius: 6px;
}
.math-depth-item p {
  margin: 0 0 20px;
  font-weight: bold;
}
.math-depth-item math {
  font-size: 32px;
}
.depth-zero {
  math-depth: 0;
}
.depth-one {
  math-depth: 1;
}
.depth-two {
  math-depth: 2;
}
Browser Result

math-depth: 0

x2

math-depth: 1

x2

math-depth: 2

x2

The examples use the same mathematical expression with different depth settings. Actual size differences depend on the browser's MathML implementation and mathematical font metrics. Some fonts or browsers may show little visible difference between deeper levels.

Relative Depth Example

.math-parent {
  math-depth: 1;
}
.math-child {
  math-depth: add(2);
}

A child element nested inside .math-parent computes to depth three.

Common Mistakes

Using Decimal Values

.math-expression {
  math-depth: 1.5;
}

The property accepts integers, not fractional numbers.

Using an Invalid Keyword

.math-expression {
  math-depth: deep;
}

The deep keyword is not supported. Use an integer, add(), or auto-add.

Confusing add() with calc()

.math-expression {
  math-depth: calc(1 + 1);
}

The defined relative adjustment syntax is add(<integer>). Use math-depth: add(2) to increase the inherited depth by two.

Assuming auto-add Always Increases Depth

The auto-add value increases depth only when the computed math-style is compact.

Assuming math-depth Changes HTML Nesting

The property changes mathematical rendering depth. It does not modify the DOM or MathML element hierarchy.

Expecting Identical Scaling in Every Browser

Mathematical font metrics and browser rendering behavior can influence the visible result. Test with the fonts used on your website.

Using math-depth Instead of font-size

For ordinary text sizing, use font-size. The math-depth property is designed for mathematical typesetting.

Important Considerations

Keep the following points in mind when using math-depth:

  • The property controls mathematical nesting depth used during rendering.
  • Its initial value is 0.
  • The property is inherited.
  • Integer values set an explicit depth.
  • Negative integer values are allowed.
  • add() adjusts depth relative to the parent's computed depth.
  • auto-add depends on the computed math-style value.
  • Mathematical depth can influence font sizing through math font metrics.
  • The property is particularly useful with MathML expressions.
  • It does not change the semantic structure of mathematical markup.
  • Use font-size for ordinary text sizing.
  • Test mathematical expressions with the fonts and browsers your website supports.

Browser Support

The CSS math-depth property is Baseline 2023 - Newly available. It is supported in modern browsers with MathML Core support, including current versions of Chrome, Edge, Firefox, and Safari.

Older browsers may not recognize the property or may differ in mathematical font scaling. Rendering can also vary according to the mathematical fonts installed on the user's system.

For current compatibility information, visit Can I Use? and search for math-depth.

When developing MathML content, test the complete mathematical expression rather than relying only on whether the browser recognizes the property.

Specifications

The math-depth property is defined in the CSS Math Module Level 1 specification.

MathML Core: math-depth

MathML Core defines the mathematical depth model and its relationship to mathematical font sizing and compact mathematical styles.

For additional documentation and examples, consult MDN Web Docs.

MDN Web Docs: math-depth