How to write superscript in HTML
Wrap the raised text in <sup>. The browser shows it smaller and raised above the line. Its partner <sub> shows text smaller and lowered:
<sup> and <sub> are inline and can contain other inline elements, so 2<sup>n+1</sup> or x<sub><i>i</i></sub> work too.
Common uses
| You want | Write | Result |
|---|---|---|
| An exponent | 10<sup>3</sup> | 10 to the power of 3 |
| An ordinal | 2<sup>nd</sup> | 2nd with raised letters |
| A footnote marker | text<sup><a href="#n1">1</a></sup> | A raised, clickable 1 |
| A chemical formula | C<sub>6</sub>H<sub>12</sub>O<sub>6</sub> | Glucose |
| An index or base | x<sub>1</sub>, log<sub>2</sub> | Lowered 1 and 2 |
| A trademark | Brand<sup>TM</sup> or Brand™ | Raised TM |
Use the tags when the position is part of the meaning: m<sup>2</sup> and m2 are different things. For small text that is only styled small (a label, a badge), use CSS instead.
Footnotes with sup
A footnote marker is a superscript link to a note at the bottom. Give each note an id and point the link at it:
Click a number to jump to its note, and "Back" to return. Removing the underline from the small link keeps the paragraph clean, while the link color still marks it as clickable.
Keeping line spacing even
By default, a <sup> or <sub> can make its line taller than the lines around it, so a paragraph with a few exponents looks unevenly spaced. Setting line-height: 0 on them fixes it:
The gray box is taller because lines two and four were pushed apart. In the green box every line has the same height. Add that rule to your base stylesheet once and forget about it.
Unicode characters and MathML
For a few common cases there are ready-made characters: ² (²), ³ (³), ¹ (¹), ™ (™) and ½ (½). They need no tag and do not affect line height, but they only exist for some digits and letters.
For real formulas (fractions, roots, exponents of exponents), use MathML, which current browsers render natively:
<msup> takes a base and an exponent; <msub> does the same for subscripts. For a single exponent in a sentence, <sup> is simpler.
Common mistakes
- Using
<sup>to make text small. It raises the text as well and tells readers it is a superscript. Usefont-sizefor small text. - Uneven line spacing. Add
sup, sub { line-height: 0; }. - Writing
x^2orH2Oin finished text. Usex<sup>2</sup>andH<sub>2</sub>Oso the content reads correctly. - Footnote markers that are too small to tap. On phones a single raised digit is a tiny target. Add a little padding to
sup a, or put the notes near the text.
Frequently Asked Questions
How do I write superscript in HTML?
Wrap the raised text in <sup>: x<sup>2</sup> shows x squared, 1<sup>st</sup> an ordinal. The browser draws it smaller and raised above the baseline.
How do I write subscript in HTML?
Use <sub>: H<sub>2</sub>O for water, x<sub>1</sub> for an index. It is drawn smaller and lowered below the baseline.
How do I write x squared in HTML?
Either x<sup>2</sup> or the ready-made character x², which you can also type as the entity ². The character is fine for squares and cubes; <sup> works for any exponent, like 2<sup>n+1</sup>.
Why does <sup> make the line spacing uneven?
The raised text can make its line taller than the others in the paragraph. Fix it with sup, sub { line-height: 0; }, which stops them from adding height to the line.
Can I use CSS instead of <sup>?
vertical-align: super with a smaller font-size looks the same, but <sup> and <sub> carry meaning: "m2" and "m<sup>2</sup>" are different values. Use the tags when the raised or lowered position is part of the content.