Skip to content
Tutorials Center
My learning

Topics

HTML Reference ยท Inline Text

The <code> element

Reference for the HTML <code> element: marking inline code and code blocks with <pre>, escaping special characters, language classes and technical details.

The <code> element marks a fragment of computer code: a function or variable name, an HTML tag, a CSS property, a file name, a command, or anything else a computer would read. Browsers display it in a monospace font.

On its own, <code> is inline and collapses white space like normal text. For a multi-line code block, put it inside a <pre> element, which keeps the line breaks and indentation.

HTML
<p>Call <code>save()</code> before closing the file.</p>

Try it

Inline code and a code block

Attributes

This element has no attributes of its own. It accepts the global attributes.

Usage notes

  • Escape special characters. Inside <code>, the browser still parses HTML. Write < as &lt; and & as &amp; when you want to show markup, or it will be treated as a real tag. See character entities.
  • Name the language with a class. There is no lang-style attribute for programming languages. The common convention is a class such as class="language-python", which syntax highlighters also recognize.
  • Related elements. Use <kbd> for keys and input the user types, <samp> for output a program prints, and <var> for variables in maths or prose.
  • Long lines. Inside <pre>, long lines don't wrap by default. Give the <pre> overflow-x: auto so it scrolls instead of breaking the layout on small screens.
  • Accessibility. Most screen readers don't announce code, so keep the surrounding sentence clear about what the code is.

Technical summary

Content categories Flow content, phrasing content, palpable content
Permitted content Phrasing content
Tag omission None; both the start and end tags are required
Permitted parents Any element that accepts phrasing content
Implicit ARIA role code
DOM interface HTMLElement
Default display inline (monospace font)

Browser support

Supported in every browser.