Explain it simply
Explain it simply
In a newspaper article, someone else's words go inside quotation marks, and a longer quotation is set apart as its own indented block. In HTML a short quote inside a sentence is <q>, and a quotation that stands as its own block is <blockquote>.
Programming tutorials also need code to look different from normal text. A code word inside a sentence is <code>, a multi-line code block is <code> inside <pre>, and a key the reader should press is <kbd>.
Problem → Why → Solution
The problem: the tag you wanted to show becomes a real tag
Someone writing an HTML tutorial typed the sentence "Use <h1> for the title" straight into a paragraph, then pasted an indented code example into another paragraph. In the browser, the text "<h1>" vanished, "for the title" became a huge heading, and every bit of the example's indentation was gone.
<p>Use <h1> for the title of the page.</p>
<p>
<ul>
<li>Item</li>
</ul>
</p>Why did that happen?
The browser treats every < as the start of a tag. So the <h1> in the sentence became a real heading element, and the <ul> in the second paragraph became a real list. The indentation disappeared because of whitespace collapsing. To show code as text, you escape < with a character reference, and to keep its spacing you use <pre>.
Core concept: quoting and showing code
<blockquote> is a block quotation from another source. It can contain paragraphs, and browsers indent it with margins on both sides. Its cite attribute can hold the source's URL, but browsers do not display it. So the visible credit (who said it, which book) goes outside the blockquote, as the spec requires, usually by wrapping the blockquote and a <figcaption> in a <figure>.
<q> is a short quote that sits inside a sentence. The browser adds the quotation marks itself through CSS, so you do not type your own; if you do, you get doubled marks. The separate <cite> element is for the title of a work, such as a book, a film or an article, and is italic by default.
<code> marks a fragment of computer code and renders in a monospace font, but it still collapses whitespace like normal text. <pre> (preformatted) keeps every space and line break exactly as in the source. The standard code block is <pre><code>...</code></pre>: <pre> preserves the spacing and <code> says "this is code". <kbd> is for input the user types, such as the Ctrl or Enter keys. Long lines inside <pre> do not wrap by default, so wide code scrolls sideways unless your CSS allows wrapping.
Even inside <code> and <pre>, the browser still parses tags. To show HTML as text, write < as the character reference made of an ampersand, lt and a semicolon, and write & as ampersand, amp, semicolon. Escaping > is not always required, but writing it as ampersand, gt, semicolon keeps the code consistent. The example shows the exact spelling.
- Long quotation: <blockquote>
- Quote inside a sentence: <q>
- Code word: <code>; block: pre+code
- Escape < and & inside code
Quote tags say whose words these are; code tags say what kind of text this is.
General pattern
<figure>
<blockquote cite="source-url"><p>Long quotation</p></blockquote>
<figcaption>Author, <cite>Title of the work</cite></figcaption>
</figure>
<p>Sentence with <q>a short quote</q> and <code>codeWord</code>.</p>
<p>Press <kbd>Key</kbd> to continue.</p>
<pre><code>escaped code, spacing kept</code></pre>figure groups the quotation with its credit, and figcaption sits outside the blockquote. <q>, <code> and <kbd> are inline and live inside sentences, while <pre><code> is a block of its own.
Example: a quote, inline code, keys and a code block
This page uses all of them: a blockquote with a figcaption credit, <q>, inline code, <kbd> and a code block. Notice how every < inside the code is written as a character reference. Then open the page and compare: the browser turns each reference back into a visible < character.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Quotes and code</title>
</head>
<body>
<figure>
<blockquote>
<p>Learning HTML is the first step to building the web.</p>
</blockquote>
<figcaption>From the course introduction</figcaption>
</figure>
<p>My teacher always said <q>close every tag you open</q>.</p>
<p>Use <code><h1></code> for the main heading.</p>
<p>Press <kbd>Ctrl</kbd> + <kbd>S</kbd> to save your file.</p>
<pre><code><ul>
<li>Code block example</li>
</ul></code></pre>
</body>
</html>Code breakdown
- Lines 8-13
<figure> <blockquote> <p>Learning HTML is the first step to building the web.</p> </blockquote> <figcaption>From the course introduction</figcaption> </figure>The quotation is a paragraph inside the blockquote, and its source is credited in a figcaption outside it. The figure ties the two together.
- Line 14
<q>close every tag you open</q>No quotation marks are typed; the browser adds them, matching the document's language.
- Line 15
<code><h1></code>Thanks to the character references, the browser shows the text <h1> instead of starting a heading. Unescaped, it would become a real heading element.
- Line 16
<kbd>Ctrl</kbd> + <kbd>S</kbd>Each key gets its own <kbd>, so CSS can later style every key as a separate keycap.
- Lines 17-19
<pre><code><ul> <li>Code block example</li> </ul></code></pre><pre> keeps the line breaks and the two spaces on the middle line. The content inside <pre> starts at the left margin of the source; otherwise the file's own indentation would show up in the output.
The quotation is indented on both sides with "From the course introduction" below it. The teacher's words appear inside “ ” marks added by the browser, and <h1>, Ctrl and S show in a monospace font. Last comes a monospace block of three lines, <ul>, <li>Code block example</li> and </ul>, with the middle line still indented.Characters to escape, and which element when
The code segment below shows how each character is written in HTML source. The left side is what you type; the comment shows the character that appears on the page.
<p><code><</code></p> <!-- shows < (always escape inside code) -->
<p><code>&</code></p> <!-- shows & (escape when shown as text) -->
<p><code>></code></p> <!-- shows > (optional, but consistent) -->
<p><code>"</code></p> <!-- shows " (inside a double-quoted attribute) -->| Element | Use it for | Default look |
|---|---|---|
| <blockquote> | A long quotation from a source | Indented both sides |
| <q> | A short quote inside a sentence | Quote marks added |
| <cite> | The title of a book, film or other work | Italic |
| <code> | A fragment of code | Monospace |
| <pre> | Text whose spacing matters | Monospace, spacing kept |
| <kbd> | Keys or input the user types | Monospace |
In real projects
Where this shows up in real projects
Documentation sites and blogs output their code blocks as <pre><code>. Syntax highlighting libraries read the text inside <code> and wrap each keyword in coloured <span> elements, often choosing the language from a class such as class="language-html". A Markdown code block written with three backticks also becomes <pre><code> behind the scenes, with every < escaped for you.
News and review sites present quotes from customers or public figures as a blockquote with a figcaption credit inside a figure, and testimonial cards on landing pages use the same structure. Software help pages write keyboard shortcuts with <kbd> and style them as keycaps in CSS. Any server code that shows user comments on a page must escape < and & too; otherwise a <script> tag inside a comment could actually run, which is the cross-site scripting (XSS) problem.
Common mistakes
Common mistakes
1. Not escaping < inside code
Writing <code><p></code> makes the browser create a real paragraph element, and the text <p> disappears. Always write < as its character reference.
2. Typing your own quotation marks inside <q>
<q>"Hello"</q> shows doubled marks, the browser's plus yours. Let the browser add them.
3. Using <blockquote> just to indent
Putting non-quoted text in a blockquote to indent it can make screen readers present it as a quotation. Indent with CSS margin-left or padding instead.
4. Putting the author inside the blockquote
Everything inside a blockquote counts as quoted words, and the author's name was not part of what they said. Move it outside using figure and figcaption.
Best practices
Best practices
- Add a language class such as class="language-html" to the <code> in every code block so highlighting tools can pick it up.
- Copy quotations word for word, and say who said them in a visible figcaption.
- When you find yourself escaping lots of code by hand, let Markdown or a template engine escape it automatically.
- Copy the code out of your own <pre> block and run it in an editor; escaping mistakes show up immediately.
Mini challenge
Mini challenge: a tips page
Write a small page about one HTML tip. It needs a credited quotation, a short inline quote, a keyboard shortcut, inline code and an escaped HTML code block.
- A blockquote and a figcaption (with <cite>) inside a figure
- One <q> in a sentence, with no hand-typed quotation marks
- One shortcut in <kbd> and one tag name in <code>
- Three escaped, indented HTML lines inside <pre><code>
<!-- Place inside the <body> of a full page -->
<h1>Tip: always add alt text</h1>
<!-- TODO: figure > blockquote (a paragraph) + figcaption with a <cite> title -->
<!-- TODO: a sentence containing a short <q> quote -->
<!-- TODO: a sentence with a <kbd> shortcut and a tag name in <code> -->
<!-- TODO: a <pre><code> block with three lines of escaped HTML -->Expected: An indented quotation with a credit showing an italic book title, a short quote wrapped in browser-added marks, a monospace key and code word, and finally an indented code block whose tags appear as visible text.
Show hint
Write the code block as normal HTML first, then replace every < with its character reference. Start the first line of code on the same line as <pre><code>.
Show solution
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Tip: always add alt text</title>
</head>
<body>
<h1>Tip: always add alt text</h1>
<figure>
<blockquote>
<p>An image without alt text is a locked door for screen reader users.</p>
</blockquote>
<figcaption>Workshop notes, <cite>Accessible Web Basics</cite></figcaption>
</figure>
<p>Our reviewer's rule is simple: <q>no alt, no merge</q>.</p>
<p>Add the <code>alt</code> attribute, then press <kbd>Ctrl</kbd> + <kbd>S</kbd>.</p>
<pre><code><figure>
<img src="cat.jpg" alt="A cat asleep on a laptop">
</figure></code></pre>
</body>
</html>Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- A long quotation is <blockquote>; a short quote inside a sentence is <q>.
- Credit the source outside the blockquote with figure and figcaption.
- Inline code is <code>, a code block is <pre><code>, and a key is <kbd>.
- To show HTML as text, escape < and & with character references.