How to Use sub Tag for Subscript Text in HTML

html sub tag

The sub tag in HTML shows text in subscript form. It places the text slightly below the normal baseline of a line.

You can use this tag in science or math content where small numbers or letters show under the main text.

Understand the <sub> Tag in HTML

The <sub> tag sets part of the text below the normal phrase line and tells the browser that this text has a smaller font size than the rest.

You must use <sub> with both the open and close tags. The code looks like this:

This is the <sub>subscript</sub>

The word inside the tag drops below the line while other text stays normal.

Writers use subscripts in formulas and math. You can also use it in footnotes or citations to keep text short.

Browsers drop the text lower than the baseline and reduce its size. This style works across most modern browsers without extra setup.

How to Style the Subscript with CSS

The <sub> tag has a default style, but you can use CSS to change it. You can customize styles or even shift the placement lower with vertical-align.

sub {
  font-size: 12px;
  color: red;
  vertical-align: -0.5em;
}

This code sets the subscript text to a small red font and moves it lower.

Examples of <sub> Tag in HTML

Subscript in a Chemical Formula:

H<sub>2</sub>O

This code shows water as H₂O. The number two moves below the main line to show two hydrogen atoms.

Subscript in Mathematical Index:

X<sub>1</sub> + X<sub>2</sub>

This example shows two variables X₁ and X₂. The numbers under the letters tell the order of variables.

Custom CSS Style for Subscript:

<p>Formula: a<sub style="color:blue; font-size:14px;">n</sub></p>

This code shows aₙ with a styled subscript. The letter “n” drops below the line and has custom styles.

Subscript with Nested Content:

<p>Note: CO<sub><em>2</em></sub></p>

This code shows CO₂ with an italic format inside the subscript. The <em> tag works inside <sub>, hence you can style the inner subscript differently.

Wrapping Up

You learned how the <sub> tag works and how CSS can change the styles.

Here is a quick recap:

  • The <sub> tag gives you the ability to place text below the line.
  • You can use subscripts for formulas in science and math.
  • You can use other tags inside <sub>.

FAQs

How to use <sub> tag in HTML?

The <sub> tag in HTML defines subscript text. It moves the text lower than the normal line. For example:
<p>H<sub>2</sub>O is water</p>

What is the difference between <sub> and <sup> in HTML?

The <sub> tag creates subscript text, while the <sup> tag creates superscript text. For example:
<p>H<sub>2</sub>O vs X<sup>2</sup></p>

Can I style <sub> tag with CSS?

Yes, you can style <sub> using CSS like any text element. For example:
<style>
sub {
  color: blue;
  font-size: 14px;
}
</style>
<p>CO<sub>2</sub></p>

Similar Reads

HTML embed Tag: How to Embed External Media in Web Pages

The HTML embed tag is used to display media content, such as videos or audio. It can also display PDFs…

HTML style Tag: Inline CSS

The style tag in HTML defines CSS inside a web page. Click to see how it works with syntax and…

HTML address Tag: Contact Information

The address tag in HTML shows contact details. The <address> tag helps show who owns the page or article. Understand…

HTML Nav Tag: How to Create Navigation Menus

The nav tag defines a navigation block in HTML. It holds main links to other parts of your site. Search…

HTML i Tag: How to Make Text Italic

The <i> tag stands for "italic." It marks text that has a different voice or tone, such as a technical…

HTML s Tag: How it Works with Examples

The HTML s tag places a line across text that has no longer value or does not hold importance. It…

HTML Heading Elements: Understanding h1 to h6 with Examples

The heading elements define a content structure in HTML. Use <h1> to <h6> to show levels. <h1> means the main…

HTML figure Tag: Responsive Design & Media Elements

The HTML figure tag links media with a caption. Use it to group an image, diagram, code block, or video…

HTML Form novalidate Attribute: How it Works with Examples

The form novalidate attribute in HTML lets you skip browser checks when you submit a form. You can control errors…

HTML link Tag: Linking Stylesheets

The link tag in HTML connects a web page to external resources like stylesheets or icons. It's placed in the…

Previous Article

PHP array_is_list: Check if an Array is a List with Examples

Next Article

Mocha JavaScript Test Framework: Guide and Examples

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.