HTML Text Formatting: strong, em, code, mark and More
Inside a paragraph, some words deserve special treatment: a warning, a stressed word, a keyboard shortcut, a chemical formula. HTML has text-level elements for these. Pick them for what the text means; the default look (bold, italic, monospace) is just the browser's styling and can be changed with CSS.
Importance and emphasis
<strong>marks content of strong importance, seriousness or urgency, such as a warning label.<em>marks stress emphasis, the word you would say differently out loud. "The oven is very hot" changes meaning when the stress moves.<b>and<i>still exist for text that is styled differently without extra importance, such as a product name in a review (<b>) or a term from another language (<i>).
Screen readers and other tools can use these distinctions, so do not pick an element because of how it looks.
Code and technical text
<code> marks a fragment of computer code, <kbd> marks keys or input the user types, and <samp> marks output from a program. For multi-line code, wrap <code> inside <pre> so spacing is preserved.
Expected page text
Warning: the oven is very hot. Press Ctrl + C to copy. Call print() to show output. Water is H2O and 210 is 1024. Highlighted text and fine print. Price: $20 $15 <p> is written with character references & shows as text. A short quotation from a source.
Other inline elements
<sub>and<sup>give subscripts and superscripts, as in H2O and 210. Notice that the page text in the expected output reads "H2O" and "210": the meaning lives only in the markup, which is one reason to add context for maths.<mark>highlights text for reference, like a search hit.<small>is for side comments and fine print.<del>and<ins>show removed and inserted text, for example a price change.
Quotations
<blockquote> holds a longer quotation as its own block, with an optional cite attribute giving the source URL. For a short quote inside a sentence, use <q>, which adds quotation marks automatically.
Character references
Some characters have special meaning in HTML. To display < literally, write <; > is > and & is &. Others, like (a space that never breaks a line) and ©, are conveniences. With <meta charset="utf-8"> you can type most other characters, like é or €, directly.
Common mistakes
- Using
<b>and<i>everywhere when you mean importance or stress. - Typing a raw
<in text, which the browser may read as the start of a tag. - Using
<blockquote>just to indent text.
Summary
<strong>= importance,<em>= stress;<b>/<i>are purely stylistic offsets.<code>,<kbd>and<samp>mark technical text.<sub>,<sup>,<mark>,<small>,<del>and<ins>cover common inline needs.- Escape
<,>and&as<,>and&.
Quiz: Text formatting