Skip to content

Markdown Code Blocks

TL;DR

What it is

A code block is a multi-line region of literal text. Markdown keeps the contents together and prevents normal inline parsing inside the block.

Why it matters

Code blocks are essential in technical writing, but they also show up in non-code documentation for command lines, configuration snippets, logs, and examples. A solid mental model here pays off across the rest of Markdown.

Syntax

```
code here
```

    indented code here

Minimal example

Source

```bash
npm install
npm run dev
```

What it does

This renders a fenced code block containing two shell commands.

More examples

Example 1: Fenced code block

```
function greet(name) {
  console.log(`Hello, ${name}`);
}
```

What it does

Triple backticks open and close the block. Everything inside is treated literally.

Example 2: Indented code block

    line one
    line two

What it does

Four leading spaces create an indented code block.

Example 3: Showing triple backticks with quadruple backticks

````
```
visible backticks
```
````

What it does

The outer fence is longer than the inner one, so the inner triple backticks appear as literal content.

Example 4: Code inside a list item

1. Install dependencies

       npm install

2. Start the app

What it does

Inside a list item, non-fenced code usually needs more indentation than it would outside the list.

Common pitfalls

Portability and platform notes

FAQ

Should I use fenced or indented code blocks?

For most modern documentation, fenced code blocks are easier to read, easier to nest, and easier to extend with language identifiers. Indented blocks are still worth learning because they are part of the core syntax.

Why did my code block not start?

If you used indentation, check whether the code was attached to a paragraph or list in a way that prevented an indented block from forming. If you used fences, check the fence length and closing line.

How do I show triple backticks inside a code block?

Wrap the whole example in a longer fence, such as four backticks.

Does Markdown parse links or emphasis inside a code block?

No. Code block contents are treated as literal text.

Practice

Related topics

References