Skip to content
Tutorials Center
My learning

Topics

Lesson 85 of 122 ยท Graphics

Math with MathML

Write mathematical formulas directly in HTML with MathML. Learn the math element, mi, mn, mo, mrow, fractions, powers, roots, display math and browser support.

Math has its own visual language: fractions stacked on top of each other, small raised exponents, square-root signs that stretch over whatever they cover, brackets that grow to fit a matrix. Ordinary text can't do this well. Writing x^2 + y^2 = r^2 works in a pinch, and HTML's <sub> and <sup> handle simple cases, but a real formula needs real math layout.

MathML (Mathematical Markup Language) is that layout language. Like SVG, it's a separate vocabulary that you can write right inside an HTML page. The browser lays out the formula with proper spacing and font sizes, the math stays as text that can be selected, zoomed and searched, and screen readers can read it aloud. In this lesson you'll learn the building blocks and write fractions, powers, roots, sums and matrices.

The math element

Every formula lives inside a <math> element. By default it's inline, flowing within a sentence. Add display="block" to show it on its own line, centered and with larger operators, the way textbooks present important equations:

Inline and block math

Like inline SVG, MathML inside HTML needs no xmlns attribute. The HTML parser recognizes <math> and treats everything inside as MathML until the closing tag.

Tokens: mi, mn and mo

The smallest pieces of a formula are token elements. Each one says what kind of thing its text is, which affects how it's displayed and spoken:

Element Stands for Examples
<mi> Identifier: variables, constants, function names x, ฯ€, sin
<mn> Number 2, 3.14, 1000
<mo> Operator: anything that acts or separates, including brackets +, =, (, ), โˆ‘, ,
<mtext> Plain text inside a formula if, otherwise

The distinctions matter:

  • A single-letter <mi> is shown in italic, the standard convention for variables. A multi-letter <mi> like sin or log is upright.
  • <mo> adds appropriate space around operators (more around =, less around commas) and lets brackets stretch to the height of what they enclose.
  • Use real Unicode characters for symbols: โˆ’ (minus, U+2212) instead of a hyphen, ร—, ยฑ, โ‰ค, โˆž, โ†’. You can type them directly in a UTF-8 page or use entities like &minus; and &times;.

Grouping with mrow

Most layout elements expect a fixed number of children. A fraction has exactly two: a numerator and a denominator. When one of those parts is more than a single token, wrap it in <mrow> (math row), which groups children into one horizontal row:

HTML
<mfrac>
  <mrow><mi>a</mi><mo>+</mo><mi>b</mi></mrow>
  <mn>2</mn>
</mfrac>

Without the <mrow>, the fraction would see four children instead of two, and the browser would show an error instead of a proper fraction. When a formula looks broken, a missing <mrow> is the first thing to check.

Fractions, powers and roots

These are the layout elements you'll use most:

Element Children Draws
<mfrac> numerator, denominator A fraction with a bar
<msup> base, superscript A power like xยฒ
<msub> base, subscript An index like xโ‚
<msubsup> base, subscript, superscript Both at once, like xโ‚ยฒ
<msqrt> any number (treated as a row) A square root
<mroot> base, index An nth root, like โˆ›x
The quadratic formula

Look at how the pieces nest: the fraction's numerator is an <mrow> that contains a square root, which itself contains a power. The square-root sign stretches to cover everything inside it automatically. Notice also that <msqrt> accepts any number of children, so its content needs no extra <mrow>.

<mfrac> has a linethickness attribute. Setting linethickness="0" removes the bar, which is how binomial coefficients are usually written (inside a pair of stretchy parentheses).

Sums, limits and matrices

Large operators like sums and integrals often carry limits above and below. In display math, use <munderover> (base, under, over) to stack them; <munder> and <mover> handle just one side.

Matrices and aligned equations use a table structure that mirrors HTML tables: <mtable> contains rows (<mtr>), which contain cells (<mtd>).

A sum and a matrix

The square brackets are <mo> elements, so they stretch to the full height of the table. Try changing the inline math at the top to display="block" and back to see how layout adapts.

Less common elements

Beyond the basics, MathML has layout elements for the situations that remain.

  • <mmultiscripts> places scripts on all four corners of a base: <mmultiscripts> base, then post-script pairs (subscript, superscript), then <mprescripts/>, then pre-script pairs. Chemistry isotopes and tensor indices need it. Use <none/> for an empty slot.
  • <mpadded> changes the space an expression takes (width, height, depth, lspace, voffset) without changing how it looks.
  • <mphantom> reserves the space of its content but draws nothing, useful for aligning two expressions.
  • <mstyle> sets presentation attributes (such as displaystyle, mathcolor, scriptlevel) for everything inside it.
  • <merror> marks content as an error. Browsers show it with a visible highlight.
  • <mspace> adds blank space with a width, height and depth.
  • <menclose> draws a notation around its content. The notation attribute takes a space-separated list, for example box, roundedbox, circle, top, bottom, left, right, updiagonalstrike, downdiagonalstrike, verticalstrike, horizontalstrike, longdiv, actuarial, radical, updiagonalarrow, phasorangle and madruwb. It is not part of MathML Core, so Chrome and Edge ignore it, while Firefox supports it. Treat it as optional decoration, or draw the line with CSS (border or text-decoration: line-through) on an <mrow>.
  • <maction> was meant to toggle or highlight parts of an expression with an actiontype, and is supported only in Firefox.
  • <mlabeledtr> is a table row whose first cell is an equation label. It has limited support, so number equations with an <mtd> instead.
  • <mglyph> inserted an image or special glyph inside a token. It is deprecated and removed from MathML Core; use a Unicode character (or an inline SVG) instead.
  • <mfenced> was also removed. Write the fences with <mo> elements, as in the matrix above.
Scripts, padding and a phantom

If a browser doesn't draw one of these, the content still appears as plain text, so nothing is lost for readers.

Styling MathML

MathML elements are styled with ordinary CSS. color, font-size and background work as usual, and you can target the elements by name:

HTML
<style>
  math[display="block"] {
    font-size: 1.25em;
    padding: 0.5em 0;
  }
  .highlight {
    color: #b91c1c;
  }
</style>

<math display="block">
  <mi>y</mi><mo>=</mo><mi class="highlight">m</mi><mi>x</mi><mo>+</mo><mi>b</mi>
</math>

Browsers use a math font to render formulas. Results look best when one is available, and many sites load a dedicated math font, such as Latin Modern Math or STIX Two Math, and apply it with font-family on the math element.

Browser support and MathML Core

MathML has existed since the 1990s, but browser support used to be patchy. Firefox and Safari supported it for many years; Chromium-based browsers (Chrome, Edge, Opera) added it in 2023. To make reliable, interoperable support possible, browser makers agreed on MathML Core, a carefully specified subset of MathML with precise layout rules.

Today every major browser supports MathML Core, and everything in this lesson belongs to it. A few practical consequences:

  • Stick to the elements and attributes covered here, and you can rely on consistent rendering.
  • Older MathML features that aren't part of Core (for example, <mfenced> for brackets or most mathvariant values) may be ignored by some browsers. Use <mo> brackets and real Unicode characters instead.
  • For very old browsers, a JavaScript library can render MathML as a fallback, but in most projects that's no longer needed.

Writing MathML in practice

MathML is verbose. A formula that takes one line in other notations can take twenty lines of tags. That's why most people don't write long formulas by hand:

  • Many authors write formulas in LaTeX notation and use a converter (there are JavaScript tools and build plugins for this) to turn them into MathML when the page is built.
  • Equation editors and some word processors can export MathML.
  • Hand-writing is still fine for short inline expressions like the ones in this lesson, and understanding the structure helps you fix converted output.

Accessibility. Screen readers such as NVDA, JAWS and VoiceOver can read MathML aloud and let users explore a formula piece by piece, though the quality varies between combinations of browser and reader. Never put formulas in images without alternative text. Well-structured MathML, with correct <mi>, <mn> and <mo> tokens and sensible <mrow> grouping, gives assistive technology the best chance. See Accessibility Basics.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How do you make a formula appear on its own line, centered, instead of inside a sentence?

    <math> is inline by default. display="block" renders it as display math on its own line.
  2. 2. Which element should hold the number 42 in a formula?

    <mn> is for numbers, <mi> for identifiers such as variables, and <mo> for operators.
  3. 3. You write an <mfrac> with four children a, +, b and 2. Why does it look broken?

    A fraction needs exactly a numerator and a denominator. Group multi-part numerators or denominators with <mrow>.
  4. 4. Which markup shows x squared?

    <msup> raises its second child as a superscript. <msub> would show xโ‚‚, and <mroot> draws a root.

Summary

  • MathML lets you write real mathematical notation inside HTML with the <math> element; add display="block" for display equations.
  • Token elements: <mi> for identifiers, <mn> for numbers, <mo> for operators and brackets, <mtext> for words.
  • <mrow> groups several items into one, which elements with a fixed number of children need.
  • <mfrac>, <msup>, <msub>, <msubsup>, <msqrt> and <mroot> build fractions, powers, indices and roots; <munderover> and <mtable> handle limits and matrices.
  • All major browsers support MathML Core; use its elements, real Unicode symbols and <mo> brackets for reliable results.

Next lesson: Introduction to Web APIs.