Chapter 06 of 26

Text Formatting

On this page

In the previous chapter, Headings & Paragraphs, we learned how to split a page into headings and blocks of text. But inside a paragraph, not every word is equal. Some words are important, some are stressed, some are code, some are chemical formulas like H2O, and some are corrections.

HTML has a set of small text-level elements for exactly this job. In this chapter, we'll learn what each one means, how it looks by default, and — most importantly — how to choose the right one.

Meaning First, Looks Second

Before we look at any tags, we need one key idea that separates modern HTML from the HTML of the 1990s.

HTML describes what content means. CSS decides how it looks.

Old HTML had tags like <font> and <center> that only changed appearance. These are now obsolete. Modern formatting elements carry meaning (also called semantics). The browser gives each one a default look, but you can change that look completely with CSS.

Why does meaning matter?

  • Accessibility – assistive technologies such as screen readers work from the structure of the document, not its colours and fonts.
  • Search engines and tools – a program reading your page can tell a keyboard shortcut from a code sample or an abbreviation.
  • Maintainability – if all “warnings” use <strong>, you can restyle every one of them from a single CSS rule.

So the question to ask is never “How do I make this bold?” but “What is this text?”


Bold and Italic: strong, b, em and i

This is the most common source of confusion for beginners, because two pairs of elements look identical by default.

<strong> – Strong Importance

Use <strong> when the text is important, serious or urgent compared to the text around it.

<p><strong>Warning:</strong> Do not unplug the device while updating.</p>
<p>Submit the form before <strong>31 March</strong>. Late entries are rejected.</p>

<b> – Bring Attention

Use <b> to draw attention to words without saying they are more important — for example keywords in a summary, or product names in a review.

<p>The <b>Redmi Note</b> and the <b>Galaxy M</b> both use a 6.7-inch display.</p>

<em> – Stress Emphasis

<em> marks the word you would stress when speaking. Moving the emphasis changes the meaning of the sentence:

<p>I <em>never</em> said he copied the assignment.</p>
<p>I never said <em>he</em> copied the assignment.</p>
<p>I never said he copied the <em>assignment</em>.</p>

The first sentence denies saying it at all. The second suggests that someone else copied. The third suggests he copied something else. Same words, different meaning — that is what <em> captures.

<i> – Alternate Voice

<i> is for text that is in a different voice or mood from the surrounding text, without extra stress: technical terms, foreign words, scientific names, a character's thoughts, names of ships.

<p>The tiger's scientific name is <i>Panthera tigris</i>.</p>
<p>He said the food was <i lang="fr">très bien</i>.</p>

Adding lang="fr" tells browsers and screen readers that the phrase is French, so it can be pronounced correctly.

Quick Comparison

ElementMeaningDefault lookExample use
<strong>Strong importance, urgencyBoldWarnings, deadlines
<b>Draw attention, no extra importanceBoldKeywords, product names
<em>Stress emphasis (changes meaning)ItalicSpoken stress
<i>Alternate voice or moodItalicForeign words, scientific names

Rule of thumb: if you read the sentence aloud and would change your tone, use <strong> or <em>. If the text is only a different kind of text, use <b> or <i>. If you just want a visual style, use CSS.

One honest note: many screen readers do not change their voice for <strong> or <em> with default settings. So never rely on emphasis alone to communicate something critical — write the word “Warning:” as well, as we did above.


Highlighting and Fine Print: mark and small

<mark> – Highlighted Text

<mark> highlights text because it is relevant in the current context — like a highlighter pen on a printed page. The classic use is showing search matches.

<p>Search results for "stack":</p>
<p>A <mark>stack</mark> follows the LIFO principle. Push and pop happen at the top of the <mark>stack</mark>.</p>

Browsers show <mark> with a yellow background by default.

<small> – Side Comments

<small> is for side comments and fine print: legal notes, copyright lines, disclaimers. It is not meant for “make this text smaller” in general — that is CSS's job.

<p>Price: ₹499 <small>(inclusive of all taxes)</small></p>
<p><small>© 2026 CS Learning Portal. All rights reserved.</small></p>

Edits and Outdated Text: del, ins and s

These three are easy to mix up because two of them look the same.

<del> and <ins> – Document Edits

<del> marks text that was removed from a document, and <ins> marks text that was added. Together they record a change history. Both accept two optional attributes:

  • datetime – when the change was made (a machine-readable date like 2026-10-15).
  • cite – a URL that explains the change.
<p>Exam date: <del datetime="2026-10-15">20 November</del> <ins datetime="2026-10-15">27 November</ins></p>

By default, deleted text has a line through it and inserted text is underlined.

<s> – No Longer Accurate

<s> marks text that is no longer correct or relevant, but which was never “edited out” of a document. The typical example is an old price:

<p>Course fee: <s>₹999</s> ₹0 — now completely free!</p>
QuestionUse
Was this text removed as part of an edit to the document?<del>
Is this text still shown, but just no longer true?<s>
Was this text added as part of an edit?<ins>

What About <u>?

<u> still exists, but its meaning is narrow: an unarticulated annotation, such as marking a misspelled word the way a spell-checker does. Avoid it for decoration — on the web, underlined text looks like a link and confuses users.

<p>You wrote: "I <u>recieved</u> the parcel."</p>

Subscript and Superscript: sub and sup

<sub> lowers text below the line and <sup> raises it above the line. They are used when the position is part of the meaning — chemistry, maths, footnotes, ordinal numbers.

<p>Water is H<sub>2</sub>O and carbon dioxide is CO<sub>2</sub>.</p>
<p>Area of a circle = πr<sup>2</sup></p>
<p>x<sub>1</sub> + x<sub>2</sub> = 10</p>
<p>She finished 1<sup>st</sup> in the GATE mock test.</p>

Rendered, these read as: H2O, CO2, πr2, x1 + x2 = 10, 1st.

For serious mathematics (fractions, matrices, integrals), HTML has a separate language called MathML, which is supported in all modern browsers. <sub> and <sup> are fine for simple formulas.


Computer Text: code, kbd, samp and var

As CS students, these four are the ones you'll use the most. All of them except <var> use a monospace (fixed-width) font by default.

ElementMeansDefault look
<code>A fragment of computer codeMonospace
<kbd>Keyboard (or other) input the user types or pressesMonospace
<samp>Sample output from a programMonospace
<var>A variable in maths or programmingItalic
<p>Use the <code>printf()</code> function to print output in C.</p>
<p>Press <kbd>Ctrl</kbd> + <kbd>S</kbd> to save the file.</p>
<p>If you forget the semicolon, the compiler prints <samp>error: expected ';' before '}' token</samp>.</p>
<p>The area of a rectangle is <var>l</var> × <var>b</var>.</p>

For a multi-line block of code, put <code> inside <pre>. The <pre> element keeps every space and line break exactly as written:

<pre><code>for (int i = 0; i &lt; 3; i++) {
    printf("%d\n", i);
}</code></pre>

Notice the &lt; in i &lt; 3. Inside HTML, a raw < starts a tag, so a less-than sign in code must be written as the character reference &lt;. We'll cover these properly in Character Entities & Symbols.


Quotations, Abbreviations and Definitions

<q> – Short Inline Quotes

<q> wraps a short quotation inside a sentence. The browser adds the quotation marks for you, so don't type them yourself.

<p>Kalam said, <q>Dream is not that which you see while sleeping; it is something that does not let you sleep.</q></p>

<blockquote> – Long Quotes

<blockquote> is for a longer quotation that forms its own block. Browsers indent it by default. The optional cite attribute holds the URL of the source (it is not shown on screen).

<blockquote cite="https://www.w3.org/People/Berners-Lee/">
  <p>This is for everyone.</p>
</blockquote>
<p>— Tim Berners-Lee, London 2012 Olympics opening ceremony</p>

<cite> – Title of a Work

<cite> marks the title of a creative work: a book, film, song, paper or website. According to the HTML standard, a person's name is not a title, so don't wrap author names in <cite>.

<p>My favourite algorithms book is <cite>Introduction to Algorithms</cite>.</p>

<abbr> – Abbreviations

<abbr> marks an abbreviation or acronym. Use the title attribute for the full form; most desktop browsers show it as a tooltip on hover.

<p><abbr title="Graduate Aptitude Test in Engineering">GATE</abbr> is held every February.</p>

Tooltips do not appear on touch screens, so a good habit is to also write the full form in the text the first time it is used.

<dfn> – The Defining Instance

<dfn> marks the term being defined in the sentence that defines it.

<p>A <dfn>compiler</dfn> translates an entire program into machine code before it runs.</p>

Worked Example: Putting It All Together

Let's build a small “exam notice” that uses the right element for each kind of text. Read each line and ask what the text is before looking at the tag.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Exam Notice</title>
</head>
<body>
  <h1>Data Structures Lab Exam</h1>
  <p><strong>Important:</strong> Bring your college ID card.</p>
  <p>Date: <del>20 November</del> <ins>27 November</ins></p>
  <p>Lab fee: <s>₹200</s> waived this semester.</p>
  <p>Topics: <mark>stacks</mark>, <mark>queues</mark> and linked lists.</p>
  <p>Write the program in your <abbr title="Integrated Development Environment">IDE</abbr> and compile it with <code>g++ main.cpp</code>.</p>
  <p>Press <kbd>Ctrl</kbd> + <kbd>Enter</kbd> to submit. You should see <samp>Submission received</samp>.</p>
  <p>Time complexity of binary search: O(log <var>n</var>). Memory used: 2<sup>10</sup> bytes.</p>
  <p>Reference: <cite>The C++ Programming Language</cite></p>
  <p>The examiner said, <q>No mobile phones inside the lab.</q></p>
  <p>The word <i lang="la">viva voce</i> means an oral exam. You <em>must</em> attend it.</p>
  <p><small>Issued by the Exam Cell. Subject to change.</small></p>
</body>
</html>

When you open this file in Chrome, you see:

  • “Important:” in bold.
  • “20 November” and “₹200” struck through, with “27 November” underlined.
  • “stacks” and “queues” with a yellow highlight.
  • “IDE” with a dotted underline (because the <abbr> has a title) — hovering shows “Integrated Development Environment”.
  • g++ main.cpp, “Ctrl”, “Enter” and “Submission received” in a monospace font.
  • n, the book title, “viva voce” and “must” in italics.
  • The 10 raised as a small superscript, and the examiner's words wrapped in curly quotation marks “…” added by the browser.
  • The last line in a slightly smaller font.

Notice how several pairs look identical — <del>/<s> and <em>/<i>/<cite> — but each tells the reader (and any program) something different.


Nesting Formatting Elements

Formatting elements can be combined, but they must be properly nested: the element opened last must be closed first.

<!-- Correct -->
<p>This is <strong><em>very</em> important</strong>.</p>

<!-- Wrong: tags overlap -->
<p>This is <strong><em>very important</strong></em>.</p>

Think of it like brackets in maths: ( [ ] ) is valid, ( [ ) ] is not. Browsers try to repair overlapping tags, but the repair may not be what you intended, and validators report it as an error.


Obsolete Formatting Tags

You will still find these in old tutorials and old college lab manuals. They are obsolete in the current HTML Living Standard — don't use them in new pages.

Obsolete tagWhat it didUse instead
<font>Changed font, size, colourCSS font-family, font-size, color
<center>Centred contentCSS text-align: center
<big>Bigger textCSS font-size
<strike>Strikethrough<s> or <del>
<tt>Teletype (monospace) text<code>, <kbd>, <samp> or CSS
<acronym>Acronyms<abbr>
<blink>, <marquee>Blinking / scrolling textNothing (or CSS animation, sparingly)

Note that <b>, <i>, <u>, <s> and <small> were not removed — they were given new, meaningful definitions in HTML5, which is what we learned above.


Common Mistakes

  • Using <strong> or <b> for headings. A bold line is not a heading. Use <h2>, <h3> and so on, as we learned in Headings & Paragraphs.
  • Typing quotation marks inside <q>. You get double quotes, like ““this””.
  • Using <u> for emphasis. Users will try to click it.
  • Using <small> or <big> for sizing. Font size is a CSS concern.
  • Using <br> or <sup> to position things. If the position doesn't change the meaning, use CSS.
  • Writing a raw < inside <code>. The browser thinks a tag is starting. Write &lt;.
  • Overlapping tags like <b><i>text</b></i>.

Interview and Exam Questions

1. What is the difference between <strong> and <b>?

Both are bold by default. <strong> means the content has strong importance, seriousness or urgency. <b> only draws attention to text (like keywords or product names) without implying extra importance. Choose based on meaning; use CSS font-weight if you only want visual boldness.

2. What is the difference between <em> and <i>?

<em> marks stress emphasis — the word you would stress when speaking, which can change the sentence's meaning. <i> marks text in an alternate voice such as a foreign phrase, a technical term or a scientific name. Both are italic by default.

3. When would you use <del> instead of <s>?

Use <del> when text has been removed as an edit to the document (optionally with datetime and cite). Use <s> when text is still displayed but is no longer accurate or relevant, such as an old price.

4. Write HTML for the expression a² + b² = c² and the formula of sulphuric acid.
<p>a<sup>2</sup> + b<sup>2</sup> = c<sup>2</sup></p>
<p>H<sub>2</sub>SO<sub>4</sub></p>
5. Which element would you use to show a keyboard shortcut, and which for a program's output?

<kbd> for user input such as <kbd>Ctrl</kbd> + <kbd>C</kbd>, and <samp> for sample program output.

6. Is <font> valid in HTML today? What replaces it?

No. <font> is obsolete in the HTML Living Standard. Its job (font family, size and colour) is done with CSS properties font-family, font-size and color.

7. Does <q> need quotation marks inside it?

No. The browser inserts the correct quotation marks automatically (based on the language of the page). Adding your own produces doubled quotes.


Summary

  • Formatting elements describe meaning; CSS controls appearance.
  • <strong> = importance, <b> = attention; <em> = stress, <i> = alternate voice.
  • <mark> highlights relevant text; <small> is for fine print.
  • <del>/<ins> record edits; <s> marks text that is no longer accurate; <u> is for annotations like spelling errors.
  • <sub> and <sup> are for formulas, footnotes and ordinals.
  • <code>, <kbd>, <samp> and <var> mark code, input, output and variables.
  • <q>, <blockquote>, <cite>, <abbr> and <dfn> handle quotes, titles, abbreviations and definitions.
  • <font>, <center>, <big>, <strike> and <tt> are obsolete — use CSS or the modern elements.

In the next chapter, Links, we'll learn the element that turns a set of pages into a web: the anchor, <a>.