Markdown requires code blocks within lists to be indented properly. See the examples below for detailed instructions.
```
) for start and end markers can cause a fenced code block to render incorrectly.Below you will find examples of how you can add code blocks to Markdown lists. The examples are divided into defined, fenced code blocks, e.g. JavaScript, Ruby, Python, Shell, etc. and undefined code blocks, which output plain text.
The ↵
characters in markdown examples denote line breaks.
A defined fenced (e.g., python
, javascript
, bash
, html
, css
, etc.) code block should be indented 0 spaces from the list item’s marker (e.g., -
, *
or 1.
) along with triple backticks for defining the language for syntax highlighting.
The space (
) is part of the item’s marker. Count from the space onwards.
The markdown below, renders a definded code block, with python
, bash
, and html
code, within an unordered list.
- Item 1↵
↵
```python↵
print("Python code block 1")↵
```↵
↵
- Item 1.1↵
↵
```bash↵
echo "Bash code block 1.1"↵
```↵
↵
- Item 1.1.1↵
↵
```html↵
<p>HTML code block 1.1.1</p>↵
```
Renders as:
Item 1
print("Python code block 1")
Item 1.1
echo "Bash code block 1.1"
Item 1.1.1
<p>HTML code block 1.1.1</p>
The markdown below, renders a definded code block, with python
, bash
, and html
code, within an ordered list.
1. Item 1↵
↵
```python↵
print("Python code block 1")↵
```↵
↵
1. Item 1.1↵
↵
```bash↵
echo "Bash code block 1.1"↵
```↵
↵
1. Item 1.1.1↵
↵
```html↵
<p>HTML code block 1.1.1</p>↵
```
Renders as:
Item 1
print("Python code block 1")
Item 1.1
echo "Bash code block 1.1"
Item 1.1.1
<p>HTML code block 1.1.1</p>
An undefined (plain text) code block should be indented at least four spaces from the list item’s marker (e.g., -
, *
or 1.
).
The space (
) is part of the item’s marker. Count from the space onwards.
The markdown below, renders an undefinded (plain text) code block within an unordered list.
- Item 1↵
↵
Plain text code block 1↵
↵
- Item 1.1↵
↵
Plain text code block 1.1↵
↵
- Item 1.1.1↵
↵
Plain text code block 1.1.1
Renders as:
Item 1
Plain text code block 1
Item 1.1
Plain text code block 1.1
Item 1.1.1
Plain text code block 1.1.1
The markdown below, renders an undefinded (plain text) code block within an ordered list.
1. Item 1↵
↵
Plain text code block 1↵
↵
1. Item 1.1↵
↵
Plain text code block 1.1↵
↵
1. Item 1.1.1↵
↵
Plain text code block 1.1.1
Renders as:
Item 1
Plain text code block 1
Item 1.1
Plain text code block 1.1
Item 1.1.1
Plain text code block 1.1.1
Sources and recommended, further resources on the topic:
License: How to add a code block to a markdown list by Jonas Jared Jacek is licensed under CC BY-SA 4.0.
This license requires that reusers give credit to the creator. It allows reusers to distribute, remix, adapt, and build upon the material in any medium or format, for noncommercial purposes only. To give credit, provide a link back to the original source, the author, and the license e.g. like this:
<p xmlns:cc="http://creativecommons.org/ns#" xmlns:dct="http://purl.org/dc/terms/"><a property="dct:title" rel="cc:attributionURL" href="https://www.ditig.com/instructions/how-to-add-a-code-block-to-markdown-list">How to add a code block to a markdown list</a> by <a rel="cc:attributionURL dct:creator" property="cc:attributionName" href="https://www.j15k.com/">Jonas Jared Jacek</a> is licensed under <a href="https://creativecommons.org/licenses/by-sa/4.0/" target="_blank" rel="license noopener noreferrer">CC BY-SA 4.0</a>.</p>
For more information see the DITig legal page.