HTML Subscript and Superscript

HTML provides two useful elements for displaying text below or above the normal text line: <sub> and <sup>. The <sub> element creates subscript text, while the <sup> element creates superscript text.

These elements are commonly used for chemical formulas, mathematical expressions, powers, footnotes, dates, and other content where text needs to appear slightly below or above the normal text.

What Is HTML Subscript?

The HTML <sub> element defines subscript text. Subscript text appears slightly below the normal text line and is usually displayed in a smaller font size.

For example, H<sub>2</sub>O represents the chemical formula for water.

The number 2 is placed below the normal text.

HTML Subscript Syntax

The basic syntax is:

<sub>Subscript Text</sub>

For example:

<p>Water is H<sub>2</sub>O.</p>

Here, the number 2 is displayed as subscript.

Subscript Examples

Subscript is commonly used in chemical formulas.

For example:

<p>Carbon dioxide is CO<sub>2</sub>.</p>

Another example is:

<p>Sulfuric acid is H<sub>2</sub>SO<sub>4</sub>.</p>

These examples make chemical formulas easier to represent correctly in HTML.

What Is HTML Superscript?

The HTML <sup> element defines superscript text. Superscript text appears slightly above the normal text line and is usually displayed in a smaller font size.

For example, x<sup>2</sup> represents x squared.

HTML Superscript Syntax

The basic syntax is:

<sup>Superscript Text</sup>

For example:

<p>2<sup>3</sup> = 8</p>

Here, the number 3 appears above the normal text.

Superscript Examples

Superscript is commonly used for mathematical powers.

For example:

<p>x<sup>2</sup> + y<sup>2</sup> = z<sup>2</sup></p>

It can also be used for ordinal numbers:

<p>He finished 1<sup>st</sup> in the competition.</p>

Another common use is for footnote references:

<p>This information is explained in more detail later.<sup>1</sup></p>

Subscript vs Superscript

The main difference between <sub> and <sup> is their position.

ElementPurposePositionExample
<sub>Subscript textBelow the text lineH<sub>2</sub>O
<sup>Superscript textAbove the text linex<sup>2</sup>

Use <sub> when the text needs to appear below the normal line and <sup> when it needs to appear above it.

Using Subscript and Superscript Together

You can use both elements in the same HTML document.

<!DOCTYPE html>
<html>
<head>
  <title>HTML Subscript and Superscript</title>
</head>

<body>

  <h1>HTML Subscript and Superscript</h1>

  <p>Water: H<sub>2</sub>O</p>

  <p>Carbon dioxide: CO<sub>2</sub></p>

  <p>Square: x<sup>2</sup></p>

  <p>Cube: x<sup>3</sup></p>

</body>
</html>

The browser positions the subscript and superscript text automatically.

Subscript and Superscript with CSS

You can customize the appearance of these elements with CSS.

<style>
  sub {
    color: blue;
  }

  sup {
    color: red;
  }
</style>

<p>H<sub>2</sub>O</p>
<p>x<sup>2</sup></p>

CSS can change properties such as color, font size, and other visual styles.

However, you should use <sub> and <sup> because of the meaning and position of the content, rather than simply using them to make text smaller or move it visually.

Subscript and Superscript in Mathematics

Superscript is frequently used to represent powers and exponents.

For example:

x<sup>2</sup>

x<sup>3</sup>

10<sup>5</sup>

A complete example is:

<p>Area of a square = a<sup>2</sup></p>

Subscript can also be used when mathematical notation requires it.

For example:

x<sub>1</sub> + x<sub>2</sub> = x<sub>3</sub>

Subscript and Superscript in Science

Subscript is particularly important for representing chemical formulas.

Examples include:

FormulaHTML Representation
Hâ‚‚OH<sub>2</sub>O
COâ‚‚CO<sub>2</sub>
Oâ‚‚O<sub>2</sub>
Hâ‚‚SOâ‚„H<sub>2</sub>SO<sub>4</sub>

Superscript can also be used for scientific notation.

For example:

<p>3 × 10<sup>8</sup> m/s</p>

This represents the approximate speed of light in a vacuum.

Common Mistakes

One common mistake is using <sub> or <sup> simply to change the visual position of ordinary text.

For example, if you only want smaller text, use CSS or <small> rather than <sub>.

The <sub> and <sup> elements should be used when the content actually has a subscript or superscript meaning.