Subscript and Superscript
Learn how to display characters below or above the normal
text line using the HTML <sub> and
<sup> elements.
Introduction
HTML provides two special text formatting elements for positioning characters relative to the normal text line.
-
<sub>displays subscript text. -
<sup>displays superscript text.
The <sub> Element
The <sub> element displays text as
subscript, which means the text appears slightly below
the normal text line.
Syntax
<sub>Text</sub>
Example
<p>
H<sub>2</sub>O
</p>
Output
H2O
Common Uses of <sub>
Chemical Formula
<p>
H<sub>2</sub>O
</p>
Carbon Dioxide
<p>
CO<sub>2</sub>
</p>
Mathematical Index
<p>
x<sub>1</sub>
</p>
Variable Notation
<p>
a<sub>n</sub>
</p>
The <sup> Element
The <sup> element displays text as
superscript, which means the text appears slightly above
the normal text line.
Syntax
<sup>Text</sup>
Example
<p>
x<sup>2</sup>
</p>
Output
x2
Common Uses of <sup>
Square
<p>
x<sup>2</sup>
</p>
Cube
<p>
x<sup>3</sup>
</p>
Mathematical Power
<p>
10<sup>6</sup>
</p>
Ordinal Numbers
<p>
1<sup>st</sup>
</p>
<sub> vs <sup>
| Feature | <sub> | <sup> |
|---|---|---|
| Position | Below the normal text line. | Above the normal text line. |
| Common Use | Chemical formulas, indices, notation. | Powers, exponents, ordinal notation. |
| Example | H2O | x2 |
Combining Subscript and Superscript
Both elements can be used together when the notation requires both lower and upper positioning.
<p>
X<sub>1</sub><sup>2</sup>
</p>
X12
Mathematical Examples
x2 + y2 = z2
a2 + b2 = c2
103 = 1000
x1 + x2 = x3
Chemical Formula Examples
Water
Carbon Dioxide
Sulfuric Acid
Example 🌍👨🏫
<h2>
HTML Examples
</h2>
<p>
Water:
H<sub>2</sub>O
</p>
<p>
Carbon Dioxide:
CO<sub>2</sub>
</p>
<p>
Area of a square:
a<sup>2</sup>
</p>
<p>
Cube:
x<sup>3</sup>
</p>
HTML Examples
Water: H2O
Carbon Dioxide: CO2
Area of a square: a2
Cube: x3
CSS and Default Appearance
Browsers provide a default visual style for
<sub> and <sup>.
CSS can be used when a different appearance is required.
<span class="custom-sup">
2
</span>
<style>
.custom-sup {
position: relative;
top: -0.5em;
font-size: 75%;
}
</style>
Important Points
-
<sub>places content below the normal text line. -
<sup>places content above the normal text line. - Subscript is commonly used in chemical formulas and indices.
- Superscript is commonly used in exponents and ordinal notation.
- CSS can be used for custom visual styling.
Common Mistakes
-
Using
<sub>when text should actually be superscript. -
Using
<sup>for chemical formulas. - Forgetting that the element represents actual text positioning and meaning.
- Using CSS positioning when the semantic HTML element already represents the intended meaning.
Practice Exercise
- Write H2O using HTML.
- Write CO2 using HTML.
- Write x2.
- Write x3.
- Write x1 and x2.
- Create a page containing at least three chemical formulas and three mathematical expressions.
Summary
The <sub> element displays text below
the normal text line and is commonly used for chemical
formulas and indices. The <sup>
element displays text above the normal text line and is
commonly used for exponents and ordinal notation.