← Back to blog/Guides

Markdown Code Blocks: Fences, Languages, and Nested Examples

Write copyable code samples with correct fences, language labels, whitespace, and nesting, including examples that contain Markdown themselves.

F
Faizan Khan
• 6 min read

Create a Markdown code block by putting at least three backticks on a line before the code and a matching closing fence after it. Add a language name such as json after the opening fence for renderers that support syntax highlighting. Inside the fence, Markdown characters remain sample text rather than becoming headings, links, or emphasis.

Markdown
1```json
2{ "mode": "preview", "retries": 2 }
3```

The structural HTML is:

HTML
1<pre><code class="language-json">{ "mode": "preview", "retries": 2 }
2</code></pre>

The class gives a highlighter a language hint. It does not execute the JSON or prove that a particular application accepts the configuration.

Fenced code versus inline code

Use inline code for a short literal embedded in a sentence: a filename, a flag, a function name, or a syntax token. Use a block when line boundaries or indentation matter.

Markdown
1Set `mode` to `preview` before reviewing the output.

Inline code is not a miniature preformatted block. Its whitespace handling differs, and it should not carry a multiline command whose newlines matter.

To include a literal backtick in an inline code span, surround the content with a longer run of backticks:

Markdown
1Use `` `mode` `` to show a code-formatted field name in Markdown source.

The spaces next to the outer delimiters are trimmed according to code-span rules in this case, leaving the literal inner backticks visible. Test complex spans instead of adding backslash escapes inside them; backslashes in code are normally literal characters.

Choose a useful language label

SampleCommon labelWhy it helps
A JSON objectjsonMakes strings, punctuation, and values distinguishable
A shell commandbash or the actual shellSignals shell-specific quoting and syntax
An HTTP exchangehttpSeparates headers and message content
Markdown sourcemarkdownMakes the document syntax recognizable
Output or a neutral transcripttextAvoids misleading code highlighting

The GitHub code-block documentation explains language identifiers for its rendering system. Supported identifiers vary by highlighter.

Do not label a terminal transcript as an executable command when it includes prompts and output. A reader who copies $ export ... can accidentally copy the dollar-sign prompt too. Separate the command from its expected output and state any prerequisites immediately before the block.

Show triple backticks inside a code block

Use a longer outer fence than any matching run inside the sample:

Markdown
1````markdown
2```json
3{ "mode": "preview" }
4```
5````

The outer four-backtick fence in the demonstrated source contains the inner three-backtick fence as text. If the sample needs four consecutive backticks, increase the outer fence again.

Tilde fences provide another option:

Markdown
1~~~text
2This sample can contain ``` without closing the tilde fence.
3~~~

A closing fence must use the same character as the opener and at least as many characters. It cannot close a backtick block with tildes. The CommonMark fenced-code rules describe these boundaries.

Preserve code inside a numbered step

Markdown
11. Create the review configuration.
2
3 ```json
4 {
5 "mode": "preview"
6 }
7 ```
8
9 Confirm the value before continuing.
102. Open the preview.

The three spaces before the fence belong to the 1. list item. The JSON indentation inside the fence belongs to the sample itself. Keeping those roles separate prevents examples from falling out of a list or acquiring unwanted leading spaces.

For nested bullets or wider ordered markers, calculate the parent content column rather than reusing three spaces everywhere. The list guide works through those cases.

Indented code is valid, but context matters

At the top level, four leading spaces can create an indented code block:

Markdown
1Expected output:
2
3 Review complete
4 Missing links: 0

That is useful for simple plain-text output. It has no fenced language label, and its indentation becomes harder to reason about inside lists. Fences usually communicate the boundary more clearly.

Do not use code indentation just to offset prose. It changes semantics, font treatment, wrapping, and how Markdown inside the block is interpreted. See Markdown indentation when the goal is paragraph layout instead.

Code blocks escape markup, not responsibility

HTML
1<button type="button">Preview</button>

In a normal code block, that HTML is displayed as text rather than creating a working button. Similarly, **bold** remains literal sample content. This is why tutorials can safely display markup without asking the page to interpret it.

However, some platforms attach special behavior to labels such as diagram languages. A code-looking block may be transformed by a plugin. Do not claim that every fenced block is inert in every publishing system.

Also review the sample itself: fences do not make leaked tokens harmless or make destructive commands safe to copy. Use example values that cannot be confused with real credentials, describe side effects, and avoid combining setup, mutation, and cleanup into an unexplained wall of commands.

A code-sample acceptance check

Before publishing, verify that the opening and closing fences match, the language label describes the content, and the copied text contains no prompt characters or smart punctuation. Run executable examples in the appropriate safe environment when possible; for output samples, confirm that they are labeled as output.

Then inspect a narrow viewport. Long lines should not push the entire article beyond the screen. A theme may provide scrolling, wrapping, or a copy control, but those are renderer features rather than fence syntax. The Markdown cheat sheet includes a compact reference for the surrounding document.

FAQ

How do I show Markdown instead of rendering it?

Put the Markdown source inside a fenced code block. If the source contains triple backticks, use a longer outer fence or a tilde fence.

Does a language label run the code?

Ordinary language labels are hints for syntax highlighting, not execution instructions. Some platforms add special transformations, so check the actual renderer for exceptions.

Why is the rest of my article displayed as code?

The closing fence may be missing, use the wrong character, be too short, or sit at the wrong container indentation. Inspect the fence boundaries before changing the article text.

Continue This Guide