The HTML <sup> tag specifies superscript text. This is text that appears at the top of a line of text. The <sub> tag represents subscript text. This is text that appears at the bottom of a line of text.
Most of the time when we encounter text, it has a traditional baseline and x-height.
Everything in this text up to this point in this article has a baseline. This baseline is basically the point where an underline would show up if we formatted it with a one, and an x-height. The x-height is the imaginary line where a lowercase letter ends. It gets its name from lowercase x since it fills the space.
In this guide, we’re going to talk about how to define subscript and superscript in HTML. We’ll walk through an example of each of these text formatting topics so you can learn to use them in your code.
When is Superscript and Subscript Text Used?
There are times when we would like to use characters that are a half a character below or above the set text. Subscripts are characters that are set half a character below the text. Superscripts are characters that are set half a character above the text.
In a real-world setting, we use these characters when we’re using math equations, chemical equations, citations or footnotes, for example.
HTML Supertext
To make super text in HTML, use the <sup> tag. This tag raises any text enclosed within to the top of the line. The sup tag does not have any tag-specific attributes.
Let’s take a look at the syntax for this method:
<p>4 <sup>2</sup> = 16</p>
We have enclosed the “2” character within <sup> tags. This makes the “2” appear in superscript. Our code now shows:
4 ² = 16
HTML Subtext
The HTML <sub> command defines subtext. Subtext refers to text that appears toward the bottom of a line of text.
Consider this syntax:
<p>This is main text. <sub>3</sub></p>
Our code returns:
This is main text. ₃
Like in the super text example, we have not specified any tag attributes for <sub>. To move text into subtext, we enclose that text within a <sub> tag.
Subtext is often used to describe element numbers on the periodic table of elements.
HTML Subtext and Supertext Example
Let’s walk through an example of both <sub> and <sup> on an HTML web page. Consider the following code:
<!DOCTYPE html> <html lang="en"> <head> <style> </style> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Superscript and Subscript</title> <style> sub { vertical-align: sub; font-size: smaller; } sup { vertical-align: super; font: smaller; } </style> </head> <body> <div style="text-align:center;" class="container"> <div> <h3>Superscript</h3> <span>E=MC<sup>2</sup></span> <br /> <span>1.7 x 10<sup>4</sup></span> </div> <div> <h3>Subscript</h3> <span>H<sub>2</sub>O</span> <br /> <span><sup>†1</sup>Ipsum, Lorem. Footnote example, 2020.</span> </div> </div> </body> </html>
As you can see, we use <sub> and <sup> subscript tags and superscript tags to create the effect on the <span> HTML element. It comes with some default values:
<style> sub { vertical-align: sub; font-size: smaller; } sup { vertical-align: super; font: smaller; } </style>
Both tags use event and global attributes that are available to us in HTML. Basically this means that you can set an event handler on it if someone were to click on the text.
In addition, you can use any of the regular HTML attributes to customize the elements. You can find a list of the regular HTML attributes available to us in the Global Attributes list written by W3Schools.
Conclusion
The HTML <sub> element adds subtext to a paragraph of text. Subtext appears at the bottom of a line of text. <sup> adds super text to text, which appears at the top of a line of text.
That’s it! You can now successfully create superscripts and subscripts in HTML.
Do you want to learn more about HTML? Check out our complete How to Learn HTML guide for expert tips and guidance on top online courses and books.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.