Inline Text Elements

HTML Living Standard

Inline text elements mark a word or phrase inside a sentence. They can convey importance, stress, or code, and can also give CSS a span of text to style. These examples use the browser’s default styles unless CSS is shown.

On this page

Important text <strong>

Use strong for a warning or a key part of a message. It tells the browser which part has strong importance. Browsers usually give it bold styling. Use CSS font-weight for purely visual bold styling.

How to try it In index.html, replace the content between <body> and </body> with this example. Save the file and reload it in the browser.

<p><strong>Allergy alert:</strong> This folder contains peanuts.</p>

What to expect “Allergy alert:” usually appears bold and carries the importance of the warning.

Stress emphasis <em>

Use em on the part of a sentence you would stress when saying it aloud. Moving the emphasis can change what the sentence implies. Browsers usually display em in italics.

How to try it In index.html, replace the content between <body> and </body> with this example. Save the file and reload it in the browser.

<p>I borrowed <em>a book</em> today.</p>
<p>I borrowed a book <em>today</em>.</p>

What to expect The first sentence emphasizes what was borrowed. The second emphasizes when it was borrowed.

Keywords and Labels <b>

Use <b> to draw attention to a word or phrase, such as a keyword or a product name in a review. Browsers usually display <b> in bold. For a warning or other text with strong importance, use <strong>.

How to try it In index.html, replace the content between <body> and </body> with this example. Save the file and reload it in the browser.

<p>The <b>Trail 20</b> backpack has an adjustable strap.</p>

What to expect “Trail 20” usually appears in bold so the product name is easy to find.

Terms and Alternate Voice <i>

Use <i> to mark a phrase that reads differently from the surrounding text, such as a scientific name or a term from another language. Browsers usually display it in italics. Use <em> when a word needs to be stressed in the sentence.

How to try it In index.html, replace the content between <body> and </body> with this example. Save the file and reload it in the browser.

<p>The house cat is known as <i>Felis catus</i>.</p>

What to expect The scientific name “Felis catus” usually appears in italics.

Highlighted Text <mark>

Use <mark> to highlight a word that is relevant to what the reader is doing, such as a match for a search term. In this example, the reader searched for “library”.

How to try it In index.html, replace the content between <body> and </body> with this example. Save the file and reload it in the browser.

<p>Opening hours for the <mark>library</mark> have changed.</p>

What to expect The word “library” appears highlighted, usually with a yellow background.

Code in a sentence <code>

Use code for a short piece of computer code, such as a function call or an HTML tag written in a sentence. Browsers normally use a fixed-width font. Encode the less-than and greater-than signs as &lt; and &gt; when you want the reader to see a literal tag. Wrap code in pre for an example that should preserve line breaks and indentation.

How to try it In index.html, replace the content between <body> and </body> with this example. Save the file and reload it in the browser.

<p>Write <code>&lt;p&gt;</code> to start a paragraph.</p>

What to expect You see the literal text “<p>” inside the sentence.

Preformatted Code <pre>

Use <pre> when the line breaks and spaces are part of the example. Place <code> inside it for multiline code. The <pre> element belongs outside the paragraph.

How to try it In index.html, replace the content between <body> and </body> with this example. Save the file and reload it in the browser.

<pre><code>if (isOpen) {
  showHours();
}</code></pre>

What to expect The example keeps its three lines and the spaces before showHours().

Subscript and Superscript <sub> and <sup>

Use <sub> and <sup> when the position of a character is part of the notation, such as the 2 in the formula for water or the exponent in a mathematical expression.

How to try it In index.html, replace the content between <body> and </body> with this example. Save the file and reload it in the browser.

<p>Water: H<sub>2</sub>O</p>
<p>Area: x<sup>2</sup></p>

What to expect The 2 in H₂O is below the text baseline. The 2 in x² is above it.

Generic inline grouping <span>

A span groups a bit of phrasing content without assigning importance or other specific meaning. Use it when CSS or JavaScript needs to target a phrase. In the example, stock is a class name and .stock is the CSS selector matching it.

How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Inline text style</title>
  <style>
    .stock { color: #166534; }
  </style>
</head>
<body>
<p>Pen is <span class="stock">in stock</span> today.</p>
</body>
</html>

What to expect Only “in stock” is green, and the sentence continues in the same line where space allows.

Official references