# all·markdown

How to write superscript and subscript in Markdown

Try it - edit the markdown and watch the preview

Visual

Open the full editor Runs in this tab, nothing is uploaded.

Markdown has no superscript or subscript syntax. The portable answer is inline HTML, which almost every renderer passes through:

Water is H<sub>2</sub>O.
Ten squared is 10<sup>2</sup>.

The caret and tilde alternative

Pandoc and some other renderers accept a lighter syntax:

10^2^        superscript
H~2~O        subscript

It is neater to write and it is not portable. Worse, ~text~ collides with GitHub Flavored Markdown’s strikethrough, where two tildes strike text out and one is undefined. If the document might be read anywhere but your own renderer, use the HTML tags.

Formulas beyond a few characters

Hand-written <sub> tags stop being reasonable somewhere around the second fraction. If a document is genuinely mathematical, use a renderer with KaTeX or MathJax support and write LaTeX between dollar signs. That is a different tool for a different job, and it is worth reaching for early.

These tags survive sanitization

<sup> and <sub> carry no behavior, so a sanitizing renderer keeps them. All Markdown sanitizes every document it renders: scripts and event handlers are removed, structural and typographic tags like these are not.

Common questions

Does Markdown have superscript syntax?
Not in the standard, and not in GitHub Flavored Markdown either. The portable answer is the HTML tags <sup> and <sub>, which nearly every renderer passes through.
What about the caret and tilde syntax?
Pandoc and a few other renderers accept ^text^ for superscript and ~text~ for subscript. It is compact but not portable, and the tilde form collides with strikethrough in GitHub Flavored Markdown.
How do I write a chemical formula?
Subscript the numbers with <sub>: H<sub>2</sub>O, CO<sub>2</sub>. For anything more complex than a formula or two, a maths extension such as KaTeX is a better fit than hand-written tags.
Can I use these inside a table or a list?
Yes. Inline HTML works anywhere inline Markdown works, including table cells and list items, because it is passed through rather than parsed as Markdown.
Are the HTML tags safe to use?
In a renderer that sanitizes its output, yes: <sup> and <sub> carry no behavior and survive sanitization. All Markdown sanitizes every document it renders, so these tags are kept and anything executable is removed.

Sources