A fenced code block displays verbatim, monospaced content — typically source code — without any Markdown processing inside it. It is delimited by a line of three or more backticks or tildes, with an optional language tag on the opening fence for syntax highlighting.
```python
def greet(name):
return f"Hello, {name}!"
```
def greet(name):
return f"Hello, {name}!"
Best Practice
- Always include a language tag on the opening fence. It enables syntax highlighting in supporting renderers and communicates intent clearly to the reader.
- Use backtick fences (
```) as the standard form; tilde fences (~~~) are less commonly expected. - To display a code block that itself contains triple backticks, use a fence of four or more backticks or use
~~~:
~~~
```text
# This block contains triple backticks
```
~~~
Notes
Tilde Fences
A fence of three or more ~ characters works identically to backtick fences and can be used interchangeably. Tilde fences are useful when the code block itself contains backticks (also see the example on Best Practice above):
~~~markdown
Use `backticks` for inline code.
~~~
Info String
The text following the opening fence markers is called the 'info string'. Most renderers use the first word as the language identifier for syntax highlighting:
```javascript
console.log("Hello");
```
Some processors use additional words in the info string for extended features (line numbers, file names), but this is not standardised.
Fences Inside List Items
When a fenced code block appears inside a list item, it must be indented to align with the list item content and preceded by a blank line:
1. Step one.
copy the text below and paste it into the typeface viewer
~~~text
The quick brown fox jumped over the lazy dog
~~~
2. Step two.
Try out different typefaces
History
Fenced code blocks were not in the original Markdown specification. They were introduced by extensions — notably PHP Markdown Extra — and later standardised by CommonMark. The indented code block (four-space indent) is the original syntax.
Markdown Flavour Support
Not in the original spec; supported by all modern flavours.
Flavour Support Detail
| Flavour | Support | Notes |
|---|---|---|
| Original Markdown | No | Only indented code blocks were defined in the original spec. |
| CommonMark | Yes | Standardised fenced code blocks with full rules for info strings and nesting. |
| GitHub Flavored Markdown | Yes | Inherits CommonMark; language tag used for syntax highlighting on GitHub. |
| Pandoc Markdown | Yes | Full support; language tag used for syntax highlighting and code formatting in output. |
Practical Examples
- Craft pattern in a hobby blog: A knitter sharing a stitch chart uses a
textfenced block to keep the symbols and columns exactly as written — without it, the spaces collapse and the grid becomes unreadable.
Stitch key for the cable repeat (12 rows):
```text
Row 1: K2 C4F K2
Row 2: P all
Row 3: K all
Row 4: P all
```
Stitch key for the cable repeat (12 rows):
Row 1: K2 C4F K2
Row 2: P all
Row 3: K all
Row 4: P all
- Aligned schedule in a personal wiki: Someone planning a weekend gathering can keep a rough timetable exactly as typed, with columns aligned, without Markdown collapsing the spacing.
Saturday's rough schedule:
```text
09:00 Breakfast and welcome
10:30 Walk to the viewpoint
12:30 Lunch at the pub
15:00 Free time
18:00 Dinner
```
Saturday's rough schedule:
09:00 Breakfast and welcome
10:30 Walk to the viewpoint
12:30 Lunch at the pub
15:00 Free time
18:00 Dinner
- Ingredient ratios in a cooking notes page: A home cook can drop in a quick reference table using a fenced block to keep the numbers aligned and easy to scan at a glance.
Basic bread ratios to remember:
```text
Flour : 100%
Water : 65%
Salt : 2%
Yeast : 1%
```
Basic bread ratios to remember:
Flour : 100%
Water : 65%
Salt : 2%
Yeast : 1%