Codectionary / Developer documentation / HTML

<code>, <pre>, <kbd>

These elements are especially relevant for a developer-focused site like this one. <code> marks up a short inline fragment of computer code, typically rendered in a monospace font. <pre> preserves whitespace and line breaks exactly as written, making it ideal for displaying blocks of code or formatted text. <kbd> represents user keyboard input, commonly used in tutorials to show keyboard shortcuts. <pre> and <code> are frequently combined to display multi-line, syntax-preserved code blocks.

Syntax

<code>const x = 5;</code>
<pre><code>function add(a, b) {
  return a + b;
}</code></pre>

Examples

Inline Code

Referencing a variable or short code snippet within a sentence.

<p>Use the <code>Array.map()</code> method to transform each item in an array.</p>

Code Block

Displaying a multi-line code block with preserved formatting.

<pre><code>function greet(name) {
  console.log(`Hello, ${name}!`);
}

greet("Codectionary");</code></pre>

Keyboard Shortcut

Documenting a keyboard shortcut in a tutorial.

<p>Press <kbd>Ctrl</kbd> + <kbd>S</kbd> to save your file.</p>

Best practices

  • Use <code> for inline references to code, variable names, or file names within regular text
  • Combine <pre> and <code> together for multi-line code blocks so both whitespace and code semantics are preserved
  • Use <kbd> specifically for keyboard input instructions, not for styling arbitrary text to look like a key
  • Add a language-specific class (like class="language-javascript") to <code> blocks if using a syntax highlighter

At a glance

Purpose
Structure and meaning for web content
File extension
.html · .htm
Runs in
Web browsers
Usually used with
CSS and JavaScript

Specifications & further reading

Related HTML documentation

<span>
The <span> element is an inline container used to group and style specific portions of text or inline elements. Unlike <div>, which is block-level, <span> doesn't break the flow of content. It's perfect for applying styles or manipulating specific words or phrases within a paragraph without affecting the surrounding content.
<h1> - <h6>
The heading elements <h1> through <h6> represent six levels of section headings, with <h1> being the most important and <h6> the least. Headings create a document outline that both sighted users and assistive technologies rely on to understand and navigate page structure. Search engines also use heading hierarchy to interpret content relevance. Each page should typically have exactly one <h1> describing its main topic, with subsequent headings nested logically to reflect the content hierarchy.
<p>
The <p> element represents a paragraph of text — one of the most fundamental building blocks of written content on the web. Browsers automatically add margin above and below paragraphs to visually separate them. The <p> element cannot contain other block-level elements like <div> or headings, only inline content and text. It is a block-level element, meaning it always starts on a new line.
Text Formatting: <strong>, <em>, <b>, <i>, <mark>, <small>
HTML provides several inline elements for formatting text with semantic and visual meaning. <strong> indicates strong importance (typically rendered bold), and <em> indicates stress emphasis (typically rendered italic) — both carry semantic weight that screen readers can announce. <b> and <i> are their purely visual counterparts, used when bold/italic styling is needed without implying importance or emphasis. <mark> highlights text as relevant to the current context, and <small> represents side comments like fine print or disclaimers.