Markdown Superscript & Subscript: Syntax, HTML & Workarounds

How to write x2 and H2O in Markdown — and which method works where

Superscript and Subscript Are Not Standard Markdown

Neither CommonMark nor GitHub Flavored Markdown defines syntax for superscript or subscript. Support depends entirely on the processor you use. In practice there are three methods: native extensions (^text^ and ~text~), HTML tags (<sup> and <sub>), and LaTeX math. The HTML tags are the only method that works almost everywhere.

Native Superscript Syntax: ^text^

Some processors, including Pandoc and markdown-it with the sup plugin, treat text between carets as superscript. Obsidian can do the same with a community plugin. It is not part of CommonMark or GFM, so it is not supported on GitHub.

X2  ·  210 = 1024  ·  m2

X^2^
2^10^ = 1024
m^2^

In Pandoc, the text between the carets cannot contain spaces unless you escape them with a backslash (^two\ words^). On a processor without support, the carets appear as literal characters.

Native Subscript Syntax: ~text~

Pandoc and a few other processors use single tildes for subscript. Obsidian can do the same with a community plugin.

H2O  ·  CO2  ·  xi

H~2~O
CO~2~
x~i~

Warning: in GitHub Flavored Markdown, ~text~ means strikethrough, not subscript. The same source renders differently depending on the processor. See The Tilde Conflict below.

HTML Superscript: <sup>

The <sup> tag is the most reliable method. Markdown passes inline HTML through to the output, and every browser knows how to render it. It works on GitHub, GitLab, VS Code, Obsidian, Jupyter, Pandoc, and MDViewer.

E = mc2
10-6 seconds
The theorem was proved in 1995[1].

E = mc<sup>2</sup>
10<sup>-6</sup> seconds
The theorem was proved in 1995<sup>[1]</sup>.

For real footnotes with clickable links, GFM and many other processors support the [^1] footnote syntax. The <sup> approach is a simple manual alternative when footnotes are not available.

HTML Subscript: <sub>

The <sub> tag works the same way and is equally portable. It is the standard answer to “how do I write H2O in Markdown”.

H2O  ·  CO2  ·  C6H12O6
x1 + x2 = xn
log2(8) = 3

H<sub>2</sub>O
CO<sub>2</sub>
C<sub>6</sub>H<sub>12</sub>O<sub>6</sub>
x<sub>1</sub> + x<sub>2</sub> = x<sub>n</sub>
log<sub>2</sub>(8) = 3

Combine both tags when you need a base with an index and an exponent, such as x<sub>i</sub><sup>2</sup>.

LaTeX Method: $X^{2}$ and $X_{2}$

If your tool renders math (KaTeX or MathJax), you can write superscripts with ^ and subscripts with _ inside dollar signs. This works in GitHub, GitLab, Jupyter notebooks, Obsidian, MDViewer, and many documentation tools. It does not work in renderers that lack math support, where the dollar signs and braces show up literally.

x2  ·  x2  ·  xi2  ·  eiπ + 1 = 0

$X^{2}$ and $X_{2}$
$x_{i}^{2}$
$e^{i\pi} + 1 = 0$
$\mathrm{H_2O}$

Use braces for multi-character exponents: $x^{10}$ renders as x10, but $x^10$ raises only the 1. LaTeX is the best choice for real equations and overkill for a single chemical formula.

Practical Examples

Chemical formulas

H2O  ·  CO2  ·  Na+  ·  SO42-

H<sub>2</sub>O
CO<sub>2</sub>
Na<sup>+</sup>
SO<sub>4</sub><sup>2-</sup>

Math

x2 + y2 = z2
E = mc2
an = an-1 + 2

x<sup>2</sup> + y<sup>2</sup> = z<sup>2</sup>
E = mc<sup>2</sup>
a<sub>n</sub> = a<sub>n-1</sub> + 2

Ordinals

The 1st, 2nd, and 3rd place winners

The 1<sup>st</sup>, 2<sup>nd</sup>, and 3<sup>rd</sup> place winners

You can also paste Unicode characters directly: 1ˢᵗ, 2ⁿᵈ, 3ʳᵈ. This works in any processor, but Unicode only covers digits and a limited set of letters, so the HTML tags are more flexible.

Trademark and copyright symbols

Do not use <sup>TM</sup> for trademarks. Use the proper Unicode characters, which work everywhere: ™ (paste ™ or use &trade;), ®, and ©.

References and footnote markers

Markdown was created in 2004[1] by John Gruber.

Markdown was created in 2004<sup>[1]</sup> by John Gruber.

Platform Support

Support for each method varies. HTML tags are the safe choice; native carets and tildes are the risky ones.

MethodGitHubGitLabObsidianVS CodePandocJupyterMDViewer
^text^ (superscript)NoNoPluginExt.YesNoNo
~text~ (subscript)No (strikethrough)No (strikethrough)PluginExt.YesNoNo (strikethrough)
<sup>YesYesYesYesYesYesYes
<sub>YesYesYesYesYesYesYes
LaTeX ($x^{2}$)YesYesYesExt.YesYesYes

Ext. = requires an extension (VS Code’s built-in preview renders HTML only; math needs a KaTeX extension, and markdown-it plugins add ^ and ~). Plugin = requires a community plugin. Pandoc enables ^ and ~ by default in its Markdown flavor, and renders LaTeX math in PDF, DOCX, and HTML (with MathJax or KaTeX). Support can change between versions, so test in your target environment.

The Tilde Conflict

The single tilde is the biggest compatibility trap in this area. The same characters mean two different things depending on the processor:

SourcePandocGitHub (GFM)
H~2~OH2OH2O
~~deleted~~deleteddeleted
H~2~O
~~deleted~~

GFM treats ~text~ and ~~text~~ as strikethrough. Pandoc uses ~text~ for subscript and ~~text~~ for strikethrough. If you write H~2~O for a README and push it to GitHub, readers see H2O. The fix is to use H<sub>2</sub>O, which is unambiguous everywhere. For the strikethrough side, see Markdown Strikethrough.

Common Mistakes

Using ^ or ~ on a platform that does not support them

On GitHub, GitLab, and most other CommonMark-based renderers, X^2^ shows the literal carets. Unless you control the processor, use the HTML tags.

X^2^  ← literal carets on GitHub
X2  ← works everywhere

X^2^             <-- literal text on GitHub
X<sup>2</sup>    <-- works everywhere

Confusing ~ subscript with ~ strikethrough

Writing H~2~O on GitHub produces struck-through text instead of a subscript. Always check how your target platform handles single tildes.

Forgetting the closing marker or tag

A missing closing ^ leaves the caret as literal text. A missing </sup> or </sub> can lower or raise the rest of the paragraph, because the browser keeps the tag open until the enclosing block ends. Every opening needs a matching close.

H<sub>2O          <-- missing </sub>, the O is lowered too
H<sub>2</sub>O     <-- correct

Spaces inside native markers

In Pandoc, ^a b^ is not superscript because the content contains a space. Escape the space (^a\ b^) or use <sup>a b</sup>.

Forgetting braces in LaTeX

$x^10$ raises only the 1. Write $x^{10}$ for a multi-character exponent.

FAQ

How do I write superscript in Markdown?

The most reliable way is the HTML tag: X<sup>2</sup>, which renders as X2. This works on GitHub, GitLab, VS Code, Obsidian, and most other renderers. Some processors such as Pandoc also support the native syntax X^2^, and math-enabled tools support LaTeX: $X^{2}$. Superscript is not part of standard CommonMark or GFM, so plain ^ does nothing on GitHub.

How do I write chemical formulas like H₂O in Markdown?

Use the HTML subscript tag: H<sub>2</sub>O renders as H2O, and CO<sub>2</sub> renders as CO2. For ions, combine tags: Na<sup>+</sup>. If your processor supports LaTeX, $\mathrm{H_2O}$ works too. You can also paste Unicode subscript characters (H₂O) directly, which works everywhere but only covers digits and a few letters.

Does GitHub support superscript and subscript in Markdown?

GitHub supports the HTML tags <sup> and <sub>, and it supports LaTeX math between $ delimiters. It does not support the native ^text^ syntax. The ~text~ syntax is not subscript on GitHub: it renders as strikethrough.

Preview Your Markdown with MDViewer

MDViewer is a native macOS app for reading and previewing Markdown files. Double-click any .md file in Finder and see it rendered instantly — superscript and subscript tags, LaTeX math, tables, code blocks, and more. Lite is free; Pro adds editing, Git history, and PDF export.

Download MDViewer

Requires macOS 13.0 or later. Intel and Apple Silicon.