# `<code>`

> The `<code>` HTML element represents a fragment of computer code.

## How do I mark up inline code in HTML?

The element that is used to display a fragment of computer code or other text that should be presented in a monospaced font. It's often used within technical documentation, tutorials or any context where code needs to be highlighted or shown differently from regular text.

- Topic: Formatting
- Type: tag
- Browser support: Chrome 1+, Chrome Android 18+, Edge 12+, Firefox 1+, Firefox Android 4+, Safari 4+, Safari iOS 3.2+

## Example

Wrong:

```html
<p>
  Use the <code><div></code> element.
</p>
```

Right:

```html
<p>
  Use the <code>&lt;div&gt;</code> element.
</p>
```

An unescaped `<div>` is parsed as a real tag, so the text disappears; `&lt;` shows it as text.

## Common mistakes

- Writing a raw tag like `<div>` inside `<code>`, so it is parsed and disappears.

## Key points

- Inline code uses `<code>`, and block code uses `<code>` inside `<pre>`.
- Special characters like `<` and `&` inside `<code>` are escaped.
- Keyboard input uses `<kbd>` and program output uses `<samp>`, not `<code>`.

## Interview questions

### Does code keep spaces and line breaks?

No, `<code>` collapses whitespace like normal text. Wrap it in `<pre>` to keep spacing.

### How do I set the language for syntax highlighting?

Add a class such as 'language-js' to `<code>`. Highlighting libraries read it; the browser itself does not highlight code.

## Related cards

- [`<pre>`](https://wonderium.dev/html/pre.md)
- [`<kbd>`](https://wonderium.dev/html/kbd.md)
- [`<samp>`](https://wonderium.dev/html/samp.md)
- [code](https://wonderium.dev/a11y/code.md)

---

- Level: [HTML](https://wonderium.dev/html.md)
- HTML version: https://wonderium.dev/html/code
- Last updated: 2026-11-25
