Raise or lower text with sup and sub
<sup> stands for superscript: smaller text set above the line. <sub> is
subscript: smaller text set below it. Both work inside a sentence, around one character or several.
The examples run in a locked box: scripts are off, nothing is sent anywhere, and your edits go away when you reload. To keep one, copy it into the HTML Editor.
Superscript with <sup>
Use <sup> where writing rules call for raised text: powers like 210, units like
cm3, and the letters after a number in dates like 1st or 3rd. The browser makes the
text a bit smaller and lifts it up. That’s all it does, so the text inside can be anything: a number, a letter, a
whole word.
The ordinal letters (st, nd, rd, th) are a matter of taste. Many style guides now write a plain “1st”, which is easier to read on screens. Both are fine HTML.
Look at the marks in the second example. ™ (™) is already a small raised character, so it
doesn’t need a <sup>. Wrapping it makes it tiny. ® (®) is different: many
fonts draw it at full size on the line, which is why people often put it in <sup>. Whether that
looks better depends on your font.
Subscript with <sub>
<sub> drops the text below the line. You’ll mostly see it in chemistry (H2O,
CO2), in math for the base of a logarithm (log2) and for numbered variables like
x1 and x2.
For ions you need both: the number of atoms below and the charge above. Writing
<sub>4</sub><sup>2−</sup> puts them one after the other, which is
how most web pages do it. If you need them stacked exactly above each other, that’s a job for MathML (see
math formulas below).
Write squared and cubed: ² and ³
Unicode has ready-made raised and lowered digits, so you don’t always need a tag. Three of them have easy names in
HTML: ¹ (¹), ² (²) and ³ (³). The others only have
number codes, like ₂ for the subscript ₂:
| You want | Character | HTML code |
|---|---|---|
| Squared | ² | ² or ² |
| Cubed | ³ | ³ or ³ |
| Superscript 1 | ¹ | ¹ or ¹ |
| Superscript 0 | ⁰ | ⁰ |
| Superscript 4 to 9 | ⁴ ⁵ ⁶ ⁷ ⁸ ⁹ | ⁴ to ⁹ |
| Subscript 0 to 9 | ₀ ₁ ₂ ₃ ₄ ₅ ₆ ₇ ₈ ₉ | ₀ to ₉ |
| Superscript + and − | ⁺ ⁻ | ⁺ and ⁻ |
You can also type the character straight into your HTML file, as long as the page is saved as UTF-8 (it almost always is). The HTML Entities page lists ², ³ and the other common symbols with a copy button.
Characters or tags?
- Characters (² ₂) are part of the text. They survive copy and paste into an email, a chat or a spreadsheet, and they don’t change the line height. But they only exist for digits, a few signs and a handful of letters.
- Tags (
<sup>,<sub>) work for any text: 2n, footnote numbers, 10−12. Paste the result somewhere without formatting, though, and m2 turns into “m2”.
So for a single squared or cubed unit, the character is the easy choice. For anything longer, or anything with
letters, use the tag. One small catch with the characters: ¹ ² ³ come from an older character set than ⁴ to ⁹, and
in some fonts they sit at a slightly different height. If a list of powers looks uneven, switch to
<sup>.
Stop sup and sub from stretching the line
You’ll notice this one on any page with a footnote or a formula in a paragraph. A raised or lowered piece of text needs room, so the browser makes that line a little taller than the others. In a paragraph the gaps between lines become uneven. The example has ruled lines in the background, so you can see the text drift:
The short fix is sup, sub { line-height: 0; }. The raised text no longer asks for extra room,
so every line keeps the same height. If you want more control over how high or low the text sits, this longer
version is a popular one. It moves the text with position: relative instead, which never changes the
line:
sup, sub {
font-size: 75%;
line-height: 0;
position: relative;
vertical-align: baseline;
}
sup { top: -0.5em; }
sub { bottom: -0.25em; }
Change top: -0.5em to lift superscripts higher or lower. Put either fix in your main stylesheet once
and every <sup> and <sub> on the site follows it.
Change the size, color and height
<sup> and <sub> take CSS like any other text. font-size changes the
size (the default is smaller, about 83% of the text around it), color the color.
The height comes from vertical-align: super and sub are what the tags use, and
you can give a length like 0.4em instead.
The message count is a <span> with vertical-align: super, not a
<sup>. That’s on purpose: the HTML spec says to use <sup> and
<sub> only where writing rules need raised or lowered text, not for decoration. For a raised look
with no meaning behind it, use CSS.
Footnote numbers that link to the note
A footnote number is a small link inside a <sup>. Give it an href that points to the
note’s id, and give the note a link back. Click the little 1 in the example:
The li:target rule highlights the note you jumped to, so it’s easy to spot on a long page. The ids
have to be unique on the page: note-1, note-2 and so on. A link that only says “1” doesn’t
tell a screen reader user much, so some sites add aria-label="Note 1" to it.
Math formulas and MathML
For a simple power, x<sup>2</sup> is fine. Once you need fractions, roots or numbers stacked
above each other, use MathML: HTML’s own tags for math. It works in all the main browsers now (Chrome and Edge
since early 2023, Firefox and Safari for much longer), with no extra files.
Inside <math>, <mi> is a letter, <mn> a number and
<mo> a sign. <msup> takes two children, the base and the power, and
<msub> does the same for an index. <mfrac> stacks one over the other. For the
ion from earlier, <msubsup> takes a base, a subscript and a superscript, and lines the last two
up above each other.
A word on screen readers: with their default settings, most of them don’t say “superscript”, so x2 can be read as “x 2”. Screen readers that understand MathML can read the formula as math (“a squared plus b squared”). For a single power in normal text, writing it out nearby (“x squared”) is the safe bet.
Common mistakes
- Forgetting
</sup>. Everything after it stays raised, up to the end of the paragraph. The HTML Validator points out tags that were never closed. - A hyphen for minus. In 10-3 the hyphen is short and easy to miss. Use
−(−), as in the second line of the example. - ™ inside
<sup>. It’s already raised, so it ends up tiny. Leave it as it is. - Using sup for small print. Fine print, like “Prices include tax”, goes in
<small>, not<sup>. And a sup inside a sup (222) works, but it gets small fast; that’s another sign you want MathML.
The HTML Tags reference has <sup> and <sub>
next to the other text tags, and the font and font size tutorial covers sizing
text in general.
Questions people ask
How do I write superscript in HTML?
Wrap the raised text in <sup>: <p>The area is 25 m<sup>2</sup>.</p>. The browser makes it a little smaller and lifts it above the line. It works for numbers, letters and whole words, like 2<sup>n</sup> or 1<sup>st</sup>.
How do I write subscript in HTML?
Wrap the lowered text in <sub>: <p>Water is H<sub>2</sub>O.</p>. It is used for chemical formulas like CO<sub>2</sub>, the base of a logarithm like log<sub>2</sub>, and numbered variables like x<sub>1</sub>.
How do I write squared (²) or cubed (³) in HTML?
Use the tag, m<sup>2</sup>, or the ready-made characters: ² gives ² and ³ gives ³. The characters keep working when the text is copied somewhere without formatting, and they do not change the line height. You can also type ² and ³ straight into a UTF-8 page.
Is there an HTML entity for subscript 2?
There is no named entity for any subscript digit, but every one has a number code: ₂ gives ₂, and the digits ₀ to ₉ are ₀ to ₉. Only ¹, ² and ³ have names (¹, ², ³); the other superscript digits use number codes too, like ⁴ for ⁴.
Why does <sup> make the line spacing uneven?
The raised text needs room, so the browser makes that line a bit taller than the others. Add sup, sub { line-height: 0; } to your CSS and every line keeps the same height. Another common fix moves the text with position: relative and top: -0.5em instead of vertical-align, which never changes the line.
How do I make superscript text smaller or change its color?
Style the tag like any other text: sup { font-size: 0.65em; color: #b91c1c; }. The default size is "smaller", about 83% of the text around it. To change how high it sits, use vertical-align with a length like 0.4em, or the position: relative fix.
How do I make a footnote number that links to the note?
Put a link inside the <sup> and point it at the note's id: <sup><a href="#note-1" id="ref-1">1</a></sup>. In the note, add a link back with href="#ref-1". Each id must be unique on the page, so number them note-1, note-2 and so on.
Should I use <sup> for math formulas?
For a simple power like x<sup>2</sup>, yes. For fractions, roots, or a subscript and superscript stacked on the same letter, use MathML, which all major browsers support: <math><msup><mi>x</mi><mn>2</mn></msup></math>. Screen readers that understand MathML can also read it as math.
Do screen readers read superscript and subscript?
They read the text, but with their default settings most do not say "superscript" or "subscript", so x<sup>2</sup> may come out as "x 2". If the meaning matters, write it in words nearby ("x squared") or use MathML for formulas.