code – code

<code>…</code>

A piece of computer code: the name of a function, a variable, an HTML element, a file, a command or a whole sample. In the text it stands on its own, a longer sample is wrapped in pre. The browser displays it in a monospaced font (font-family: monospace; when the page does not set a font size, browsers shrink it from 16 px to 13 px).

  • Default display: display: inline
  • End tag: required
  • Content: phrasing content
  • Where it can be used: anywhere phrasing content is allowed; a multi-line sample in pre
  • DOM interface: HTMLElement

Attributes

Only global attributes. The language of a sample is usually written as a class, class="language-php" – libraries such as Prism or highlight.js highlight it accordingly.

Examples

Code in the text and a multi-line sample:

<p>The function <code>mb_strlen()</code> returns the length of a text.</p>
<pre><code class="language-php">echo mb_strlen('Crème brûlée');   // 12</code></pre>
result

The function mb_strlen() returns the length of a text.

echo mb_strlen('Crème brûlée');   // 12

Things to watch out for

  • The characters < and & are written in code as the entities &lt; and &amp;, otherwise the browser takes them as tags.
  • Without pre, spaces and line breaks in the code collapse – a multi-line sample belongs in <pre><code>.
  • The smaller font appears only with plain monospace and only at the default font size. With a specific font (font-family: Consolas, monospace) or when the page sets the font size in px, the code has the same size as the surrounding text.

Browser support

Baseline – widely available: works in all major browsers and has for a long time (since 2015 at the latest). Since version: Chrome 1, Firefox 1, Safari 4. As of October 2026 (MDN browser-compat-data).

Related:
pre – preformatted text
kbd – keyboard input
samp – program output
var – variable