HTML provides robust capabilities for text formatting, including the ability to create superscript and subscript text. These elements are essential for displaying mathematical expressions, chemical formulas, footnotes, and ordinal numbers in a web document.
This guide will explore how to implement these features using HTML tags and CSS, while also connecting to related topics such as footnotes and exponents in HTML.
Superscript text is formatted to appear slightly above the normal line of text. This text is usually rendered in a smaller font and is typically used for footnotes, exponents, and ordinal indicators.
Subscript text appears slightly below the normal line of text, also often in a smaller font. This is commonly used in chemical formulas and mathematical expressions.
<sup>
TagThe <sup>
tag is used to define superscript text. This tag is straightforward and widely supported across all browsers. Here’s how you can use it:
1<p>Einstein’s famous equation is E = mc<sup>2</sup>.</p>
<sub>
TagThe <sub>
tag is used for subscript text. Similar to <sup>
, it is simple to use and universally supported:
1<p>The molecular formula for glucose is C<sub>6</sub>H<sub>12</sub>O<sub>6</sub>.</p>
In some scenarios, such as fractional notations, both superscript and subscript can be used together:
1<p>The fraction 3<sup>1</sup>/<sub>2</sub> can be written as 3.5.</p>
While the default rendering of <sup>
and <sub>
tags is sufficient for most purposes, you may want to customize their appearance using CSS. The vertical-align property is particularly useful for fine-tuning the positioning of these elements.
1<style> 2 .sup-text { 3 vertical-align: super; 4 font-size: 0.75em; 5 } 6 .sub-text { 7 vertical-align: sub; 8 font-size: 0.75em; 9 } 10</style> 11 12<p>Customized text: E = mc<span class="sup-text">2</span></p> 13<p>Customized text: H<span class="sub-text">2</span>O</p>
The default CSS settings for superscript and subscript are designed to ensure these elements are rendered correctly across most browsers. However, adjusting the font-size and vertical-align properties can help maintain consistent line heights and improve readability:
1sup, sub { 2 font-size: 0.8em; /* smaller font */ 3 line-height: 1; /* normal line */ 4}
Superscript text is often used for footnotes in documents. You can create a footnote by combining the <sup>
tag with an anchor <a>
tag:
1<p>HTML5 is the latest version of HTML<sup><a href="#footnote1">[1]</a></sup>.</p> 2<p id="footnote1">[1] Released in 2014.</p>
Exponents are another common use case for superscript text. Mathematical notations frequently require raising numbers or variables above the baseline:
1<p>The equation for the area of a circle is A = πr<sup>2</sup>.</p>
Understanding and using HTML superscript and subscript tags allows for more precise and semantically correct text formatting. Whether you’re displaying mathematical expressions, chemical formulas, or footnotes, these tags ensure that your content is clear and accessible. By leveraging CSS for additional styling, you can maintain a consistent and professional appearance across your web documents.
Tired of manually designing screens, coding on weekends, and technical debt? Let DhiWise handle it for you!
You can build an e-commerce store, healthcare app, portfolio, blogging website, social media or admin panel right away. Use our library of 40+ pre-built free templates to create your first application using DhiWise.